aws-sdk-http-async 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +182 -14
  4. data/VERSION +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44de734cf991f0362c86355fb7b92e93712ee539d9ddd63ff0d4747d8f92209e
4
- data.tar.gz: eea70d56ec295ab722bdeb49e3c346fdce48bf20259e2c8877bd518d0b134a72
3
+ metadata.gz: b40de2df441050eab62879fbb3bd3cf745efc6530ce531b0fc7ce10bd1eea1f7
4
+ data.tar.gz: 28497ecd3c0e9548f33016f933afe61e760ee3e8e91f83198b6b544d661f31d7
5
5
  SHA512:
6
- metadata.gz: be907470517f3f683b7cfbabf768a98096375b7e4a37338711c958dda2694ab39ba5c679bc53c95f18d7ca481855b2337cafcb07806cea0c4e5cf67c292c34fb
7
- data.tar.gz: 8a455d973f5395d467d4ac2b1c82471ee4d63f70f902a0b89fdc0933ec6444689afb34d099a0c99d73baf13d7756aa8260f3967b2fc6260bfa8fb170a5df8583
6
+ metadata.gz: 48c1f0750ff9afc3f06203f868b9616a32083bd96d9a04d1598328e77e3ba35982e3a7c7c5e3fe2479f49c82394c59d8fda471522cb5c57d2b8f6954824e74b2
7
+ data.tar.gz: 114ba75b96e80f10f2660d8f182620ff818a9c728d2efc4e36175a3979774498d3c2fa22d80e74db8dbe86f56cff1054853f7774727415953ed37c2812f88642
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  Unreleased Changes
2
2
  ------------------
3
3
 
4
+ 0.2.1 (2026-12-01)
5
+ ------------------
6
+
7
+ * Issue - Correct the README and the RFC: on pooled connections, fibers do not serialize on the SDK's Net::HTTP transport. The slowdown that reproduces is Ruby Bug #21270 (since Ruby 3.4 every new Net::HTTP connection to a host name blocks the event loop during its DNS lookup), which `Socket.tcp_fast_fallback = false` avoids as well. The benchmark scripts are in `benchmark/`. The library code is unchanged.
8
+
4
9
  0.2.0 (2026-11-27)
5
10
  ------------------
6
11
 
data/README.md CHANGED
@@ -2,22 +2,55 @@
2
2
 
3
3
  Async HTTP handler plugin for the AWS SDK for Ruby, built on `async-http`.
4
4
 
5
- Why:
5
+ ## Do you need this gem?
6
6
 
7
- The AWS SDK’s default HTTP transport uses Net::HTTP wrapped in a connection
8
- pool. Contrary to what you might expect, Net::HTTP itself is fiber-friendly in
9
- Ruby 3.0+—the fiber scheduler hooks into blocking I/O and yields to other fibers
10
- automatically.
7
+ Probably not. If AWS calls stall your fibers under Falcon or Async on Ruby 3.4
8
+ or later, try this first:
11
9
 
12
- The problem is that the SDK's Net::HTTP transport is synchronous end-to-end and
13
- relies on implicit scheduler hooks in Net::HTTP, OpenSSL, and DNS resolution. In
14
- practice, these hooks don't yield reliably, so the single reactor thread gets
15
- blocked often enough that all fibers serialize instead of overlapping.
16
-
17
- For high-throughput AWS APIs (DynamoDB, STS, IAM, Lambda), we need an
18
- async-compatible transport to restore concurrent I/O without threads.
10
+ ```ruby
11
+ # In a Rails app: config/initializers/tcp_fast_fallback.rb
12
+ require 'socket'
13
+ Socket.tcp_fast_fallback = false
14
+ ```
19
15
 
