aws-sdk-http-async 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +39 -0
- data/README.md +329 -50
- data/VERSION +1 -1
- data/lib/async/aws/client_cache.rb +758 -97
- data/lib/async/aws/errors.rb +18 -0
- data/lib/async/aws/handler.rb +189 -53
- data/lib/async/aws/http_plugin.rb +1 -1
- metadata +274 -228
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 579b894f42ca45c4a48d752640aafd18094eee9d0a54cd0c9736568cfba9e274
|
|
4
|
+
data.tar.gz: 40c715643f4559d6860486b59473e9c49ef288c8eeb3ae893175f2f9e30be34a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 62ee7c2f000cfe6ed1c508070060ca762860df042bc7cb93d11ad2444117a7816fb22d3162a39fe4f08e3ec9bf63577d9da9e54c0a2cc2e24c76c2aa8515c5cc
|
|
7
|
+
data.tar.gz: bb2c9dc8e61902e1c0acc173bafc2b4a11d6a02dc91ca0155965fe8dfc9e51167f75a354a4b848ea06c10f703e69caa385a46bc3fc122dd0033c5b73e50820f9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
Unreleased Changes
|
|
2
2
|
------------------
|
|
3
3
|
|
|
4
|
+
0.1.1 (2026-11-26)
|
|
5
|
+
------------------
|
|
6
|
+
|
|
7
|
+
* Issue - Fix a connection leak: Seahorse builds a new handler for every API call, and each handler created its own client cache, so every call inside a reactor built a new `Async::HTTP::Client`, opened a new connection and never closed it. Handlers now share the process-wide `Async::Aws::ClientCache.default` (thread-safe and fork-safe), which reuses one client per reactor, endpoint and config. `async_http_client_cache` still overrides it.
|
|
8
|
+
|
|
9
|
+
* Issue - Drop cache entries whose reactor has finished, without waiting for the reactor to be garbage collected, and never wait on a busy pool that no fiber is left to release.
|
|
10
|
+
|
|
11
|
+
* Issue - `ClientCache#clear!` now closes only the clients of the calling reactor and of finished reactors. It no longer closes the clients of other running reactors from the wrong thread, which could leave those reactors unable to finish. `clear!(timeout:)` is one deadline for all clients and closes the connections of in-flight requests once it expires, instead of waiting for them again.
|
|
12
|
+
|
|
13
|
+
* Issue - Fix calls through `http_proxy` in short-lived reactors: a `Sync { }` block and the `:sync` fallback no longer hang after the call, also when the proxy never answers `CONNECT`. A CONNECT tunnel now belongs to its reactor instead of the request that opened it, so cancelling that request no longer breaks the pooled tunnel for other calls. Idle tunnels are retired before the proxy socket timeout breaks them, and a tunnel the proxy dropped fails the request after `http_open_timeout` instead of leaving it waiting.
|
|
14
|
+
|
|
15
|
+
* Issue - Let a timeout the caller puts around a call (`task.with_timeout { client.get_item(...) }`) reach the caller as `Async::TimeoutError`. It was reported as a networking error, so the SDK sent the request again without a deadline. The transport's own timeouts (`http_read_timeout`, `async_http_header_timeout`, `async_http_total_timeout`, the wait for a free connection) raise `Async::Aws::TransportTimeoutError` internally and are still reported as `Seahorse::Client::NetworkingError`. The total timeout of a call no longer fails (and retries) another call made from its streaming block, and it ends once the response is complete instead of interrupting response listeners. In tests, a stub that raises a plain `Async::TimeoutError` (`to_raise(Async::TimeoutError)`) now counts as the caller's timeout; stub a network timeout with WebMock's `to_timeout`.
|
|
16
|
+
|
|
17
|
+
* Issue - Treat `async_http_connection_limit: 0` or a negative value as no limit, like `nil`, instead of waiting forever for a connection that is never opened. A Float is rounded up; a value that is not a number raises `ArgumentError`.
|
|
18
|
+
|
|
19
|
+
* Issue - Because connections are now reused, a call can fail with `Seahorse::Client::NetworkingError` (`EOFError`/`Errno::ECONNRESET`) when the server closes a kept-alive connection at the moment it is reused, as with the SDK's Net::HTTP pool. The SDK's default retries absorb it; clients with retries disabled (`max_attempts: 1`; `retry_limit: 0` only in the `legacy` retry mode) see the error.
|
|
20
|
+
|
|
21
|
+
* Issue - Trim the client cache back to `async_http_max_cached_clients` when a burst of in-flight clients is released, close a client that was cleared while a request was still using it once that request finishes, and keep the cache consistent when a task is cancelled during checkout, eviction or release.
|
|
22
|
+
|
|
23
|
+
* Issue - Keep TLS settings that differ in effect apart in the client cache: relative certificate paths under different working directories, and `ssl_verify_peer` values of different types.
|
|
24
|
+
|
|
25
|
+
* Issue - Because clients are now reused, `ssl_cert`, `ssl_key` and `ssl_ca_bundle` files are read once per cached client instead of on every call (as they already were with an injected `async_http_client_cache`). After rotating such a file in place, call `Async::Aws::ClientCache.default.clear!` inside each reactor, or restart, so new connections use the new file.
|
|
26
|
+
|
|
27
|
+
* Issue - Raise the promised `ArgumentError` for an empty `ssl_cert`/`ssl_key` even when a client for the same endpoint is already cached.
|
|
28
|
+
|
|
29
|
+
* Issue - Keep WebMock-stubbed calls working across reactors: closing a finished reactor's client no longer raises `WebMock::NetConnectNotAllowedError` out of the next call.
|
|
30
|
+
|
|
31
|
+
* Issue - Log the invalid `async_http_fallback` and `async_http_streaming_uploads` warnings once per process and value (for the first 100 distinct values) instead of once per call, and share one Net::HTTP fallback handler.
|
|
32
|
+
|
|
33
|
+
* Feature - Raise the default `async_http_connection_limit` from 10 to 50. The limit is now effective per reactor and endpoint; before, every call had its own pool unless a shared cache was injected (those setups go from 10 to 50). Set `Aws.config[:async_http_connection_limit]` to tune it; see "Connection limit" in the README for queueing and nested calls from streaming blocks.
|
|
34
|
+
|
|
35
|
+
* Feature - Honor `http_idle_timeout` (default 5 seconds): a pooled connection is closed once it has been unused for one to two times that long, instead of being reused indefinitely.
|
|
36
|
+
|
|
37
|
+
* Feature - Open the connections of a shared client concurrently, so one slow connect does not hold up other calls to the same endpoint. Calls beyond `async_http_connection_limit` wait their turn before a connection is requested; that wait is bounded by `async_http_header_timeout` and `async_http_total_timeout`, and a call that gives up fails with a networking error.
|
|
38
|
+
|
|
39
|
+
* Feature - Add `Async::Aws::ClientCache.default` and `Async::Aws::ClientCache#size`.
|
|
40
|
+
|
|
41
|
+
* Feature - Require `async-http >= 0.105.0` and `async >= 2.46.0`. Connection reuse relies on async-http's idle-connection check before a pooled HTTP/1 connection is reused and on `Async::Task#defer_cancel`; older versions are not supported.
|
|
42
|
+
|
|
4
43
|
0.1.0 (2026-01-19)
|
|
5
44
|
------------------
|
|
6
45
|
|
data/README.md
CHANGED
|
@@ -2,9 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
Async HTTP handler plugin for the AWS SDK for Ruby, built on `async-http`.
|
|
4
4
|
|
|
5
|
-
Why
|
|
6
|
-
|
|
7
|
-
transport
|
|
5
|
+
Why:
|
|
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.
|
|
11
|
+
|
|
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.
|
|
19
|
+
|
|
20
|
+
More background in [my blog post](https://thomas-witt.com/blog/aws-sdk-http-async/).
|
|
8
21
|
|
|
9
22
|
## Installation
|
|
10
23
|
|
|
@@ -29,21 +42,28 @@ clients for every request.
|
|
|
29
42
|
#### Singleton Repository Pattern
|
|
30
43
|
|
|
31
44
|
```ruby
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
include Singleton
|
|
45
|
+
require 'async/semaphore'
|
|
46
|
+
require 'async/task'
|
|
35
47
|
|
|
36
|
-
|
|
48
|
+
INIT_SEMAPHORE = Async::Semaphore.new(1)
|
|
49
|
+
INIT_MUTEX = Mutex.new
|
|
37
50
|
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
def client
|
|
52
|
+
return @client if defined?(@client) && @client
|
|
40
53
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
if Async::Task.current?
|
|
55
|
+
INIT_SEMAPHORE.acquire do
|
|
56
|
+
return @client if defined?(@client) && @client
|
|
57
|
+
@client = Aws::DynamoDB::Client.new
|
|
58
|
+
end
|
|
59
|
+
else
|
|
60
|
+
INIT_MUTEX.synchronize do
|
|
61
|
+
return @client if defined?(@client) && @client
|
|
62
|
+
@client = Aws::DynamoDB::Client.new
|
|
45
63
|
end
|
|
46
64
|
end
|
|
65
|
+
|
|
66
|
+
@client
|
|
47
67
|
end
|
|
48
68
|
```
|
|
49
69
|
|
|
@@ -52,6 +72,7 @@ Initializer shortcut:
|
|
|
52
72
|
```ruby
|
|
53
73
|
# config/initializers/aws_clients.rb
|
|
54
74
|
DDB = Database::DynamodbRepository.instance unless defined?(DDB)
|
|
75
|
+
# or Rails.application.config.x.ddb = Database::DynamodbRepository.instance
|
|
55
76
|
```
|
|
56
77
|
|
|
57
78
|
Usage:
|
|
@@ -62,8 +83,7 @@ DDB.client.list_tables
|
|
|
62
83
|
|
|
63
84
|
#### Dependency Injection Pattern
|
|
64
85
|
|
|
65
|
-
Alternative dependency injection example (
|
|
66
|
-
if you need to swap clients per context):
|
|
86
|
+
Alternative dependency injection example (ideal for tests or per‑context configs):
|
|
67
87
|
|
|
68
88
|
```ruby
|
|
69
89
|
class WorldDominationService
|
|
@@ -77,14 +97,43 @@ class WorldDominationService
|
|
|
77
97
|
end
|
|
78
98
|
|
|
79
99
|
service = WorldDominationService.new(dynamodb: Aws::DynamoDB::Client.new)
|
|
100
|
+
# or Rails.application.config.x.dynamodb_client = Aws::DynamoDB::Client.new
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Advanced Configuration (Force Async Outside Falcon)
|
|
104
|
+
|
|
105
|
+
To force async execution for all Rake tasks, add this line at the very top of
|
|
106
|
+
your `Rakefile`, before any task definitions or Rails app loading:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
require 'aws-sdk-http-async/rake'
|
|
80
110
|
```
|
|
81
111
|
|
|
82
112
|
### File Handle Limits (EMFILE)
|
|
83
113
|
|
|
84
114
|
If you see `Errno::EMFILE` or "Too many open files", raise your file descriptor
|
|
85
|
-
limit.
|
|
115
|
+
limit. This commonly occurs when processing many records with concurrent AWS
|
|
116
|
+
calls, especially when combined with Redis, OpenSearch, or database connections.
|
|
117
|
+
|
|
118
|
+
Each AWS request can hold open multiple file descriptors (TCP sockets, SSL
|
|
119
|
+
contexts). Combined with Redis pub/sub, OpenSearch indexing, or database
|
|
120
|
+
connections, you can quickly exceed the default limit (256 on macOS, 1024 on
|
|
121
|
+
many Linux distros). Verify via `ulimit -n`.
|
|
122
|
+
|
|
123
|
+
#### Recommended Dev Env: Add to Your Shell Profile
|
|
124
|
+
|
|
125
|
+
Add to your `~/.zshrc` or `~/.bashrc`:
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
ulimit -n 65536 2>/dev/null || ulimit -n 4096 2>/dev/null || true
|
|
129
|
+
```
|
|
86
130
|
|
|
87
|
-
|
|
131
|
+
This applies to all terminal sessions (Falcon, rake tasks, console). Re-open
|
|
132
|
+
your terminal or run `source ~/.zshrc` for changes to take effect.
|
|
133
|
+
|
|
134
|
+
#### For Falcon/bin/dev
|
|
135
|
+
|
|
136
|
+
If you prefer per-project configuration, add to your `bin/dev`:
|
|
88
137
|
|
|
89
138
|
```sh
|
|
90
139
|
if command -v ulimit >/dev/null; then
|
|
@@ -96,8 +145,10 @@ if command -v ulimit >/dev/null; then
|
|
|
96
145
|
fi
|
|
97
146
|
```
|
|
98
147
|
|
|
99
|
-
|
|
100
|
-
|
|
148
|
+
#### For Docker
|
|
149
|
+
|
|
150
|
+
Add `ULIMIT_NOFILE=65536` to your environment and include in
|
|
151
|
+
`bin/docker-entrypoint`:
|
|
101
152
|
|
|
102
153
|
```sh
|
|
103
154
|
if command -v ulimit >/dev/null; then
|
|
@@ -105,16 +156,6 @@ if command -v ulimit >/dev/null; then
|
|
|
105
156
|
fi
|
|
106
157
|
```
|
|
107
158
|
|
|
108
|
-
### Advanced Configuration (Force Async Outside Falcon)
|
|
109
|
-
|
|
110
|
-
To force async execution for all Rake tasks, add this line at the very top of
|
|
111
|
-
your `Rakefile`, before any task definitions or Rails app loading (usually after
|
|
112
|
-
`require_relative '../config/boot'`):
|
|
113
|
-
|
|
114
|
-
```ruby
|
|
115
|
-
require 'aws-sdk-http-async/rake'
|
|
116
|
-
```
|
|
117
|
-
|
|
118
159
|
## Configuration
|
|
119
160
|
|
|
120
161
|
### Fallback Behavior (No Reactor)
|
|
@@ -158,7 +199,7 @@ Supported options (inherited from Net::HTTP plugin):
|
|
|
158
199
|
|
|
159
200
|
- `http_open_timeout`
|
|
160
201
|
- `http_read_timeout`
|
|
161
|
-
- `http_idle_timeout` (
|
|
202
|
+
- `http_idle_timeout` (default: 5; a pooled connection is closed once it has been unused for one to two times this many seconds)
|
|
162
203
|
- `ssl_verify_peer`
|
|
163
204
|
- `ssl_ca_store`
|
|
164
205
|
- `ssl_ca_bundle`
|
|
@@ -187,6 +228,11 @@ Aws::DynamoDB::Client.new(
|
|
|
187
228
|
|
|
188
229
|
Tip: Reuse OpenSSL certificate/key objects across client instances to keep cache keys stable.
|
|
189
230
|
|
|
231
|
+
Certificate and key **files** are read once per cached client, not per request.
|
|
232
|
+
After rotating files in place, call `clear!` on the cache in use (by default
|
|
233
|
+
`Async::Aws::ClientCache.default`) inside each reactor, or restart, so new
|
|
234
|
+
connections present the new certificate.
|
|
235
|
+
|
|
190
236
|
#### Proxy Support Notes
|
|
191
237
|
|
|
192
238
|
`http_proxy` accepts a full URL, e.g.:
|
|
@@ -204,10 +250,20 @@ Limitations:
|
|
|
204
250
|
- HTTPS proxies reuse the same SSL verification options as the target (CA
|
|
205
251
|
store/bundle/dir).
|
|
206
252
|
- All requests for the client go through the configured proxy.
|
|
253
|
+
- Tunnels are pooled like direct connections. A tunnel belongs to its reactor,
|
|
254
|
+
not to the request that opened it: cancelling that request leaves the tunnel
|
|
255
|
+
usable for other calls, and an open tunnel does not keep a short‑lived
|
|
256
|
+
`Sync { }` block from finishing.
|
|
257
|
+
- A tunnel socket gets the same I/O timeout as a direct connection
|
|
258
|
+
(`http_open_timeout`), so a tunnel the proxy dropped fails the request
|
|
259
|
+
instead of leaving it waiting.
|
|
260
|
+
- When the reactor is stopped (SIGINT), idle tunnels are closed at once and a
|
|
261
|
+
tunnel in use is closed after its request, like direct connections: requests
|
|
262
|
+
that a server is still answering (`defer_stop`) can finish.
|
|
207
263
|
|
|
208
264
|
### Plugin options
|
|
209
265
|
|
|
210
|
-
- `async_http_connection_limit` (default:
|
|
266
|
+
- `async_http_connection_limit` (default: 50; max connections per reactor and endpoint, `nil` or `0` for unlimited, see [Connection limit](#connection-limit))
|
|
211
267
|
- `async_http_force_accept_encoding` (default: true)
|
|
212
268
|
- `async_http_body_warn_bytes` (default: 5_242_880)
|
|
213
269
|
- `async_http_max_buffer_bytes` (default: 5MB; raise if buffering exceeds this; set to `nil` or `0` for unlimited)
|
|
@@ -217,7 +273,7 @@ store/bundle/dir).
|
|
|
217
273
|
- `async_http_max_cached_clients` (default: 100; LRU eviction per reactor when exceeded)
|
|
218
274
|
- `async_http_streaming_uploads` (default: :auto; values: :auto, :force, :off)
|
|
219
275
|
- `async_http_fallback` (default: :net_http; values: :net_http, :sync, :raise)
|
|
220
|
-
- `async_http_client_cache` (default: nil,
|
|
276
|
+
- `async_http_client_cache` (default: nil, which uses the process-wide `Async::Aws::ClientCache.default`; inject a cache for lifecycle control)
|
|
221
277
|
|
|
222
278
|
### Timeout semantics
|
|
223
279
|
|
|
@@ -231,6 +287,19 @@ indefinitely; add your own total deadline if needed. Use
|
|
|
231
287
|
`async_http_total_timeout` to enforce an overall deadline (upload + headers +
|
|
232
288
|
body).
|
|
233
289
|
|
|
290
|
+
When one of these timeouts expires, the call fails with a
|
|
291
|
+
`Seahorse::Client::NetworkingError`, which the SDK may retry. A timeout you put
|
|
292
|
+
around a call yourself is not one of them: `task.with_timeout(2) {
|
|
293
|
+
client.get_item(...) }` raises `Async::TimeoutError` to your code after 2
|
|
294
|
+
seconds and the call is not retried. (Up to 0.1.0 such a timeout was reported
|
|
295
|
+
as a networking error and the request was sent again without a deadline.)
|
|
296
|
+
|
|
297
|
+
One case is not covered: an upload to a peer that has stopped reading. Closing
|
|
298
|
+
that connection waits for the socket timeout (`http_open_timeout`), so your
|
|
299
|
+
timeout arrives that much later, and if one of the transport's timeouts has
|
|
300
|
+
already expired for the attempt, your timeout is lost and the attempt is
|
|
301
|
+
reported as a networking error (see "Known limits" in the RFC).
|
|
302
|
+
|
|
234
303
|
### Opt-out of auto-patching
|
|
235
304
|
|
|
236
305
|
If you want to load the handler without global patching:
|
|
@@ -259,31 +328,135 @@ inherit the plugin from their parent class and are not tracked for removal.
|
|
|
259
328
|
Note: patching scans existing classes at load time; require this gem early in
|
|
260
329
|
boot for best results.
|
|
261
330
|
|
|
262
|
-
##
|
|
331
|
+
## Connection reuse
|
|
332
|
+
|
|
333
|
+
The gem keeps one `Async::HTTP::Client` (a connection pool) per **reactor +
|
|
334
|
+
endpoint + connection‑relevant config** in a process‑wide cache,
|
|
335
|
+
`Async::Aws::ClientCache.default`. Every AWS client and every API call in the
|
|
336
|
+
process shares it, so calls inside one reactor reuse connections instead of
|
|
337
|
+
opening a new one each time. There is nothing to configure.
|
|
338
|
+
|
|
339
|
+
- **Per reactor.** A pool belongs to the reactor that created it, so each
|
|
340
|
+
Falcon worker (or each thread running its own reactor) has its own pools.
|
|
341
|
+
When a reactor finishes, its pools close their connections and the cache
|
|
342
|
+
drops the entries.
|
|
343
|
+
- **Transient reactors.** `Sync { }` around a single call, and the `:sync`
|
|
344
|
+
fallback mode, create one reactor per call, so each call opens and closes its
|
|
345
|
+
own connection. Wrap the whole unit of work in one `Sync { }` to reuse
|
|
346
|
+
connections.
|
|
347
|
+
- **Bounded.** `async_http_max_cached_clients` (default: 100) caps the cached
|
|
348
|
+
clients per reactor with LRU eviction; an evicted client is closed. Clients
|
|
349
|
+
with a request in flight are never evicted, so a burst can exceed the cap
|
|
350
|
+
until those requests finish. Set it to `nil` or `0` to disable eviction.
|
|
351
|
+
All AWS clients in a reactor share the cache: the cap is applied whenever a
|
|
352
|
+
client is added, with the cap of the AWS client that adds it, to all of that
|
|
353
|
+
reactor's cached clients.
|
|
354
|
+
- **Idle connections.** A connection is closed once it has been unused for one
|
|
355
|
+
to two times `http_idle_timeout` (default: 5 seconds), so a quiet pool
|
|
356
|
+
shrinks again and does not hand out connections that a NAT gateway or
|
|
357
|
+
firewall dropped in the meantime.
|
|
358
|
+
- **Reused connections can fail once.** A server may close a kept‑alive
|
|
359
|
+
connection at the moment a call reuses it. Connections that were already
|
|
360
|
+
closed when they are picked are replaced, but a request that crosses the
|
|
361
|
+
close on the wire fails with `Seahorse::Client::NetworkingError`
|
|
362
|
+
(`EOFError` or `Errno::ECONNRESET`), as with the SDK's Net::HTTP pool. The
|
|
363
|
+
SDK retries it on a new connection; with retries disabled
|
|
364
|
+
(`max_attempts: 1`; `retry_limit: 0` only in the `legacy` retry mode) the
|
|
365
|
+
error reaches your code.
|
|
366
|
+
- **Fork‑safe.** A forked child starts with an empty cache and writes nothing
|
|
367
|
+
to the parent's connections (it closes its own copies of the sockets the
|
|
368
|
+
first time it uses the cache; a child that only makes reactorless
|
|
369
|
+
Net::HTTP fallback calls keeps them until it exits). Fork outside a reactor, as preforking servers do;
|
|
370
|
+
forking in the middle of a running reactor is not supported. A connection
|
|
371
|
+
that another thread's reactor was still opening at the moment of the fork
|
|
372
|
+
(connect or TLS handshake) is not in a pool yet: the child's copy of that
|
|
373
|
+
socket stays open until it is garbage collected.
|
|
374
|
+
|
|
375
|
+
### Connection limit
|
|
376
|
+
|
|
377
|
+
`async_http_connection_limit` (default: 50) is the maximum number of
|
|
378
|
+
connections of one cached client, i.e. **per reactor and endpoint** (AWS
|
|
379
|
+
clients whose connection config differs, e.g. another `http_open_timeout`, TLS
|
|
380
|
+
setting or proxy, get separate clients with their own limit). The gem speaks
|
|
381
|
+
HTTP/1.1, where one connection carries one request at a time, so it is also
|
|
382
|
+
the maximum number of requests in flight to that endpoint. Further calls wait
|
|
383
|
+
their turn. New connections are opened concurrently, so a cold burst does not
|
|
384
|
+
queue behind one handshake. The limit counts requests: a connection that is
|
|
385
|
+
still finishing an upload after the server answered early is not counted, so
|
|
386
|
+
the pool can hold more connections than the limit. The surplus goes when it
|
|
387
|
+
has been idle for `http_idle_timeout`; with `http_idle_timeout: 0` it stays
|
|
388
|
+
until the client is closed. `nil`, `0` or a negative value means no limit.
|
|
389
|
+
|
|
390
|
+
- **Higher** allows more parallel calls (a request that fans out 32–64
|
|
391
|
+
concurrent calls needs at least that many to avoid queueing), at the cost of
|
|
392
|
+
more sockets and more load that AWS may throttle. Budget up to `limit` file
|
|
393
|
+
descriptors per cached client (three per connection through a proxy), see
|
|
394
|
+
[File Handle Limits](#file-handle-limits-emfile).
|
|
395
|
+
- **Lower** keeps the socket count small; excess calls queue.
|
|
396
|
+
|
|
397
|
+
A call that waits its turn is bounded by `async_http_header_timeout` (the wait
|
|
398
|
+
for a turn and the wait for response headers each get that long) and by
|
|
399
|
+
`async_http_total_timeout`. Both are off by default: without one of them a
|
|
400
|
+
queued call waits indefinitely. A call that gives up fails with a networking
|
|
401
|
+
error, which the SDK may retry.
|
|
402
|
+
|
|
403
|
+
Stopping a batch of calls that is larger than the limit (for example
|
|
404
|
+
`Async::Barrier#stop`) stops them one after the other, and each stopped call
|
|
405
|
+
hands its turn to a queued one, which may open a connection before it is
|
|
406
|
+
stopped too.
|
|
407
|
+
|
|
408
|
+
A call made **inside a streaming block** (e.g. `get_object { |chunk| ... }`)
|
|
409
|
+
to the same endpoint needs a second connection while the outer response still
|
|
410
|
+
holds its own. If every connection is held by such an outer response, the
|
|
411
|
+
nested calls can never start. Keep the limit above the number of concurrent
|
|
412
|
+
streaming responses that make nested calls, or move the nested call out of
|
|
413
|
+
the block. The same goes for a call made from a response listener
|
|
414
|
+
(`on_headers`, `on_data`, `on_done`, `on_error` of the HTTP response): the
|
|
415
|
+
outer call keeps its turn until its listeners have run.
|
|
416
|
+
|
|
417
|
+
```ruby
|
|
418
|
+
Aws.config[:async_http_connection_limit] = 100 # per reactor and endpoint
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
Before 0.1.1 the limit only applied when you injected a shared cache;
|
|
422
|
+
otherwise every call got its own pool (and leaked it).
|
|
423
|
+
|
|
424
|
+
### Cache injection
|
|
263
425
|
|
|
264
426
|
```ruby
|
|
265
427
|
cache = Async::Aws::ClientCache.new
|
|
266
428
|
client = Aws::DynamoDB::Client.new(async_http_client_cache: cache)
|
|
267
429
|
```
|
|
268
430
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
431
|
+
An injected cache replaces the default cache for that AWS client. It caches
|
|
432
|
+
**async‑http client instances** (connection pools), not AWS responses. **Most
|
|
433
|
+
apps should ignore this** and use the default cache. Inject a cache only when
|
|
434
|
+
you need one of these:
|
|
273
435
|
|
|
274
436
|
- **Deterministic shutdown** in tests or short‑lived scripts (so servers don’t
|
|
275
437
|
hang waiting for pooled connections to drain).
|
|
276
|
-
- **
|
|
277
|
-
|
|
438
|
+
- **A separate pool** for some AWS clients, isolated from the rest of the
|
|
439
|
+
process.
|
|
278
440
|
- **Custom lifecycle hooks** (e.g., you want to clear the pool on reload).
|
|
279
441
|
|
|
280
442
|
If you inject a cache, call `clear!`/`close!` during shutdown to close pooled
|
|
281
|
-
connections.
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
443
|
+
connections; `Async::Aws::ClientCache.default.clear!` does the same for the
|
|
444
|
+
default cache. `clear!` closes the clients of the reactor it is called in, plus
|
|
445
|
+
those of reactors that have already finished. It never closes the clients of
|
|
446
|
+
another running reactor (their sockets belong to that reactor's thread), so in
|
|
447
|
+
cross‑reactor apps call it inside each reactor.
|
|
448
|
+
|
|
449
|
+
Without a timeout, `clear!` waits for the calling reactor's in‑flight requests.
|
|
450
|
+
`clear!(timeout: 5)` waits up to 5 seconds in total, then closes their
|
|
451
|
+
connections; those attempts fail with a networking error, which the SDK may
|
|
452
|
+
retry on a new connection. A request that is still opening a direct
|
|
453
|
+
connection when the timeout expires is not interrupted; its client is closed
|
|
454
|
+
when it finishes. The same holds for a real request made while WebMock is
|
|
455
|
+
enabled, if WebMock allows that request but not its endpoint as a whole
|
|
456
|
+
(a path-specific `allow:`). `clear!` closes what exists at that moment: it does not stop later
|
|
457
|
+
calls from opening new connections. Inside a request's own block (for example
|
|
458
|
+
a streaming block), always pass a timeout: without one, `clear!` would wait
|
|
459
|
+
for that very request.
|
|
287
460
|
|
|
288
461
|
Cold‑start behavior: the cache is **not** gated. If multiple fibers hit a
|
|
289
462
|
totally cold cache for the same endpoint at the same time, they may build
|
|
@@ -300,6 +473,23 @@ WebMock works for unit tests, but connection pooling can keep sockets open. Use
|
|
|
300
473
|
`WebMock.disable_net_connect!(allow_localhost: true)` and isolate tests that
|
|
301
474
|
rely on real IO.
|
|
302
475
|
|
|
476
|
+
The default client cache is process‑wide, so it outlives a single example. An
|
|
477
|
+
example that runs in its own `Sync { }` leaves no open connections behind,
|
|
478
|
+
because its reactor ends with it. If your examples share one long‑lived
|
|
479
|
+
reactor, call `Async::Aws::ClientCache.default.clear!` in an `after` hook; that
|
|
480
|
+
also matters when WebMock is switched on or off between examples.
|
|
481
|
+
|
|
482
|
+
To stub a network timeout, use WebMock's `to_timeout` (or
|
|
483
|
+
`to_raise(IO::TimeoutError)`, `to_raise(Errno::ETIMEDOUT)`): the call fails with
|
|
484
|
+
a `Seahorse::Client::NetworkingError` and is retried. `to_raise(Async::TimeoutError)`
|
|
485
|
+
looks like a timeout you put around the call yourself, so it reaches your code
|
|
486
|
+
as it is, without a retry.
|
|
487
|
+
|
|
488
|
+
With WebMock enabled, a real (allowed) request through `http_proxy` waits
|
|
489
|
+
forever: WebMock reads the body of the CONNECT request, which stays open for
|
|
490
|
+
as long as the tunnel does. Use `WebMock.disable!` around examples that go
|
|
491
|
+
through a real proxy.
|
|
492
|
+
|
|
303
493
|
### Docker integration tests
|
|
304
494
|
|
|
305
495
|
RSpec Integration tests run against DynamoDB Local + MinIO,
|
|
@@ -307,8 +497,96 @@ and proxy tests use tinyproxy + toxiproxy.
|
|
|
307
497
|
|
|
308
498
|
### RSpec/rails test setup
|
|
309
499
|
|
|
310
|
-
|
|
311
|
-
|
|
500
|
+
By default, when no Async reactor is present, the gem falls back to Net::HTTP.
|
|
501
|
+
To exercise the async-http code path in tests, use one of these approaches:
|
|
502
|
+
|
|
503
|
+
#### Option A: Global `around` hook (Recommended)
|
|
504
|
+
|
|
505
|
+
Tag specs with `:async` and wrap them in a reactor:
|
|
506
|
+
|
|
507
|
+
```ruby
|
|
508
|
+
# spec/support/async.rb
|
|
509
|
+
RSpec.configure do |config|
|
|
510
|
+
config.around(:each, :async) do |example|
|
|
511
|
+
Sync { example.run }
|
|
512
|
+
end
|
|
513
|
+
end
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
```ruby
|
|
517
|
+
# spec/integration/dynamodb_spec.rb
|
|
518
|
+
RSpec.describe 'DynamoDB operations', :async do
|
|
519
|
+
let(:client) { Aws::DynamoDB::Client.new }
|
|
520
|
+
|
|
521
|
+
it 'lists tables' do
|
|
522
|
+
expect(client.list_tables.table_names).to be_an(Array)
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
#### Option B: Set fallback to `:sync`
|
|
528
|
+
|
|
529
|
+
Force all AWS calls to use async-http inside a transient reactor:
|
|
530
|
+
|
|
531
|
+
```ruby
|
|
532
|
+
# spec/spec_helper.rb
|
|
533
|
+
Aws.config[:async_http_fallback] = :sync
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
Or via environment variable:
|
|
537
|
+
|
|
538
|
+
```bash
|
|
539
|
+
AWS_SDK_HTTP_ASYNC_FALLBACK=sync bundle exec rspec
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
#### Option C: Wrap individual tests
|
|
543
|
+
|
|
544
|
+
For selective async testing:
|
|
545
|
+
|
|
546
|
+
```ruby
|
|
547
|
+
it 'performs async operation' do
|
|
548
|
+
Sync do
|
|
549
|
+
client = Aws::DynamoDB::Client.new
|
|
550
|
+
client.list_tables
|
|
551
|
+
end
|
|
552
|
+
end
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
#### Cache injection for deterministic cleanup
|
|
556
|
+
|
|
557
|
+
Inject a cache to control connection lifecycle and avoid test hangs:
|
|
558
|
+
|
|
559
|
+
```ruby
|
|
560
|
+
RSpec.describe 'AWS operations', :async do
|
|
561
|
+
let(:cache) { Async::Aws::ClientCache.new }
|
|
562
|
+
let(:client) do
|
|
563
|
+
Aws::DynamoDB::Client.new(
|
|
564
|
+
async_http_client_cache: cache,
|
|
565
|
+
async_http_fallback: :raise
|
|
566
|
+
)
|
|
567
|
+
end
|
|
568
|
+
|
|
569
|
+
after { cache.close! }
|
|
570
|
+
|
|
571
|
+
it 'lists tables' do
|
|
572
|
+
expect(client.list_tables.table_names).to be_an(Array)
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
#### Testing concurrent fiber execution
|
|
578
|
+
|
|
579
|
+
```ruby
|
|
580
|
+
it 'handles parallel calls', :async do
|
|
581
|
+
results = Async do |task|
|
|
582
|
+
5.times.map do
|
|
583
|
+
task.async { client.list_tables.table_names }
|
|
584
|
+
end.map(&:wait)
|
|
585
|
+
end.wait
|
|
586
|
+
|
|
587
|
+
expect(results).to all(be_an(Array))
|
|
588
|
+
end
|
|
589
|
+
```
|
|
312
590
|
|
|
313
591
|
The gem also supplies a simple `async-rake` script for testing async behavior in Rake
|
|
314
592
|
|
|
@@ -348,7 +626,7 @@ bin/ci
|
|
|
348
626
|
Quick reference:
|
|
349
627
|
|
|
350
628
|
```bash
|
|
351
|
-
VERSION=0.1.
|
|
629
|
+
VERSION=0.1.1
|
|
352
630
|
|
|
353
631
|
# Build + push (after version bump + changelog)
|
|
354
632
|
gem build aws-sdk-http-async.gemspec
|
|
@@ -365,14 +643,15 @@ GitHub release (optional, via `gh`):
|
|
|
365
643
|
gh release create "v${VERSION}" \
|
|
366
644
|
--title "v${VERSION}" \
|
|
367
645
|
--generate-notes \
|
|
368
|
-
|
|
646
|
+
"aws-sdk-http-async-${VERSION}.gem"
|
|
369
647
|
```
|
|
370
648
|
|
|
371
649
|
### Requirements
|
|
372
650
|
|
|
373
651
|
- Ruby 3.4+
|
|
374
652
|
- aws-sdk-core >= 3.241.0
|
|
375
|
-
- async
|
|
653
|
+
- async >= 2.46.0
|
|
654
|
+
- async-http >= 0.105.0
|
|
376
655
|
|
|
377
656
|
No backwards compatibility is maintained for older Ruby/AWS SDK versions.
|
|
378
657
|
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.1.
|
|
1
|
+
0.1.1
|