aws-sdk-core 3.54.2 → 3.126.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1247 -0
  3. data/LICENSE.txt +202 -0
  4. data/VERSION +1 -1
  5. data/lib/aws-defaults/default_configuration.rb +153 -0
  6. data/lib/aws-defaults/defaults_mode_config_resolver.rb +107 -0
  7. data/lib/aws-defaults.rb +3 -0
  8. data/lib/aws-sdk-core/arn.rb +92 -0
  9. data/lib/aws-sdk-core/arn_parser.rb +40 -0
  10. data/lib/aws-sdk-core/assume_role_credentials.rb +2 -0
  11. data/lib/aws-sdk-core/assume_role_web_identity_credentials.rb +104 -0
  12. data/lib/aws-sdk-core/async_client_stubs.rb +4 -2
  13. data/lib/aws-sdk-core/binary/decode_handler.rb +2 -0
  14. data/lib/aws-sdk-core/binary/encode_handler.rb +2 -0
  15. data/lib/aws-sdk-core/binary/event_builder.rb +8 -6
  16. data/lib/aws-sdk-core/binary/event_parser.rb +5 -3
  17. data/lib/aws-sdk-core/binary/event_stream_decoder.rb +2 -0
  18. data/lib/aws-sdk-core/binary/event_stream_encoder.rb +2 -0
  19. data/lib/aws-sdk-core/binary.rb +2 -0
  20. data/lib/aws-sdk-core/client_side_monitoring/publisher.rb +11 -1
  21. data/lib/aws-sdk-core/client_side_monitoring/request_metrics.rb +2 -0
  22. data/lib/aws-sdk-core/client_stubs.rb +15 -12
  23. data/lib/aws-sdk-core/credential_provider.rb +1 -30
  24. data/lib/aws-sdk-core/credential_provider_chain.rb +102 -40
  25. data/lib/aws-sdk-core/credentials.rb +2 -0
  26. data/lib/aws-sdk-core/deprecations.rb +17 -11
  27. data/lib/aws-sdk-core/eager_loader.rb +2 -0
  28. data/lib/aws-sdk-core/ec2_metadata.rb +237 -0
  29. data/lib/aws-sdk-core/ecs_credentials.rb +5 -4
  30. data/lib/aws-sdk-core/endpoint_cache.rb +16 -11
  31. data/lib/aws-sdk-core/errors.rb +102 -15
  32. data/lib/aws-sdk-core/event_emitter.rb +2 -0
  33. data/lib/aws-sdk-core/ini_parser.rb +2 -0
  34. data/lib/aws-sdk-core/instance_profile_credentials.rb +153 -39
  35. data/lib/aws-sdk-core/json/builder.rb +2 -0
  36. data/lib/aws-sdk-core/json/error_handler.rb +2 -0
  37. data/lib/aws-sdk-core/json/handler.rb +2 -0
  38. data/lib/aws-sdk-core/json/json_engine.rb +12 -8
  39. data/lib/aws-sdk-core/json/oj_engine.rb +35 -6
  40. data/lib/aws-sdk-core/json/parser.rb +10 -0
  41. data/lib/aws-sdk-core/json.rb +11 -28
  42. data/lib/aws-sdk-core/log/formatter.rb +16 -4
  43. data/lib/aws-sdk-core/log/handler.rb +2 -0
  44. data/lib/aws-sdk-core/log/param_filter.rb +38 -13
  45. data/lib/aws-sdk-core/log/param_formatter.rb +2 -0
  46. data/lib/aws-sdk-core/pageable_response.rb +48 -24
  47. data/lib/aws-sdk-core/pager.rb +5 -0
  48. data/lib/aws-sdk-core/param_converter.rb +2 -0
  49. data/lib/aws-sdk-core/param_validator.rb +55 -7
  50. data/lib/aws-sdk-core/plugins/api_key.rb +5 -1
  51. data/lib/aws-sdk-core/plugins/apig_authorizer_token.rb +2 -0
  52. data/lib/aws-sdk-core/plugins/apig_credentials_configuration.rb +2 -0
  53. data/lib/aws-sdk-core/plugins/apig_user_agent.rb +2 -0
  54. data/lib/aws-sdk-core/plugins/client_metrics_plugin.rb +28 -1
  55. data/lib/aws-sdk-core/plugins/client_metrics_send_plugin.rb +2 -0
  56. data/lib/aws-sdk-core/plugins/credentials_configuration.rb +26 -7
  57. data/lib/aws-sdk-core/plugins/defaults_mode.rb +40 -0
  58. data/lib/aws-sdk-core/plugins/endpoint_discovery.rb +12 -4
  59. data/lib/aws-sdk-core/plugins/endpoint_pattern.rb +8 -6
  60. data/lib/aws-sdk-core/plugins/event_stream_configuration.rb +2 -0
  61. data/lib/aws-sdk-core/plugins/global_configuration.rb +2 -0
  62. data/lib/aws-sdk-core/plugins/helpful_socket_errors.rb +2 -0
  63. data/lib/aws-sdk-core/plugins/http_checksum.rb +57 -0
  64. data/lib/aws-sdk-core/plugins/idempotency_token.rb +2 -0
  65. data/lib/aws-sdk-core/plugins/invocation_id.rb +2 -0
  66. data/lib/aws-sdk-core/plugins/jsonvalue_converter.rb +2 -0
  67. data/lib/aws-sdk-core/plugins/logging.rb +2 -0
  68. data/lib/aws-sdk-core/plugins/param_converter.rb +2 -0
  69. data/lib/aws-sdk-core/plugins/param_validator.rb +2 -0
  70. data/lib/aws-sdk-core/plugins/protocols/api_gateway.rb +19 -0
  71. data/lib/aws-sdk-core/plugins/protocols/ec2.rb +2 -0
  72. data/lib/aws-sdk-core/plugins/protocols/json_rpc.rb +2 -0
  73. data/lib/aws-sdk-core/plugins/protocols/query.rb +2 -0
  74. data/lib/aws-sdk-core/plugins/protocols/rest_json.rb +18 -1
  75. data/lib/aws-sdk-core/plugins/protocols/rest_xml.rb +2 -0
  76. data/lib/aws-sdk-core/plugins/recursion_detection.rb +27 -0
  77. data/lib/aws-sdk-core/plugins/regional_endpoint.rb +74 -16
  78. data/lib/aws-sdk-core/plugins/response_paging.rb +2 -0
  79. data/lib/aws-sdk-core/plugins/retries/client_rate_limiter.rb +139 -0
  80. data/lib/aws-sdk-core/plugins/retries/clock_skew.rb +100 -0
  81. data/lib/aws-sdk-core/plugins/retries/error_inspector.rb +146 -0
  82. data/lib/aws-sdk-core/plugins/retries/retry_quota.rb +59 -0
  83. data/lib/aws-sdk-core/plugins/retry_errors.rb +295 -108
  84. data/lib/aws-sdk-core/plugins/signature_v2.rb +2 -0
  85. data/lib/aws-sdk-core/plugins/signature_v4.rb +28 -25
  86. data/lib/aws-sdk-core/plugins/stub_responses.rb +5 -0
  87. data/lib/aws-sdk-core/plugins/transfer_encoding.rb +4 -4
  88. data/lib/aws-sdk-core/plugins/user_agent.rb +6 -8
  89. data/lib/aws-sdk-core/process_credentials.rb +8 -7
  90. data/lib/aws-sdk-core/query/ec2_param_builder.rb +2 -0
  91. data/lib/aws-sdk-core/query/handler.rb +2 -0
  92. data/lib/aws-sdk-core/query/param.rb +2 -0
  93. data/lib/aws-sdk-core/query/param_builder.rb +2 -0
  94. data/lib/aws-sdk-core/query/param_list.rb +2 -0
  95. data/lib/aws-sdk-core/query.rb +2 -0
  96. data/lib/aws-sdk-core/refreshing_credentials.rb +2 -0
  97. data/lib/aws-sdk-core/resources/collection.rb +2 -0
  98. data/lib/aws-sdk-core/rest/handler.rb +2 -0
  99. data/lib/aws-sdk-core/rest/request/body.rb +21 -1
  100. data/lib/aws-sdk-core/rest/request/builder.rb +2 -0
  101. data/lib/aws-sdk-core/rest/request/endpoint.rb +10 -3
  102. data/lib/aws-sdk-core/rest/request/headers.rb +20 -6
  103. data/lib/aws-sdk-core/rest/request/querystring_builder.rb +4 -2
  104. data/lib/aws-sdk-core/rest/response/body.rb +2 -0
  105. data/lib/aws-sdk-core/rest/response/headers.rb +6 -3
  106. data/lib/aws-sdk-core/rest/response/parser.rb +2 -0
  107. data/lib/aws-sdk-core/rest/response/status_code.rb +2 -0
  108. data/lib/aws-sdk-core/rest.rb +2 -0
  109. data/lib/aws-sdk-core/shared_config.rb +153 -122
  110. data/lib/aws-sdk-core/shared_credentials.rb +9 -1
  111. data/lib/aws-sdk-core/sso_credentials.rb +131 -0
  112. data/lib/aws-sdk-core/structure.rb +13 -2
  113. data/lib/aws-sdk-core/stubbing/data_applicator.rb +2 -0
  114. data/lib/aws-sdk-core/stubbing/empty_stub.rb +2 -0
  115. data/lib/aws-sdk-core/stubbing/protocols/api_gateway.rb +2 -0
  116. data/lib/aws-sdk-core/stubbing/protocols/ec2.rb +2 -0
  117. data/lib/aws-sdk-core/stubbing/protocols/json.rb +3 -1
  118. data/lib/aws-sdk-core/stubbing/protocols/query.rb +4 -2
  119. data/lib/aws-sdk-core/stubbing/protocols/rest.rb +33 -7
  120. data/lib/aws-sdk-core/stubbing/protocols/rest_json.rb +3 -1
  121. data/lib/aws-sdk-core/stubbing/protocols/rest_xml.rb +2 -2
  122. data/lib/aws-sdk-core/stubbing/stub_data.rb +2 -0
  123. data/lib/aws-sdk-core/stubbing/xml_error.rb +2 -0
  124. data/lib/aws-sdk-core/type_builder.rb +2 -0
  125. data/lib/aws-sdk-core/util.rb +6 -0
  126. data/lib/aws-sdk-core/waiters/errors.rb +2 -0
  127. data/lib/aws-sdk-core/waiters/poller.rb +2 -0
  128. data/lib/aws-sdk-core/waiters/waiter.rb +2 -0
  129. data/lib/aws-sdk-core/waiters.rb +2 -0
  130. data/lib/aws-sdk-core/xml/builder.rb +5 -3
  131. data/lib/aws-sdk-core/xml/default_list.rb +2 -0
  132. data/lib/aws-sdk-core/xml/default_map.rb +2 -0
  133. data/lib/aws-sdk-core/xml/doc_builder.rb +15 -4
  134. data/lib/aws-sdk-core/xml/error_handler.rb +3 -1
  135. data/lib/aws-sdk-core/xml/parser/engines/libxml.rb +2 -0
  136. data/lib/aws-sdk-core/xml/parser/engines/nokogiri.rb +2 -0
  137. data/lib/aws-sdk-core/xml/parser/engines/oga.rb +2 -0
  138. data/lib/aws-sdk-core/xml/parser/engines/ox.rb +3 -1
  139. data/lib/aws-sdk-core/xml/parser/engines/rexml.rb +4 -1
  140. data/lib/aws-sdk-core/xml/parser/frame.rb +25 -0
  141. data/lib/aws-sdk-core/xml/parser/parsing_error.rb +2 -0
  142. data/lib/aws-sdk-core/xml/parser/stack.rb +2 -0
  143. data/lib/aws-sdk-core/xml/parser.rb +7 -0
  144. data/lib/aws-sdk-core/xml.rb +2 -0
  145. data/lib/aws-sdk-core.rb +22 -4
  146. data/lib/aws-sdk-sso/client.rb +568 -0
  147. data/lib/aws-sdk-sso/client_api.rb +190 -0
  148. data/lib/aws-sdk-sso/customizations.rb +1 -0
  149. data/lib/aws-sdk-sso/errors.rb +102 -0
  150. data/lib/aws-sdk-sso/resource.rb +26 -0
  151. data/lib/aws-sdk-sso/types.rb +352 -0
  152. data/lib/aws-sdk-sso.rb +55 -0
  153. data/lib/aws-sdk-sts/client.rb +970 -414
  154. data/lib/aws-sdk-sts/client_api.rb +41 -1
  155. data/lib/aws-sdk-sts/customizations.rb +4 -0
  156. data/lib/aws-sdk-sts/errors.rb +33 -9
  157. data/lib/aws-sdk-sts/plugins/sts_regional_endpoints.rb +38 -0
  158. data/lib/aws-sdk-sts/presigner.rb +75 -0
  159. data/lib/aws-sdk-sts/resource.rb +4 -1
  160. data/lib/aws-sdk-sts/types.rb +632 -236
  161. data/lib/aws-sdk-sts.rb +16 -6
  162. data/lib/seahorse/client/async_base.rb +2 -0
  163. data/lib/seahorse/client/async_response.rb +2 -0
  164. data/lib/seahorse/client/base.rb +6 -1
  165. data/lib/seahorse/client/block_io.rb +6 -2
  166. data/lib/seahorse/client/configuration.rb +6 -0
  167. data/lib/seahorse/client/events.rb +2 -0
  168. data/lib/seahorse/client/h2/connection.rb +31 -25
  169. data/lib/seahorse/client/h2/handler.rb +6 -5
  170. data/lib/seahorse/client/handler.rb +2 -0
  171. data/lib/seahorse/client/handler_builder.rb +2 -0
  172. data/lib/seahorse/client/handler_list.rb +2 -0
  173. data/lib/seahorse/client/handler_list_entry.rb +6 -4
  174. data/lib/seahorse/client/http/async_response.rb +2 -0
  175. data/lib/seahorse/client/http/headers.rb +2 -0
  176. data/lib/seahorse/client/http/request.rb +5 -3
  177. data/lib/seahorse/client/http/response.rb +5 -3
  178. data/lib/seahorse/client/logging/formatter.rb +6 -2
  179. data/lib/seahorse/client/logging/handler.rb +2 -0
  180. data/lib/seahorse/client/managed_file.rb +2 -0
  181. data/lib/seahorse/client/net_http/connection_pool.rb +30 -23
  182. data/lib/seahorse/client/net_http/handler.rb +24 -7
  183. data/lib/seahorse/client/net_http/patches.rb +15 -84
  184. data/lib/seahorse/client/networking_error.rb +2 -0
  185. data/lib/seahorse/client/plugin.rb +9 -6
  186. data/lib/seahorse/client/plugin_list.rb +2 -0
  187. data/lib/seahorse/client/plugins/content_length.rb +13 -7
  188. data/lib/seahorse/client/plugins/endpoint.rb +4 -2
  189. data/lib/seahorse/client/plugins/h2.rb +6 -1
  190. data/lib/seahorse/client/plugins/logging.rb +2 -0
  191. data/lib/seahorse/client/plugins/net_http.rb +39 -3
  192. data/lib/seahorse/client/plugins/operation_methods.rb +2 -0
  193. data/lib/seahorse/client/plugins/raise_response_errors.rb +2 -0
  194. data/lib/seahorse/client/plugins/request_callback.rb +110 -0
  195. data/lib/seahorse/client/plugins/response_target.rb +23 -14
  196. data/lib/seahorse/client/request.rb +2 -0
  197. data/lib/seahorse/client/request_context.rb +2 -0
  198. data/lib/seahorse/client/response.rb +5 -5
  199. data/lib/seahorse/model/api.rb +6 -0
  200. data/lib/seahorse/model/authorizer.rb +2 -0
  201. data/lib/seahorse/model/operation.rb +5 -0
  202. data/lib/seahorse/model/shapes.rb +27 -0
  203. data/lib/seahorse/util.rb +8 -1
  204. data/lib/seahorse/version.rb +2 -0
  205. data/lib/seahorse.rb +3 -0
  206. metadata +43 -11
