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
data/lib/async/aws/errors.rb
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
|
+
require 'async'
|
|
2
|
+
|
|
1
3
|
module Async
|
|
2
4
|
module Aws
|
|
3
5
|
class NoReactorError < RuntimeError; end
|
|
4
6
|
class BodyTooLargeError < RuntimeError; end
|
|
7
|
+
|
|
8
|
+
# A deadline of the transport itself (`http_read_timeout`,
|
|
9
|
+
# `async_http_header_timeout`, `async_http_total_timeout`, the wait for a
|
|
10
|
+
# free connection). Its own class tells it apart from a timeout the caller
|
|
11
|
+
# put around the call (`task.with_timeout { client.get_item(...) }`), which
|
|
12
|
+
# must reach the caller as it is.
|
|
13
|
+
class TransportTimeoutError < Async::TimeoutError
|
|
14
|
+
# The handler invocation whose `async_http_total_timeout` this is. That
|
|
15
|
+
# deadline also runs while a streaming block makes another AWS call,
|
|
16
|
+
# which must not take it for a timeout of its own. Nil for the deadlines
|
|
17
|
+
# that only run inside the invocation that set them.
|
|
18
|
+
#
|
|
19
|
+
# @api private
|
|
20
|
+
# @return [Object, nil]
|
|
21
|
+
attr_accessor :scope
|
|
22
|
+
end
|
|
5
23
|
end
|
|
6
24
|
end
|
data/lib/async/aws/handler.rb
CHANGED
|
@@ -13,23 +13,89 @@ require 'seahorse/client/networking_error'
|
|
|
13
13
|
module Async
|
|
14
14
|
module Aws
|
|
15
15
|
class Handler < Seahorse::Client::Handler
|
|
16
|
+
# Seahorse builds a new handler for every API call, so anything that must
|
|
17
|
+
# outlive one call (warn-once bookkeeping, the Net::HTTP fallback handler)
|
|
18
|
+
# lives on the class.
|
|
16
19
|
@transport_mutex = Mutex.new
|
|
17
20
|
@transport_logged = {}
|
|
21
|
+
@warned = {}
|
|
22
|
+
@shared_state_pid = Process.pid
|
|
23
|
+
@net_http_handler = nil
|
|
24
|
+
WARN_ONCE_LIMIT = 100
|
|
18
25
|
|
|
19
26
|
class << self
|
|
20
27
|
# @param kind [Symbol]
|
|
21
28
|
# @param logger [Logger, nil]
|
|
22
29
|
# @return [void]
|
|
23
30
|
def log_transport_once(kind, logger)
|
|
24
|
-
return unless kind == :async_http
|
|
31
|
+
return unless kind == :async_http && logger
|
|
25
32
|
|
|
26
33
|
@transport_mutex.synchronize do
|
|
34
|
+
forget_inherited_state
|
|
27
35
|
return if @transport_logged[kind]
|
|
28
36
|
|
|
29
37
|
@transport_logged[kind] = true
|
|
30
38
|
end
|
|
31
39
|
|
|
32
|
-
logger
|
|
40
|
+
logger.info('[aws-sdk-http-async] using async-http transport')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Logs `message` as a warning the first time `key` is seen in this
|
|
44
|
+
# process. A call without a logger does not count as seen.
|
|
45
|
+
#
|
|
46
|
+
# @api private
|
|
47
|
+
# @param key [String]
|
|
48
|
+
# @param logger [Logger, nil]
|
|
49
|
+
# @param message [String]
|
|
50
|
+
# @return [void]
|
|
51
|
+
def warn_once(key, logger, message)
|
|
52
|
+
return unless logger
|
|
53
|
+
|
|
54
|
+
@transport_mutex.synchronize do
|
|
55
|
+
forget_inherited_state
|
|
56
|
+
return if @warned[key]
|
|
57
|
+
|
|
58
|
+
# Bounded: past the limit a new key warns on every occurrence.
|
|
59
|
+
@warned[key] = true if @warned.size < WARN_ONCE_LIMIT
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
logger.warn(message)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# @api private
|
|
66
|
+
# @return [Seahorse::Client::NetHttp::Handler] shared stateless fallback handler
|
|
67
|
+
def net_http_handler
|
|
68
|
+
return @net_http_handler if @net_http_handler
|
|
69
|
+
|
|
70
|
+
@transport_mutex.synchronize do
|
|
71
|
+
@net_http_handler ||= begin
|
|
72
|
+
require 'seahorse/client/net_http/handler'
|
|
73
|
+
Seahorse::Client::NetHttp::Handler.new(nil)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Forgets the log-once/warn-once state. Intended for tests.
|
|
79
|
+
#
|
|
80
|
+
# @api private
|
|
81
|
+
# @return [void]
|
|
82
|
+
def reset_shared_state!
|
|
83
|
+
@transport_mutex.synchronize do
|
|
84
|
+
@transport_logged.clear
|
|
85
|
+
@warned.clear
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
# "Once per process": a forked child logs again, possibly to another
|
|
92
|
+
# destination than its parent. Called with the mutex held.
|
|
93
|
+
def forget_inherited_state
|
|
94
|
+
return if @shared_state_pid == Process.pid
|
|
95
|
+
|
|
96
|
+
@transport_logged.clear
|
|
97
|
+
@warned.clear
|
|
98
|
+
@shared_state_pid = Process.pid
|
|
33
99
|
end
|
|
34
100
|
end
|
|
35
101
|
|
|
@@ -163,14 +229,12 @@ module Async
|
|
|
163
229
|
].freeze
|
|
164
230
|
|
|
165
231
|
# @param handler [Seahorse::Client::Handler, nil]
|
|
166
|
-
# @param client_cache [Async::Aws::ClientCache, nil]
|
|
232
|
+
# @param client_cache [Async::Aws::ClientCache, nil] defaults to the
|
|
233
|
+
# process-wide {Async::Aws::ClientCache.default}
|
|
167
234
|
# @return [void]
|
|
168
235
|
def initialize(handler = nil, client_cache: nil)
|
|
169
236
|
super(handler)
|
|
170
|
-
@client_cache = client_cache
|
|
171
|
-
@fallback_mutex = Mutex.new
|
|
172
|
-
@invalid_fallback_warned = {}
|
|
173
|
-
@invalid_streaming_warned = {}
|
|
237
|
+
@client_cache = client_cache
|
|
174
238
|
end
|
|
175
239
|
|
|
176
240
|
# @param context [Seahorse::Client::RequestContext]
|
|
@@ -229,18 +293,34 @@ module Async
|
|
|
229
293
|
when :raise
|
|
230
294
|
raise NoReactorError, 'Async reactor is required. Wrap calls in Sync { }.'
|
|
231
295
|
when :sync
|
|
232
|
-
Sync
|
|
296
|
+
# Without a scheduler, Sync creates a reactor that ends with the block.
|
|
297
|
+
transient_reactor = Fiber.scheduler.nil?
|
|
298
|
+
Sync do
|
|
299
|
+
call_with_reactor(context)
|
|
300
|
+
ensure
|
|
301
|
+
close_transient_clients(context.config) if transient_reactor
|
|
302
|
+
end
|
|
233
303
|
else
|
|
234
304
|
return @handler.call(context) if @handler
|
|
235
305
|
|
|
236
|
-
net_http_handler.call(context)
|
|
306
|
+
Handler.net_http_handler.call(context)
|
|
237
307
|
end
|
|
238
308
|
end
|
|
239
309
|
|
|
310
|
+
# The reactor of a :sync call ends with it: close its clients first, so
|
|
311
|
+
# their entries do not stay in the cache. An injected cache-like object
|
|
312
|
+
# manages its own lifecycle.
|
|
313
|
+
def close_transient_clients(config)
|
|
314
|
+
cache = client_cache(config)
|
|
315
|
+
cache.clear! if cache.is_a?(ClientCache)
|
|
316
|
+
rescue StandardError => e
|
|
317
|
+
logger_for(config)&.warn("[aws-sdk-http-async] failed to close clients of the :sync reactor: #{e.message}")
|
|
318
|
+
end
|
|
319
|
+
|
|
240
320
|
def force_net_http(context)
|
|
241
321
|
return @handler.call(context) if @handler
|
|
242
322
|
|
|
243
|
-
net_http_handler.call(context)
|
|
323
|
+
Handler.net_http_handler.call(context)
|
|
244
324
|
end
|
|
245
325
|
|
|
246
326
|
def fallback_mode(config)
|
|
@@ -263,19 +343,15 @@ module Async
|
|
|
263
343
|
end
|
|
264
344
|
|
|
265
345
|
def warn_invalid_fallback_once(source, value, config)
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
@invalid_fallback_warned[key] = true
|
|
271
|
-
end
|
|
272
|
-
logger_for(config)&.warn(
|
|
273
|
-
"[aws-sdk-http-async] invalid async_http_fallback #{source}=#{value.inspect}; using :net_http"
|
|
346
|
+
Handler.warn_once(
|
|
347
|
+
"fallback:#{source}:#{value.inspect}",
|
|
348
|
+
logger_for(config),
|
|
349
|
+
"[aws-sdk-http-async] invalid async_http_fallback #{source}=#{value.inspect}; using :net_http",
|
|
274
350
|
)
|
|
275
351
|
end
|
|
276
352
|
|
|
277
353
|
def log_transport_once(kind, config)
|
|
278
|
-
|
|
354
|
+
Handler.log_transport_once(kind, logger_for(config))
|
|
279
355
|
end
|
|
280
356
|
|
|
281
357
|
def streaming_mode(config)
|
|
@@ -290,72 +366,99 @@ module Async
|
|
|
290
366
|
end
|
|
291
367
|
|
|
292
368
|
def warn_invalid_streaming_once(value, config)
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
@invalid_streaming_warned[key] = true
|
|
298
|
-
end
|
|
299
|
-
logger_for(config)&.warn(
|
|
300
|
-
"[aws-sdk-http-async] invalid async_http_streaming_uploads=#{value.inspect}; using :auto"
|
|
369
|
+
Handler.warn_once(
|
|
370
|
+
"streaming:#{value.inspect}",
|
|
371
|
+
logger_for(config),
|
|
372
|
+
"[aws-sdk-http-async] invalid async_http_streaming_uploads=#{value.inspect}; using :auto",
|
|
301
373
|
)
|
|
302
374
|
end
|
|
303
375
|
|
|
304
|
-
def
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
@fallback_mutex.synchronize do
|
|
308
|
-
return @net_http_handler if @net_http_handler
|
|
376
|
+
def transmit(config, req, resp)
|
|
377
|
+
total_timeout = config.async_http_total_timeout
|
|
378
|
+
return transmit_inner(config, req, resp) unless total_timeout && total_timeout > 0
|
|
309
379
|
|
|
310
|
-
|
|
311
|
-
|
|
380
|
+
# Raised as a copy of this instance, which carries the scope along.
|
|
381
|
+
expired = TransportTimeoutError.new
|
|
382
|
+
expired.scope = scope = Object.new
|
|
383
|
+
Async::Task.current.with_timeout(total_timeout, expired) do |deadline|
|
|
384
|
+
transmit_inner(config, req, resp, deadline:, scope:)
|
|
312
385
|
end
|
|
313
386
|
end
|
|
314
387
|
|
|
315
|
-
def
|
|
316
|
-
|
|
317
|
-
if total_timeout && total_timeout > 0
|
|
318
|
-
Async::Task.current.with_timeout(total_timeout, Async::TimeoutError) do
|
|
319
|
-
transmit_inner(config, req, resp)
|
|
320
|
-
end
|
|
321
|
-
else
|
|
322
|
-
transmit_inner(config, req, resp)
|
|
323
|
-
end
|
|
388
|
+
def client_cache(config)
|
|
389
|
+
config.async_http_client_cache || @client_cache || ClientCache.default
|
|
324
390
|
end
|
|
325
391
|
|
|
326
|
-
def transmit_inner(config, req, resp)
|
|
327
|
-
cache = config
|
|
392
|
+
def transmit_inner(config, req, resp, deadline: nil, scope: nil)
|
|
393
|
+
cache = client_cache(config)
|
|
394
|
+
# Set once the outcome of the request has been signalled to Seahorse.
|
|
395
|
+
settled = false
|
|
396
|
+
# Set while the connection is used: what fails then is not the caller's code.
|
|
397
|
+
on_wire = false
|
|
328
398
|
runner = ->(client) do
|
|
329
399
|
request = build_request(req, config)
|
|
330
400
|
response = nil
|
|
331
401
|
|
|
332
402
|
begin
|
|
403
|
+
on_wire = true
|
|
333
404
|
response = call_with_timeout(client, request, config)
|
|
405
|
+
on_wire = false
|
|
334
406
|
bytes_received = 0
|
|
335
407
|
|
|
336
408
|
headers = response_headers(response)
|
|
337
409
|
resp.signal_headers(response.status.to_i, headers)
|
|
338
410
|
|
|
339
411
|
loop do
|
|
412
|
+
on_wire = true
|
|
340
413
|
chunk = read_with_timeout(response, config)
|
|
414
|
+
on_wire = false
|
|
341
415
|
break if chunk.nil? || chunk.empty?
|
|
342
416
|
|
|
343
417
|
bytes_received += chunk.bytesize
|
|
344
418
|
resp.signal_data(chunk)
|
|
345
419
|
end
|
|
346
420
|
|
|
421
|
+
# The response is complete: the total deadline must not interrupt
|
|
422
|
+
# the listeners Seahorse runs now, nor turn their result into a
|
|
423
|
+
# retry. The same holds for the listeners of an error, below.
|
|
424
|
+
deadline&.cancel!
|
|
347
425
|
complete_response(req, resp, bytes_received, headers)
|
|
426
|
+
settled = true
|
|
348
427
|
rescue Async::Stop
|
|
349
428
|
raise
|
|
350
|
-
rescue
|
|
429
|
+
rescue Async::TimeoutError => error
|
|
430
|
+
# Not one of this transport's deadlines: the caller's own
|
|
431
|
+
# `with_timeout` around the call. It is not a network error and
|
|
432
|
+
# must not be retried.
|
|
433
|
+
raise unless transport_timeout?(error, scope)
|
|
434
|
+
|
|
435
|
+
settled = true
|
|
436
|
+
deadline&.cancel!
|
|
351
437
|
resp.signal_error(networking_error(error, req))
|
|
352
|
-
rescue SystemCallError => error
|
|
438
|
+
rescue *NETWORK_ERRORS, SystemCallError => error
|
|
439
|
+
settled = true
|
|
440
|
+
deadline&.cancel!
|
|
353
441
|
resp.signal_error(networking_error(error, req))
|
|
354
442
|
rescue StandardError => error
|
|
355
|
-
|
|
356
|
-
|
|
443
|
+
settled = true
|
|
444
|
+
deadline&.cancel!
|
|
445
|
+
if on_wire && cache.respond_to?(:force_closed?) && cache.force_closed?(client)
|
|
446
|
+
# The connection was closed under the request (`clear!(timeout:)`),
|
|
447
|
+
# which protocol-http1 reports as whatever its torn-down state raises.
|
|
448
|
+
resp.signal_error(networking_error(error, req))
|
|
449
|
+
else
|
|
450
|
+
logger_for(config)&.error("[aws-sdk-http-async] unexpected error: #{error.class}: #{error.message}")
|
|
451
|
+
resp.signal_error(error)
|
|
452
|
+
end
|
|
357
453
|
ensure
|
|
358
|
-
|
|
454
|
+
# The request is over, whichever way it ends. The total deadline
|
|
455
|
+
# does not cover the cleanup: giving up the turn switches to the
|
|
456
|
+
# call that waits for it, and a deadline that came due in that pass
|
|
457
|
+
# of the event loop would replace a stop or a timeout of the caller
|
|
458
|
+
# on its way out and be reported as a network error, which the SDK
|
|
459
|
+
# retries.
|
|
460
|
+
deadline&.cancel!
|
|
461
|
+
close_response(response)
|
|
359
462
|
end
|
|
360
463
|
end
|
|
361
464
|
|
|
@@ -364,6 +467,39 @@ module Async
|
|
|
364
467
|
else
|
|
365
468
|
runner.call(cache.client_for(req.endpoint, config))
|
|
366
469
|
end
|
|
470
|
+
rescue TransportTimeoutError => error
|
|
471
|
+
raise unless transport_timeout?(error, scope)
|
|
472
|
+
|
|
473
|
+
# Timed out while waiting its turn: a network error like a timeout
|
|
474
|
+
# during the request. Once the outcome is signalled, a timeout can only
|
|
475
|
+
# have hit the cleanup of a finished request, and signalling an error
|
|
476
|
+
# then would make the SDK retry an operation that succeeded.
|
|
477
|
+
return if settled
|
|
478
|
+
|
|
479
|
+
# The wait for a turn has its own timeout; the total deadline must not
|
|
480
|
+
# cut off the listeners of the error either.
|
|
481
|
+
deadline&.cancel!
|
|
482
|
+
resp.signal_error(networking_error(error, req))
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
# The outcome is signalled by now. After `clear!(timeout:)` closed the
|
|
486
|
+
# socket under the request, closing a partly read body raises; that must
|
|
487
|
+
# not replace the error that was signalled.
|
|
488
|
+
def close_response(response)
|
|
489
|
+
response&.close
|
|
490
|
+
rescue IOError, SystemCallError
|
|
491
|
+
nil
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# Whether `error` is a deadline of this invocation. The total deadline of
|
|
495
|
+
# an outer call (one whose streaming block makes this call) is not: like a
|
|
496
|
+
# timeout the caller put around the call, it passes through. WebMock's
|
|
497
|
+
# `to_timeout` raises a plain `Async::TimeoutError` from inside the
|
|
498
|
+
# stubbed client; it stands for a network timeout.
|
|
499
|
+
def transport_timeout?(error, scope)
|
|
500
|
+
return error.message == 'WebMock timeout error' unless error.is_a?(TransportTimeoutError)
|
|
501
|
+
|
|
502
|
+
error.scope.nil? || error.scope.equal?(scope)
|
|
367
503
|
end
|
|
368
504
|
|
|
369
505
|
def build_request(http_request, config)
|
|
@@ -552,7 +688,7 @@ module Async
|
|
|
552
688
|
return nil if body.nil?
|
|
553
689
|
return read_response_chunk(body) if timeout.nil? || timeout <= 0
|
|
554
690
|
|
|
555
|
-
Async::Task.current.with_timeout(timeout,
|
|
691
|
+
Async::Task.current.with_timeout(timeout, TransportTimeoutError) { read_response_chunk(body) }
|
|
556
692
|
end
|
|
557
693
|
|
|
558
694
|
def read_response_chunk(body)
|
|
@@ -572,7 +708,7 @@ module Async
|
|
|
572
708
|
def call_with_timeout(client, request, config)
|
|
573
709
|
header_timeout = config.async_http_header_timeout
|
|
574
710
|
if header_timeout && header_timeout > 0
|
|
575
|
-
return Async::Task.current.with_timeout(header_timeout,
|
|
711
|
+
return Async::Task.current.with_timeout(header_timeout, TransportTimeoutError) { client.call(request) }
|
|
576
712
|
end
|
|
577
713
|
|
|
578
714
|
timeout = config.http_read_timeout
|
|
@@ -581,7 +717,7 @@ module Async
|
|
|
581
717
|
body = request.body
|
|
582
718
|
return client.call(request) unless body.nil? || body.is_a?(String) || body.is_a?(Protocol::HTTP::Body::Buffered)
|
|
583
719
|
|
|
584
|
-
Async::Task.current.with_timeout(timeout,
|
|
720
|
+
Async::Task.current.with_timeout(timeout, TransportTimeoutError) { client.call(request) }
|
|
585
721
|
end
|
|
586
722
|
|
|
587
723
|
def complete_response(req, resp, bytes_received, headers)
|
|
@@ -8,7 +8,7 @@ module Async
|
|
|
8
8
|
def add_options(config)
|
|
9
9
|
Seahorse::Client::Plugins::NetHttp.new.add_options(config)
|
|
10
10
|
|
|
11
|
-
config.add_option(:async_http_connection_limit,
|
|
11
|
+
config.add_option(:async_http_connection_limit, 50)
|
|
12
12
|
config.add_option(:async_http_force_accept_encoding, true)
|
|
13
13
|
config.add_option(:async_http_body_warn_bytes, 5 * 1024 * 1024)
|
|
14
14
|
config.add_option(:async_http_max_buffer_bytes, 5 * 1024 * 1024)
|