20
- More background in [my blog post](https://thomas-witt.com/blog/aws-sdk-http-async/).
16
+ Or set `RUBY_TCP_NO_FAST_FALLBACK=1` in the environment, which needs no code.
17
+ Either way it is process-wide. Its trade-offs are listed under
18
+ [Benchmark](#benchmark).
19
+
20
+ How this gem came about:
21
+
22
+ 1. **I had a performance problem.** Requests that fan out DynamoDB queries in
23
+ fibers under Falcon were slow in production.
24
+ 2. **I thought async-http would solve it, and it did.** The same fan-out in
25
+ threads was fast, so I concluded that the SDK’s Net::HTTP transport
26
+ serializes fibers. I wrote this gem to put the SDK on async-http, and the
27
+ problem went away.
28
+ 3. **What I can reproduce is a different cause: a bug in Ruby.** Since Ruby
29
+ 3.4, every new Net::HTTP connection to a host name blocks the event loop
30
+ during its DNS lookup
31
+ ([Ruby Bug #21270](https://bugs.ruby-lang.org/issues/21270)). It hits
32
+ fibers and not threads, and async-http does not use that code path. On
33
+ pooled connections, fibers overlap fully on the SDK’s Net::HTTP transport.
34
+ I never measured DNS latency in production, so I can’t prove that the bug
35
+ explains everything I saw.
36
+ 4. **That problem has a one-line fix,** the one above. The gem works and does
37
+ what it should, but it is now more of a technology exercise.
38
+
39
+ The numbers are in [Benchmark](#benchmark). The discussion with the AWS SDK
40
+ team is in
41
+ [aws/aws-sdk-ruby#2621](https://github.com/aws/aws-sdk-ruby/issues/2621), and
42
+ the story behind the gem is in
43
+ [my blog post](https://thomas-witt.com/blog/aws-sdk-http-async/).
44
+
45
+ What the gem still does differently from the SDK’s Net::HTTP transport:
46
+
47
+ - **The Ruby bug does not affect it,** and it needs no process-wide switch.
48
+ - **Its connection pool is bounded** per reactor and endpoint
49
+ (`async_http_connection_limit`). The SDK’s Net::HTTP pool has no limit.
50
+ - **It has an optional total request deadline** (`async_http_total_timeout`).
51
+
52
+ What it costs: about 0.1–0.2 ms more per request than Net::HTTP on pooled
53
+ connections, and one more dependency in the request path.
21
54
 
22
55
  ## Installation
23
56
 
@@ -611,6 +644,141 @@ and Set‑Cookie2 values are joined with "\n" to preserve cookie boundaries.
611
644
 
612
645
  See `md-docs/aws-sdk-http-async-rfc.md` for current design notes.
613
646
 
647
+ ## Benchmark
648
+
649
+ Ten concurrent DynamoDB `Query` calls through the SDK on cold connections (an
650
+ empty pool), against a local TLS endpoint with 50 ms of latency and a real,
651
+ slow DNS server. My January 2026 stack: Ruby 3.4.8, aws-sdk-core 3.241.3,
652
+ async 2.35.3, async-http 0.94.0, net-http 0.9.1 and 0.1.0 of this gem, in
653
+ Docker on an arm64 laptop. Median wall-clock ms:
654
+
655
+ | DNS server answers after | threads + Net::HTTP | fibers + Net::HTTP | fibers + Net::HTTP, `Socket.tcp_fast_fallback = false` | fibers + this gem |
656
+ | --- | ---: | ---: | ---: | ---: |
657
+ | 1 ms | 69 | 94 | 70 | 69 |
658
+ | 5 ms | 71 | 136 | 71 | 70 |
659
+ | 20 ms | 84 | 238 | 85 | 87 |
660
+ | 100 ms | 172 | 630 | 175 | 178 |
661
+
662
+ Each cell is the mean of two runs, each the median of five fan-outs.
663
+
664
+ - **Pooled connections are unaffected.** With a filled pool and 0.1.1 or later
665
+ of this gem, both transports take about 60 ms with fast or slow DNS.
666
+ - **Not version specific.** On Ruby 4.0.10 with 0.2.0 of this gem the 100 ms
667
+ row is 174 / 630 / 176 / 175. The fibers cell varies with machine load
668
+ (575–630).
669
+ - **630 ms, not 10 × 100 ms,** because only five of the ten calls open a
670
+ connection. The others start late enough to reuse one.
671
+ - **Cold connections are common at low traffic.** The SDK pool drops sessions
672
+ idle for more than 5 s (`http_idle_timeout`), and the regional DynamoDB name
673
+ has a 5 s TTL.
674
+ - **A fix in Ruby is proposed:**
675
+ [ruby/ruby#18370](https://github.com/ruby/ruby/pull/18370), open since
676
+ August 2026.
677
+
678
+ The one-line fix has trade-offs of its own:
679
+
680
+ - It is process-wide: it turns Happy Eyeballs off by default for every socket.
681
+ - Under Async, lookups then go through Ruby’s `Resolv` instead of libc. On a
682
+ dual-stack host `Resolv` asks for AAAA and then A, one after the other.
683
+ - On Ruby 4.0 that lookup is not covered by `http_open_timeout`: with a DNS
684
+ server that never answers, a connect took 75 s to fail (Ruby 4.0.6).
685
+
686
+ The last two points apply to this gem as well: async-http resolves through
687
+ `Resolv` too.
688
+
689
+ ### Run it yourself
690
+
691
+ The scripts are in [`benchmark/`](benchmark). They need no AWS account.
692
+
693
+ ```sh
694
+ cd benchmark
695
+
696
+ # The Ruby bug without the SDK (Ruby >= 3.4)
697
+ ruby hev2_min.rb
698
+
699
+ # One row of the table (100 ms; REAL_DNS_MS=1, 5 or 20 for the others), on Ruby 4.0
700
+ # with current gems. It starts a slow DNS server inside a disposable container.
701
+ docker run --rm -v "$PWD:/w:ro" -e REAL_DNS_MS=100 ruby:4.0 ruby /w/repro.rb
702
+
703
+ # Without Docker: the slow resolver is simulated (see the header of repro.rb)
704
+ ruby repro.rb
705
+ ```
706
+
707
+ `hev2_min.rb` opens five connections with `TCPSocket.new`, each with a
708
+ simulated 200 ms lookup. On Ruby 4.0.10 (macOS) that took 209 ms in threads
709
+ and 1021 ms in fibers. With `fast_fallback: false`, which is what
710
+ `Socket.tcp_fast_fallback = false` makes the default, the fibers took 202 ms;
711
+ in that row the lookup delay is a fiber sleep, because Ruby’s test hook does
712
+ not reach `Resolv`.
713
+
714
+ <details>
715
+ <summary>benchmark/hev2_min.rb</summary>
716
+
717
+ ```ruby
718
+ # frozen_string_literal: true
719
+
720
+ # Minimal illustration of https://bugs.ruby-lang.org/issues/21270, without the AWS SDK. From benchmark/: ruby hev2_min.rb
721
+ # Since Ruby 3.4, TCPSocket.new does Happy Eyeballs v2 and waits for its getaddrinfo(3) helper threads in a
722
+ # select(2) that bypasses the fiber scheduler: while a name resolves, no other fiber on that thread runs.
723
+ require 'bundler/inline'
724
+ gemfile do
725
+ source 'https://rubygems.org'
726
+ gem 'async'
727
+ end
728
+ require 'socket'
729
+
730
+ N = 5
731
+ DELAY_MS = 200 # per name resolution
732
+
733
+ def elapsed_ms
734
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
735
+ yield
736
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - started
737
+ end
738
+
739
+ servers = [TCPServer.new('127.0.0.1', 0)] # "localhost" may resolve to either loopback address: listen on both
740
+ PORT = servers.first.addr[1]
741
+ servers << TCPServer.new('::1', PORT) if Socket.ip_address_list.any?(&:ipv6_loopback?)
742
+ servers.each { |server| Thread.new { loop { server.accept.close } } }
743
+ connect = ->(**options) { TCPSocket.new('localhost', PORT, **options).close }
744
+
745
+ # The slow resolver is simulated. With Happy Eyeballs, Ruby's own test hook makes the helper threads sleep before
746
+ # they report: 2n+1 ms on Ruby 3.4 and 4.0, n ms on Ruby 4.1, so keep the candidate that measures closest to DELAY_MS.
747
+ HAPPY = [(DELAY_MS - 1) / 2, DELAY_MS]
748
+ .map { |n| { fast_fallback: true, test_mode_settings: { delay: { ipv4: n, ipv6: n } } } }
749
+ .min_by { |options| (elapsed_ms { connect.call(**options) } - DELAY_MS).abs }
750
+ # Without Happy Eyeballs, TCPSocket.new resolves through the fiber scheduler, which that hook does not reach.
751
+ # There the same delay is a fiber sleep, so the row marked * cannot block the reactor by construction.
752
+ Async::Scheduler.prepend(Module.new do
753
+ def address_resolve(hostname)
754
+ sleep DELAY_MS / 1000.0
755
+ super
756
+ end
757
+ end)
758
+
759
+ lookup = elapsed_ms { connect.call(**HAPPY) }.round
760
+ puts RUBY_DESCRIPTION, "async #{Async::VERSION}, Socket.tcp_fast_fallback=#{Socket.tcp_fast_fallback}",
761
+ "#{N} x TCPSocket.new('localhost', port); one of them alone, lookup delayed by the test hook, takes #{lookup} ms"
762
+ wall = elapsed_ms { Array.new(N) { Thread.new { connect.call(**HAPPY) } }.each(&:join) }
763
+ printf("%-32s wall %4d ms\n", 'threads, fast_fallback: true', wall)
764
+
765
+ rows = { 'fibers, fast_fallback: true' => HAPPY, 'fibers, fast_fallback: false *' => { fast_fallback: false } }
766
+ rows.each do |label, options|
767
+ Sync do |task|
768
+ late = 0.0 # the most a fiber that only sleeps 1 ms at a time was late
769
+ probe = task.async { loop { late = [late, elapsed_ms { sleep 0.001 } - 1].max } }
770
+ wall = elapsed_ms { Array.new(N) { task.async { connect.call(**options) } }.each(&:wait) }
771
+ sleep 0.003 # lets the timer fiber wake up once more, so it also counts a delay that ended with the last connect
772
+ printf("%-32s wall %4d ms; a 1 ms timer fiber was up to %3d ms late\n", label, wall, late)
773
+ ensure
774
+ probe&.stop
775
+ end
776
+ end
777
+ puts '* lookup delay simulated by a fiber sleep in the scheduler (Fiber.scheduler.address_resolve)'
778
+ ```
779
+
780
+ </details>
781
+
614
782
  ## Development
615
783
 
616
784
  ```bash
@@ -626,7 +794,7 @@ bin/ci
626
794
  Quick reference:
627
795
 
628
796
  ```bash
629
- VERSION=0.2.0
797
+ VERSION=0.2.1
630
798
 
631
799
  # Build + push (after version bump + changelog)
632
800
  gem build aws-sdk-http-async.gemspec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-sdk-http-async
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Witt