@@ -1,12 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module Plugins
3
5
  module Protocols
4
6
  class RestJson < Seahorse::Client::Plugin
5
7
 
8
+ class ContentTypeHandler < Seahorse::Client::Handler
9
+ def call(context)
10
+ body = context.http_request.body
11
+ # Rest::Handler will set a default JSON body, so size can be checked
12
+ # if this handler is run after serialization.
13
+ if !body.respond_to?(:size) ||
14
+ (body.respond_to?(:size) && body.size > 0)
15
+ context.http_request.headers['Content-Type'] ||=
16
+ 'application/json'
17
+ end
18
+ @handler.call(context)
19
+ end
20
+ end
21
+
6
22
  handler(Rest::Handler)
23
+ handler(ContentTypeHandler, priority: 30)
7
24
  handler(Json::ErrorHandler, step: :sign)
8
-
9
25
  end
26
+
10
27
  end
11
28
  end
12
29
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module Plugins
3
5
  module Protocols
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module Plugins
5
+ # @api private
6
+ class RecursionDetection < Seahorse::Client::Plugin
7
+
8
+ # @api private
9
+ class Handler < Seahorse::Client::Handler
10
+ def call(context)
11
+
12
+ unless context.http_request.headers.key?('x-amz-trace-id')
13
+ if ENV['AWS_LAMBDA_FUNCTION_NAME'] &&
14
+ (trace_id = ENV['_X_AMZ_TRACE_ID'])
15
+ context.http_request.headers['x-amz-trace-id'] = trace_id
16
+ end
17
+ end
18
+ @handler.call(context)
19
+ end
20
+ end
21
+
22
+ # should be at the end of build so that
23
+ # modeled traits / service customizations apply first
24
+ handler(Handler, step: :build, order: 99)
25
+ end
26
+ end
27
+ end
@@ -1,11 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module Plugins
3
5
  # @api private
4
6
  class RegionalEndpoint < Seahorse::Client::Plugin
5
-
6
- # raised when region is not configured
7
- MISSING_REGION = 'missing required configuration option :region'
8
-
9
7
  option(:profile)
10
8
 
11
9
  option(:region,
@@ -14,7 +12,7 @@ module Aws
14
12
  docstring: <<-DOCS) do |cfg|
15
13
  The AWS region to connect to. The configured `:region` is
16
14
  used to determine the service `:endpoint`. When not passed,
17
- a default `:region` is search for in the following locations:
15
+ a default `:region` is searched for in the following locations:
18
16
 
19
17
  * `Aws.config[:region]`
20
18
  * `ENV['AWS_REGION']`
@@ -26,35 +24,95 @@ a default `:region` is search for in the following locations:
26
24
  resolve_region(cfg)
27
25
  end
28
26
 
27
+ option(:use_dualstack_endpoint,
28
+ doc_type: 'Boolean',
29
+ docstring: <<-DOCS) do |cfg|
30
+ When set to `true`, dualstack enabled endpoints (with `.aws` TLD)
31
+ will be used if available.
32
+ DOCS
33
+ resolve_use_dualstack_endpoint(cfg)
34
+ end
35
+
36
+ option(:use_fips_endpoint,
37
+ doc_type: 'Boolean',
38
+ docstring: <<-DOCS) do |cfg|
39
+ When set to `true`, fips compatible endpoints will be used if available.
40
+ When a `fips` region is used, the region is normalized and this config
41
+ is set to `true`.
42
+ DOCS
43
+ resolve_use_fips_endpoint(cfg)
44
+ end
45
+
29
46
  option(:regional_endpoint, false)
30
47
 
31
48
  option(:endpoint, doc_type: String, docstring: <<-DOCS) do |cfg|
32
49
  The client endpoint is normally constructed from the `:region`
33
50
  option. You should only configure an `:endpoint` when connecting
34
- to test endpoints. This should be avalid HTTP(S) URI.
51
+ to test or custom endpoints. This should be a valid HTTP(S) URI.
35
52
  DOCS
36
53
  endpoint_prefix = cfg.api.metadata['endpointPrefix']
37
54
  if cfg.region && endpoint_prefix
38
- Aws::Partitions::EndpointProvider.resolve(cfg.region, endpoint_prefix)
55
+ if cfg.respond_to?(:sts_regional_endpoints)
56
+ sts_regional = cfg.sts_regional_endpoints
57
+ end
58
+
59
+ # check region is a valid RFC host label
60
+ unless Seahorse::Util.host_label?(cfg.region)
61
+ raise Errors::InvalidRegionError
62
+ end
63
+
64
+ region = cfg.region
65
+ new_region = region.gsub('fips-', '').gsub('-fips', '')
66
+ if region != new_region
67
+ warn("Legacy region #{region} was transformed to #{new_region}."\
68
+ '`use_fips_endpoint` config was set to true.')
69
+ cfg.override_config(:use_fips_endpoint, true)
70
+ cfg.override_config(:region, new_region)
71
+ end
72
+
73
+ Aws::Partitions::EndpointProvider.resolve(
74
+ cfg.region,
75
+ endpoint_prefix,
76
+ sts_regional,
77
+ {
78
+ dualstack: cfg.use_dualstack_endpoint,
79
+ fips: cfg.use_fips_endpoint
80
+ }
81
+ )
39
82
  end
40
83
  end
41
84
 
42
85
  def after_initialize(client)
43
- if client.config.region.nil? or client.config.region == ''
86
+ if client.config.region.nil? || client.config.region == ''
44
87
  raise Errors::MissingRegionError
45
88
  end
46
89
  end
47
90
 
48
- private
91
+ class << self
92
+ private
49
93
 
50
- def self.resolve_region(cfg)
51
- keys = %w(AWS_REGION AMAZON_REGION AWS_DEFAULT_REGION)
52
- env_region = ENV.values_at(*keys).compact.first
53
- env_region = nil if env_region == ''
54
- cfg_region = Aws.shared_config.region(profile: cfg.profile)
55
- env_region || cfg_region
56
- end
94
+ def resolve_region(cfg)
95
+ keys = %w[AWS_REGION AMAZON_REGION AWS_DEFAULT_REGION]
96
+ env_region = ENV.values_at(*keys).compact.first
97
+ env_region = nil if env_region == ''
98
+ cfg_region = Aws.shared_config.region(profile: cfg.profile)
99
+ env_region || cfg_region
100
+ end
101
+
102
+ def resolve_use_dualstack_endpoint(cfg)
103
+ value = ENV['AWS_USE_DUALSTACK_ENDPOINT']
104
+ value ||= Aws.shared_config.use_dualstack_endpoint(
105
+ profile: cfg.profile
106
+ )
107
+ Aws::Util.str_2_bool(value) || false
108
+ end
57
109
 
110
+ def resolve_use_fips_endpoint(cfg)
111
+ value = ENV['AWS_USE_FIPS_ENDPOINT']
112
+ value ||= Aws.shared_config.use_fips_endpoint(profile: cfg.profile)
113
+ Aws::Util.str_2_bool(value) || false
114
+ end
115
+ end
58
116
  end
59
117
  end
60
118
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module Plugins
3
5
  # @api private
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module Plugins
5
+ module Retries
6
+ # @api private
7
+ # Used only in 'adaptive' retry mode
8
+ class ClientRateLimiter
9
+ MIN_CAPACITY = 1
10
+ MIN_FILL_RATE = 0.5
11
+ SMOOTH = 0.8
12
+ # How much to scale back after a throttling response
13
+ BETA = 0.7
14
+ # Controls how aggressively we scale up after being throttled
15
+ SCALE_CONSTANT = 0.4
16
+
17
+ def initialize
18
+ @mutex = Mutex.new
19
+ @fill_rate = nil
20
+ @max_capacity = nil
21
+ @current_capacity = 0
22
+ @last_timestamp = nil
23
+ @enabled = false
24
+ @measured_tx_rate = 0
25
+ @last_tx_rate_bucket = Aws::Util.monotonic_seconds
26
+ @request_count = 0
27
+ @last_max_rate = 0
28
+ @last_throttle_time = Aws::Util.monotonic_seconds
29
+ @calculated_rate = nil
30
+ end
31
+
32
+ def token_bucket_acquire(amount, wait_to_fill = true)
33
+ # Client side throttling is not enabled until we see a
34
+ # throttling error
35
+ return unless @enabled
36
+
37
+ @mutex.synchronize do
38
+ token_bucket_refill
39
+
40
+ # Next see if we have enough capacity for the requested amount
41
+ while @current_capacity < amount
42
+ raise Aws::Errors::RetryCapacityNotAvailableError unless wait_to_fill
43
+ @mutex.sleep((amount - @current_capacity) / @fill_rate)
44
+ token_bucket_refill
45
+ end
46
+ @current_capacity -= amount
47
+ end
48
+ end
49
+
50
+ def update_sending_rate(is_throttling_error)
51
+ @mutex.synchronize do
52
+ update_measured_rate
53
+
54
+ if is_throttling_error
55
+ rate_to_use = if @enabled
56
+ [@measured_tx_rate, @fill_rate].min
57
+ else
58
+ @measured_tx_rate
59
+ end
60
+
61
+ # The fill_rate is from the token bucket
62
+ @last_max_rate = rate_to_use
63
+ calculate_time_window
64
+ @last_throttle_time = Aws::Util.monotonic_seconds
65
+ @calculated_rate = cubic_throttle(rate_to_use)
66
+ enable_token_bucket
67
+ else
68
+ calculate_time_window
69
+ @calculated_rate = cubic_success(Aws::Util.monotonic_seconds)
70
+ end
71
+
72
+ new_rate = [@calculated_rate, 2 * @measured_tx_rate].min
73
+ token_bucket_update_rate(new_rate)
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def token_bucket_refill
80
+ timestamp = Aws::Util.monotonic_seconds
81
+ unless @last_timestamp
82
+ @last_timestamp = timestamp
83
+ return
84
+ end
85
+
86
+ fill_amount = (timestamp - @last_timestamp) * @fill_rate
87
+ @current_capacity = [
88
+ @max_capacity, @current_capacity + fill_amount
89
+ ].min
90
+
91
+ @last_timestamp = timestamp
92
+ end
93
+
94
+ def token_bucket_update_rate(new_rps)
95
+ # Refill based on our current rate before we update to the
96
+ # new fill rate
97
+ token_bucket_refill
98
+ @fill_rate = [new_rps, MIN_FILL_RATE].max
99
+ @max_capacity = [new_rps, MIN_CAPACITY].max
100
+ # When we scale down we can't have a current capacity that exceeds our
101
+ # max_capacity.
102
+ @current_capacity = [@current_capacity, @max_capacity].min
103
+ end
104
+
105
+ def enable_token_bucket
106
+ @enabled = true
107
+ end
108
+
109
+ def update_measured_rate
110
+ t = Aws::Util.monotonic_seconds
111
+ time_bucket = (t * 2).floor / 2.0
112
+ @request_count += 1
113
+ if time_bucket > @last_tx_rate_bucket
114
+ current_rate = @request_count / (time_bucket - @last_tx_rate_bucket)
115
+ @measured_tx_rate = (current_rate * SMOOTH) +
116
+ (@measured_tx_rate * (1 - SMOOTH))
117
+ @request_count = 0
118
+ @last_tx_rate_bucket = time_bucket
119
+ end
120
+ end
121
+
122
+ def calculate_time_window
123
+ # This is broken out into a separate calculation because it only
124
+ # gets updated when @last_max_rate changes so it can be cached.
125
+ @time_window = ((@last_max_rate * (1 - BETA)) / SCALE_CONSTANT)**(1.0 / 3)
126
+ end
127
+
128
+ def cubic_success(timestamp)
129
+ dt = timestamp - @last_throttle_time
130
+ (SCALE_CONSTANT * ((dt - @time_window)**3)) + @last_max_rate
131
+ end
132
+
133
+ def cubic_throttle(rate_to_use)
134
+ rate_to_use * BETA
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module Plugins
5
+ module Retries
6
+
7
+ # @api private
8
+ class ClockSkew
9
+
10
+ CLOCK_SKEW_THRESHOLD = 5 * 60 # five minutes
11
+
12
+ def initialize
13
+ @mutex = Mutex.new
14
+ # clock_corrections are recorded only on errors
15
+ # and only when time difference is greater than the
16
+ # CLOCK_SKEW_THRESHOLD
17
+ @endpoint_clock_corrections = Hash.new(0)
18
+
19
+ # estimated_skew is calculated on every request
20
+ # and is used to estimate a TTL for requests
21
+ @endpoint_estimated_skews = Hash.new(nil)
22
+ end
23
+
24
+ # Gets the clock_correction in seconds to apply to a given endpoint
25
+ # @param endpoint [URI / String]
26
+ def clock_correction(endpoint)
27
+ @mutex.synchronize { @endpoint_clock_corrections[endpoint.to_s] }
28
+ end
29
+
30
+ # The estimated skew factors in any clock skew from
31
+ # the service along with any network latency.
32
+ # This provides a more accurate value for the ttl,
33
+ # which should represent when the client will stop
34
+ # waiting for a request.
35
+ # Estimated Skew should not be used to correct clock skew errors
36
+ # it should only be used to estimate TTL for a request
37
+ def estimated_skew(endpoint)
38
+ @mutex.synchronize { @endpoint_estimated_skews[endpoint.to_s] }
39
+ end
40
+
41
+ # Determines whether a request has clock skew by comparing
42
+ # the current time against the server's time in the response
43
+ # @param context [Seahorse::Client::RequestContext]
44
+ def clock_skewed?(context)
45
+ server_time = server_time(context.http_response)
46
+ !!server_time &&
47
+ (Time.now.utc - server_time).abs > CLOCK_SKEW_THRESHOLD
48
+ end
49
+
50
+ # Called only on clock skew related errors
51
+ # Update the stored clock skew correction value for an endpoint
52
+ # from the server's time in the response
53
+ # @param context [Seahorse::Client::RequestContext]
54
+ def update_clock_correction(context)
55
+ endpoint = context.http_request.endpoint
56
+ now_utc = Time.now.utc
57
+ server_time = server_time(context.http_response)
58
+ if server_time && (now_utc - server_time).abs > CLOCK_SKEW_THRESHOLD
59
+ set_clock_correction(endpoint, server_time - now_utc)
60
+ end
61
+ end
62
+
63
+ # Called for every request
64
+ # Update our estimated clock skew for the endpoint
65
+ # from the servers time in the response
66
+ # @param context [Seahorse::Client::RequestContext]
67
+ def update_estimated_skew(context)
68
+ endpoint = context.http_request.endpoint
69
+ now_utc = Time.now.utc
70
+ server_time = server_time(context.http_response)
71
+ return unless server_time
72
+ @mutex.synchronize do
73
+ @endpoint_estimated_skews[endpoint.to_s] = server_time - now_utc
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ # @param response [Seahorse::Client::Http::Response:]
80
+ def server_time(response)
81
+ begin
82
+ Time.parse(response.headers['date']).utc
83
+ rescue
84
+ nil
85
+ end
86
+ end
87
+
88
+ # Sets the clock correction for an endpoint
89
+ # @param endpoint [URI / String]
90
+ # @param correction [Number]
91
+ def set_clock_correction(endpoint, correction)
92
+ @mutex.synchronize do
93
+ @endpoint_clock_corrections[endpoint.to_s] = correction
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module Plugins
5
+ module Retries
6
+ # @api private
7
+ # This class will be obsolete when APIs contain modeled exceptions
8
+ class ErrorInspector
9
+ EXPIRED_CREDS = Set.new(
10
+ [
11
+ 'InvalidClientTokenId', # query services
12
+ 'UnrecognizedClientException', # json services
13
+ 'InvalidAccessKeyId', # s3
14
+ 'AuthFailure', # ec2
15
+ 'InvalidIdentityToken', # sts
16
+ 'ExpiredToken', # route53
17
+ 'ExpiredTokenException' # kinesis
18
+ ]
19
+ )
20
+
21
+ THROTTLING_ERRORS = Set.new(
22
+ [
23
+ 'Throttling', # query services
24
+ 'ThrottlingException', # json services
25
+ 'ThrottledException', # sns
26
+ 'RequestThrottled', # sqs
27
+ 'RequestThrottledException', # generic service
28
+ 'ProvisionedThroughputExceededException', # dynamodb
29
+ 'TransactionInProgressException', # dynamodb
30
+ 'RequestLimitExceeded', # ec2
31
+ 'BandwidthLimitExceeded', # cloud search
32
+ 'LimitExceededException', # kinesis
33
+ 'TooManyRequestsException', # batch
34
+ 'PriorRequestNotComplete', # route53
35
+ 'SlowDown', # s3
36
+ 'EC2ThrottledException' # ec2
37
+ ]
38
+ )
39
+
40
+ CHECKSUM_ERRORS = Set.new(
41
+ [
42
+ 'CRC32CheckFailed' # dynamodb
43
+ ]
44
+ )
45
+
46
+ NETWORKING_ERRORS = Set.new(
47
+ [
48
+ 'RequestTimeout', # s3
49
+ 'InternalError', # s3
50
+ 'RequestTimeoutException', # glacier
51
+ 'IDPCommunicationError' # sts
52
+ ]
53
+ )
54
+
55
+ # See: https://github.com/aws/aws-sdk-net/blob/5810dfe401e0eac2e59d02276d4b479224b4538e/sdk/src/Core/Amazon.Runtime/Pipeline/RetryHandler/RetryPolicy.cs#L78
56
+ CLOCK_SKEW_ERRORS = Set.new(
57
+ [
58
+ 'RequestTimeTooSkewed',
59
+ 'RequestExpired',
60
+ 'InvalidSignatureException',
61
+ 'SignatureDoesNotMatch',
62
+ 'AuthFailure',
63
+ 'RequestInTheFuture'
64
+ ]
65
+ )
66
+
67
+ def initialize(error, http_status_code)
68
+ @error = error
69
+ @name = extract_name(@error)
70
+ @http_status_code = http_status_code
71
+ end
72
+
73
+ def expired_credentials?
74
+ !!(EXPIRED_CREDS.include?(@name) || @name.match(/expired/i))
75
+ end
76
+
77
+ def throttling_error?
78
+ !!(THROTTLING_ERRORS.include?(@name) ||
79
+ @name.match(/throttl/i) ||
80
+ @http_status_code == 429) ||
81
+ modeled_throttling?
82
+ end
83
+
84
+ def checksum?
85
+ CHECKSUM_ERRORS.include?(@name) || @error.is_a?(Errors::ChecksumError)
86
+ end
87
+
88
+ def networking?
89
+ @error.is_a?(Seahorse::Client::NetworkingError) ||
90
+ @error.is_a?(Errors::NoSuchEndpointError) ||
91
+ NETWORKING_ERRORS.include?(@name)
92
+ end
93
+
94
+ def server?
95
+ (500..599).cover?(@http_status_code)
96
+ end
97
+
98
+ def endpoint_discovery?(context)
99
+ return false unless context.operation.endpoint_discovery
100
+
101
+ @http_status_code == 421 ||
102
+ @name == 'InvalidEndpointException' ||
103
+ @error.is_a?(Errors::EndpointDiscoveryError)
104
+ end
105
+
106
+ def modeled_retryable?
107
+ @error.is_a?(Errors::ServiceError) && @error.retryable?
108
+ end
109
+
110
+ def modeled_throttling?
111
+ @error.is_a?(Errors::ServiceError) && @error.throttling?
112
+ end
113
+
114
+ def clock_skew?(context)
115
+ CLOCK_SKEW_ERRORS.include?(@name) &&
116
+ context.config.clock_skew.clock_skewed?(context)
117
+ end
118
+
119
+ def retryable?(context)
120
+ server? ||
121
+ modeled_retryable? ||
122
+ throttling_error? ||
123
+ networking? ||
124
+ checksum? ||
125
+ endpoint_discovery?(context) ||
126
+ (expired_credentials? && refreshable_credentials?(context)) ||
127
+ clock_skew?(context)
128
+ end
129
+
130
+ private
131
+
132
+ def refreshable_credentials?(context)
133
+ context.config.credentials.respond_to?(:refresh!)
134
+ end
135
+
136
+ def extract_name(error)
137
+ if error.is_a?(Errors::ServiceError)
138
+ error.class.code || error.class.name.to_s
139
+ else
140
+ error.class.name.to_s
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module Plugins
5
+ module Retries
6
+
7
+ # @api private
8
+ # Used in 'standard' and 'adaptive' retry modes.
9
+ class RetryQuota
10
+ INITIAL_RETRY_TOKENS = 500
11
+ RETRY_COST = 5
12
+ NO_RETRY_INCREMENT = 1
13
+ TIMEOUT_RETRY_COST = 10
14
+
15
+ def initialize(opts = {})
16
+ @mutex = Mutex.new
17
+ @max_capacity = opts.fetch(:max_capacity, INITIAL_RETRY_TOKENS)
18
+ @available_capacity = @max_capacity
19
+ end
20
+
21
+ # check if there is sufficient capacity to retry
22
+ # and return it. If there is insufficient capacity
23
+ # return 0
24
+ # @return [Integer] The amount of capacity checked out
25
+ def checkout_capacity(error_inspector)
26
+ @mutex.synchronize do
27
+ capacity_amount = if error_inspector.networking?
28
+ TIMEOUT_RETRY_COST
29
+ else
30
+ RETRY_COST
31
+ end
32
+
33
+ # unable to acquire capacity
34
+ return 0 if capacity_amount > @available_capacity
35
+
36
+ @available_capacity -= capacity_amount
37
+ capacity_amount
38
+ end
39
+ end
40
+
41
+ # capacity_amount refers to the amount of capacity requested from
42
+ # the last retry. It can either be RETRY_COST, TIMEOUT_RETRY_COST,
43
+ # or unset.
44
+ def release(capacity_amount)
45
+ # Implementation note: The release() method is called for
46
+ # every API call. In the common case where the request is
47
+ # successful and we're at full capacity, we can avoid locking.
48
+ # We can't exceed max capacity so there's no work we have to do.
49
+ return if @available_capacity == @max_capacity
50
+
51
+ @mutex.synchronize do
52
+ @available_capacity += capacity_amount || NO_RETRY_INCREMENT
53
+ @available_capacity = [@available_capacity, @max_capacity].min
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end