hearth 1.0.0.pre1 → 1.0.0.pre3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -4
- data/VERSION +1 -1
- data/lib/hearth/anonymous_auth_resolver.rb +11 -0
- data/lib/hearth/api_error.rb +15 -1
- data/lib/hearth/auth_option.rb +21 -0
- data/lib/hearth/auth_schemes/anonymous.rb +21 -0
- data/lib/hearth/auth_schemes/http_api_key.rb +16 -0
- data/lib/hearth/auth_schemes/http_basic.rb +16 -0
- data/lib/hearth/auth_schemes/http_bearer.rb +16 -0
- data/lib/hearth/auth_schemes/http_digest.rb +16 -0
- data/lib/hearth/auth_schemes.rb +32 -0
- data/lib/hearth/checksums.rb +31 -0
- data/lib/hearth/client.rb +66 -0
- data/lib/hearth/client_stubs.rb +128 -0
- data/lib/hearth/config/env_provider.rb +53 -0
- data/lib/hearth/config/resolver.rb +53 -0
- data/lib/hearth/configuration.rb +15 -0
- data/lib/hearth/connection_pool.rb +77 -0
- data/lib/hearth/context.rb +29 -4
- data/lib/hearth/dns/host_address.rb +27 -0
- data/lib/hearth/dns/host_resolver.rb +92 -0
- data/lib/hearth/dns.rb +48 -0
- data/lib/hearth/endpoint_rules.rb +154 -0
- data/lib/hearth/http/api_error.rb +4 -8
- data/lib/hearth/http/client.rb +206 -59
- data/lib/hearth/http/error_inspector.rb +85 -0
- data/lib/hearth/http/error_parser.rb +18 -20
- data/lib/hearth/http/field.rb +49 -0
- data/lib/hearth/http/fields.rb +117 -0
- data/lib/hearth/http/header_list_builder.rb +42 -0
- data/lib/hearth/http/header_list_parser.rb +92 -0
- data/lib/hearth/http/middleware/content_length.rb +7 -4
- data/lib/hearth/http/middleware/content_md5.rb +30 -0
- data/lib/hearth/http/middleware/request_compression.rb +154 -0
- data/lib/hearth/http/middleware.rb +12 -0
- data/lib/hearth/http/networking_error.rb +1 -14
- data/lib/hearth/http/request.rb +83 -56
- data/lib/hearth/http/response.rb +42 -13
- data/lib/hearth/http.rb +16 -5
- data/lib/hearth/identities/anonymous.rb +8 -0
- data/lib/hearth/identities/http_api_key.rb +16 -0
- data/lib/hearth/identities/http_bearer.rb +16 -0
- data/lib/hearth/identities/http_login.rb +20 -0
- data/lib/hearth/identities.rb +21 -0
- data/lib/hearth/identity_provider.rb +17 -0
- data/lib/hearth/interceptor.rb +506 -0
- data/lib/hearth/interceptor_context.rb +40 -0
- data/lib/hearth/interceptor_list.rb +48 -0
- data/lib/hearth/interceptors.rb +76 -0
- data/lib/hearth/json.rb +4 -4
- data/lib/hearth/middleware/auth.rb +103 -0
- data/lib/hearth/middleware/build.rb +32 -1
- data/lib/hearth/middleware/endpoint.rb +79 -0
- data/lib/hearth/middleware/host_prefix.rb +11 -8
- data/lib/hearth/middleware/initialize.rb +57 -0
- data/lib/hearth/middleware/parse.rb +45 -7
- data/lib/hearth/middleware/retry.rb +105 -24
- data/lib/hearth/middleware/send.rb +137 -26
- data/lib/hearth/middleware/sign.rb +65 -0
- data/lib/hearth/middleware/validate.rb +11 -1
- data/lib/hearth/middleware.rb +20 -8
- data/lib/hearth/middleware_stack.rb +2 -44
- data/lib/hearth/networking_error.rb +18 -0
- data/lib/hearth/number_helper.rb +3 -3
- data/lib/hearth/output.rb +8 -4
- data/lib/hearth/plugin_list.rb +53 -0
- data/lib/hearth/query/param.rb +56 -0
- data/lib/hearth/query/param_list.rb +54 -0
- data/lib/hearth/query/param_matcher.rb +31 -0
- data/lib/hearth/refreshing_identity_provider.rb +63 -0
- data/lib/hearth/request.rb +22 -0
- data/lib/hearth/response.rb +36 -0
- data/lib/hearth/retry/adaptive.rb +60 -0
- data/lib/hearth/retry/capacity_not_available_error.rb +9 -0
- data/lib/hearth/retry/client_rate_limiter.rb +145 -0
- data/lib/hearth/retry/exponential_backoff.rb +15 -0
- data/lib/hearth/retry/retry_quota.rb +56 -0
- data/lib/hearth/retry/standard.rb +46 -0
- data/lib/hearth/retry.rb +29 -0
- data/lib/hearth/signers/anonymous.rb +16 -0
- data/lib/hearth/signers/http_api_key.rb +29 -0
- data/lib/hearth/signers/http_basic.rb +23 -0
- data/lib/hearth/signers/http_bearer.rb +19 -0
- data/lib/hearth/signers/http_digest.rb +19 -0
- data/lib/hearth/signers.rb +23 -0
- data/lib/hearth/structure.rb +7 -3
- data/lib/hearth/stubs.rb +38 -0
- data/lib/hearth/time_helper.rb +6 -5
- data/lib/hearth/validator.rb +60 -5
- data/lib/hearth/waiters/poller.rb +10 -9
- data/lib/hearth/waiters/waiter.rb +23 -9
- data/lib/hearth/xml/formatter.rb +11 -2
- data/lib/hearth/xml/node.rb +2 -3
- data/lib/hearth/xml/node_matcher.rb +0 -1
- data/lib/hearth.rb +37 -6
- data/sig/lib/hearth/aliases.rbs +6 -0
- data/sig/lib/hearth/anonymous_auth_resolver.rbs +5 -0
- data/sig/lib/hearth/api_error.rbs +13 -0
- data/sig/lib/hearth/auth_option.rbs +11 -0
- data/sig/lib/hearth/auth_schemes/anonymous.rbs +7 -0
- data/sig/lib/hearth/auth_schemes/http_api_key.rbs +7 -0
- data/sig/lib/hearth/auth_schemes/http_basic.rbs +7 -0
- data/sig/lib/hearth/auth_schemes/http_bearer.rbs +7 -0
- data/sig/lib/hearth/auth_schemes/http_digest.rbs +7 -0
- data/sig/lib/hearth/auth_schemes.rbs +13 -0
- data/sig/lib/hearth/block_io.rbs +9 -0
- data/sig/lib/hearth/client.rbs +9 -0
- data/sig/lib/hearth/client_stubs.rbs +5 -0
- data/sig/lib/hearth/configuration.rbs +7 -0
- data/sig/lib/hearth/dns/host_address.rbs +11 -0
- data/sig/lib/hearth/dns/host_resolver.rbs +19 -0
- data/sig/lib/hearth/endpoint_rules.rbs +17 -0
- data/sig/lib/hearth/http/api_error.rbs +13 -0
- data/sig/lib/hearth/http/client.rbs +9 -0
- data/sig/lib/hearth/http/field.rbs +19 -0
- data/sig/lib/hearth/http/fields.rbs +43 -0
- data/sig/lib/hearth/http/header_list_builder.rbs +15 -0
- data/sig/lib/hearth/http/header_list_parser.rbs +19 -0
- data/sig/lib/hearth/http/networking_error.rbs +6 -0
- data/sig/lib/hearth/http/request.rbs +25 -0
- data/sig/lib/hearth/http/response.rbs +21 -0
- data/sig/lib/hearth/identities/anonymous.rbs +6 -0
- data/sig/lib/hearth/identities/http_api_key.rbs +9 -0
- data/sig/lib/hearth/identities/http_bearer.rbs +9 -0
- data/sig/lib/hearth/identities/http_login.rbs +11 -0
- data/sig/lib/hearth/identities.rbs +9 -0
- data/sig/lib/hearth/identity_provider.rbs +7 -0
- data/sig/lib/hearth/interceptor.rbs +9 -0
- data/sig/lib/hearth/interceptor_context.rbs +17 -0
- data/sig/lib/hearth/interceptor_list.rbs +16 -0
- data/sig/lib/hearth/interfaces.rbs +87 -0
- data/sig/lib/hearth/json/parse_error.rbs +9 -0
- data/sig/lib/hearth/networking_error.rbs +7 -0
- data/sig/lib/hearth/output.rbs +11 -0
- data/sig/lib/hearth/plugin_list.rbs +13 -0
- data/sig/lib/hearth/query/param.rbs +17 -0
- data/sig/lib/hearth/query/param_list.rbs +25 -0
- data/sig/lib/hearth/refreshing_identity_provider.rbs +10 -0
- data/sig/lib/hearth/request.rbs +9 -0
- data/sig/lib/hearth/response.rbs +11 -0
- data/sig/lib/hearth/retry/adaptive.rbs +13 -0
- data/sig/lib/hearth/retry/exponential_backoff.rbs +7 -0
- data/sig/lib/hearth/retry/standard.rbs +13 -0
- data/sig/lib/hearth/retry/strategy.rbs +11 -0
- data/sig/lib/hearth/retry.rbs +9 -0
- data/sig/lib/hearth/signers/anonymous.rbs +9 -0
- data/sig/lib/hearth/signers/http_api_key.rbs +9 -0
- data/sig/lib/hearth/signers/http_basic.rbs +9 -0
- data/sig/lib/hearth/signers/http_bearer.rbs +9 -0
- data/sig/lib/hearth/signers/http_digest.rbs +9 -0
- data/sig/lib/hearth/signers.rbs +9 -0
- data/sig/lib/hearth/structure.rbs +6 -0
- data/sig/lib/hearth/stubs.rbs +9 -0
- data/sig/lib/hearth/union.rbs +5 -0
- data/sig/lib/hearth/waiters/waiter.rbs +17 -0
- data/sig/lib/hearth/xml/parse_error.rbs +9 -0
- metadata +151 -25
- data/lib/hearth/http/headers.rb +0 -70
- data/lib/hearth/middleware/around_handler.rb +0 -24
- data/lib/hearth/middleware/request_handler.rb +0 -24
- data/lib/hearth/middleware/response_handler.rb +0 -25
- data/lib/hearth/middleware_builder.rb +0 -246
- data/lib/hearth/stubbing/client_stubs.rb +0 -115
- data/lib/hearth/stubbing/stubs.rb +0 -32
- data/lib/hearth/waiters/errors.rb +0 -15
- data/sig/lib/seahorse/api_error.rbs +0 -10
- data/sig/lib/seahorse/document.rbs +0 -2
- data/sig/lib/seahorse/http/api_error.rbs +0 -21
- data/sig/lib/seahorse/http/headers.rbs +0 -47
- data/sig/lib/seahorse/http/response.rbs +0 -21
- data/sig/lib/seahorse/simple_delegator.rbs +0 -3
- data/sig/lib/seahorse/structure.rbs +0 -18
- data/sig/lib/seahorse/stubbing/client_stubs.rbs +0 -103
- data/sig/lib/seahorse/stubbing/stubs.rbs +0 -14
- data/sig/lib/seahorse/union.rbs +0 -6
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Query
|
5
|
+
# A class used to represent a query parameter before serialization.
|
6
|
+
class Param
|
7
|
+
# @param [String] name
|
8
|
+
# @param [String, Array<String>, nil] value (nil)
|
9
|
+
def initialize(name, value = nil)
|
10
|
+
@name = name
|
11
|
+
@value = value
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
attr_reader :name
|
16
|
+
|
17
|
+
# @return [String, Array<String>, nil]
|
18
|
+
attr_reader :value
|
19
|
+
|
20
|
+
# @return [String]
|
21
|
+
def to_s
|
22
|
+
if value.is_a?(Array)
|
23
|
+
serialize_array(name, value)
|
24
|
+
else
|
25
|
+
serialize(name, value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Boolean]
|
30
|
+
def ==(other)
|
31
|
+
other.is_a?(Param) &&
|
32
|
+
other.name == name &&
|
33
|
+
other.value == value
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [Integer]
|
37
|
+
def <=>(other)
|
38
|
+
name <=> other.name
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def serialize(name, value)
|
44
|
+
value.nil? ? escape(name) : "#{escape(name)}=#{escape(value)}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def serialize_array(name, values)
|
48
|
+
values.map { |v| serialize(name, v) }.join('&')
|
49
|
+
end
|
50
|
+
|
51
|
+
def escape(value)
|
52
|
+
Hearth::HTTP.uri_escape(value.to_s)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Query
|
5
|
+
# A class used to collect query params before serialization.
|
6
|
+
class ParamList
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@params = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param [String] param_name
|
14
|
+
# @param [String, nil] param_value
|
15
|
+
# @return [Param]
|
16
|
+
def set(param_name, param_value = nil)
|
17
|
+
param = Param.new(param_name, param_value)
|
18
|
+
@params[param.name] = param
|
19
|
+
end
|
20
|
+
alias []= set
|
21
|
+
|
22
|
+
# @return [Param, nil]
|
23
|
+
def [](param_name)
|
24
|
+
@params[param_name]
|
25
|
+
end
|
26
|
+
|
27
|
+
# @param [String] param_name
|
28
|
+
# @return [Param, nil]
|
29
|
+
def delete(param_name)
|
30
|
+
@params.delete(param_name)
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Enumerable]
|
34
|
+
def each(&block)
|
35
|
+
to_a.each(&block)
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Boolean]
|
39
|
+
def empty?
|
40
|
+
@params.empty?
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [Array<Param>] Returns an array of sorted {Param} objects.
|
44
|
+
def to_a
|
45
|
+
@params.values.sort
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [String]
|
49
|
+
def to_s
|
50
|
+
to_a.map(&:to_s).join('&')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rspec/expectations'
|
4
|
+
|
5
|
+
# Provides an rspec matcher for CGI.parse to check Float precision.
|
6
|
+
RSpec::Matchers.define :match_query_params do |expected|
|
7
|
+
match do |actual|
|
8
|
+
return true if actual == expected
|
9
|
+
return false unless actual.instance_of?(expected.class)
|
10
|
+
|
11
|
+
expect(actual.keys.length).to eq(expected.keys.length)
|
12
|
+
expect(actual.values.length).to eq(expected.values.length)
|
13
|
+
|
14
|
+
expected.each do |ek, e|
|
15
|
+
expect(actual.keys).to include(ek)
|
16
|
+
a = actual[ek]
|
17
|
+
expect(e.length).to eq(a.length)
|
18
|
+
|
19
|
+
a.zip(e).each do |a0, e0|
|
20
|
+
# Timestamps can have optional precision.
|
21
|
+
|
22
|
+
float = Float(a0)
|
23
|
+
expect(float).to eq(e0.to_f)
|
24
|
+
rescue StandardError
|
25
|
+
expect(a0).to eq(e0)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
diffable
|
31
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
# A module that can be included in a class to provide a #identity method.
|
5
|
+
# The class must implement #refresh(properties) that sets @identity. The
|
6
|
+
# refresh method will be called when #identity is called and the identity
|
7
|
+
# is nil or near expiration.
|
8
|
+
module RefreshingIdentityProvider
|
9
|
+
SYNC_EXPIRATION_LENGTH = 300 # 5 minutes
|
10
|
+
ASYNC_EXPIRATION_LENGTH = 600 # 10 minutes
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@mutex = Mutex.new
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Identities::Base]
|
17
|
+
def identity(properties = {})
|
18
|
+
if @identity
|
19
|
+
refresh_if_near_expiration!(properties)
|
20
|
+
else # initialization
|
21
|
+
@mutex.synchronize { refresh(properties) }
|
22
|
+
end
|
23
|
+
@identity
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def sync_expiration_length
|
29
|
+
self.class::SYNC_EXPIRATION_LENGTH
|
30
|
+
end
|
31
|
+
|
32
|
+
def async_expiration_length
|
33
|
+
self.class::ASYNC_EXPIRATION_LENGTH
|
34
|
+
end
|
35
|
+
|
36
|
+
# Refreshes identity asynchronously and synchronously.
|
37
|
+
# If we are near to expiration, block while refreshing the identity.
|
38
|
+
# Otherwise, if we're approaching expiration, use the existing identity
|
39
|
+
# but attempt a refresh in the background.
|
40
|
+
def refresh_if_near_expiration!(properties)
|
41
|
+
# NOTE: This check is an optimization. Rather than acquire the mutex on
|
42
|
+
# every #refresh_if_near_expiration call, we check before doing so, and
|
43
|
+
# then we check within the mutex to avoid a race condition.
|
44
|
+
if near_expiration?(sync_expiration_length)
|
45
|
+
@mutex.synchronize do
|
46
|
+
refresh(properties) if near_expiration?(sync_expiration_length)
|
47
|
+
end
|
48
|
+
elsif @async_refresh && near_expiration?(async_expiration_length)
|
49
|
+
unless @mutex.locked?
|
50
|
+
Thread.new do
|
51
|
+
@mutex.synchronize do
|
52
|
+
refresh(properties) if near_expiration?(async_expiration_length)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def near_expiration?(expiration_length)
|
60
|
+
(Time.now.to_i + expiration_length) > @identity.expiration.to_i
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
module Hearth
|
7
|
+
# Represents a base request.
|
8
|
+
class Request
|
9
|
+
# @param [URI] uri (URI(''))
|
10
|
+
# @param [IO, StringIO] body (StringIO.new)
|
11
|
+
def initialize(uri: URI(''), body: StringIO.new)
|
12
|
+
@uri = uri
|
13
|
+
@body = body
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [URI]
|
17
|
+
attr_accessor :uri
|
18
|
+
|
19
|
+
# @return [IO, StringIO]
|
20
|
+
attr_accessor :body
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module Hearth
|
6
|
+
# Represents a base response.
|
7
|
+
class Response
|
8
|
+
# @param [IO, StringIO] body (StringIO.new)
|
9
|
+
def initialize(body: StringIO.new)
|
10
|
+
@body = body
|
11
|
+
end
|
12
|
+
|
13
|
+
# @return [IO, StringIO]
|
14
|
+
attr_accessor :body
|
15
|
+
|
16
|
+
# Replace attributes from other response
|
17
|
+
# @param [Response] other
|
18
|
+
# @return [Response]
|
19
|
+
def replace(other)
|
20
|
+
@body.truncate(0)
|
21
|
+
IO.copy_stream(other.body, @body)
|
22
|
+
@body.rewind
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
# Resets the response.
|
27
|
+
# @return [Response]
|
28
|
+
def reset
|
29
|
+
# IO does not respond to #truncate but it does respond to #rewind
|
30
|
+
# however it returns an illegal seek error.
|
31
|
+
@body.truncate(0) if @body.respond_to?(:truncate)
|
32
|
+
@body.rewind if @body.respond_to?(:rewind) && !@body.instance_of?(IO)
|
33
|
+
self
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Retry
|
5
|
+
# Adaptive retry strategy for retrying requests.
|
6
|
+
class Adaptive
|
7
|
+
# @param [#call] backoff (ExponentialBackoff) A callable object that
|
8
|
+
# calculates a backoff delay for a retry attempt.
|
9
|
+
# @param [Integer] max_attempts (3) The maximum number of attempts that
|
10
|
+
# will be made for a single request, including the initial attempt.
|
11
|
+
# @param [Boolean] wait_to_fill When true, the request will sleep until
|
12
|
+
# there is sufficient client side capacity to retry the request. When
|
13
|
+
# false, the request will raise a `CapacityNotAvailableError` and will
|
14
|
+
# not retry instead of sleeping.
|
15
|
+
def initialize(backoff: ExponentialBackoff.new, max_attempts: 3,
|
16
|
+
wait_to_fill: true)
|
17
|
+
super()
|
18
|
+
@backoff = backoff
|
19
|
+
@max_attempts = max_attempts
|
20
|
+
@wait_to_fill = wait_to_fill
|
21
|
+
|
22
|
+
# instance state
|
23
|
+
@client_rate_limiter = ClientRateLimiter.new
|
24
|
+
@retry_quota = RetryQuota.new
|
25
|
+
@capacity_amount = 0
|
26
|
+
end
|
27
|
+
|
28
|
+
def acquire_initial_retry_token(_token_scope = nil)
|
29
|
+
@client_rate_limiter.token_bucket_acquire(
|
30
|
+
1, wait_to_fill: @wait_to_fill
|
31
|
+
)
|
32
|
+
Token.new(retry_count: 0)
|
33
|
+
end
|
34
|
+
|
35
|
+
def refresh_retry_token(retry_token, error_info)
|
36
|
+
return unless error_info.retryable?
|
37
|
+
|
38
|
+
@client_rate_limiter.update_sending_rate(
|
39
|
+
error_info.error_type == 'Throttling'
|
40
|
+
)
|
41
|
+
return if retry_token.retry_count >= @max_attempts - 1
|
42
|
+
|
43
|
+
@capacity_amount = @retry_quota.checkout_capacity(error_info)
|
44
|
+
return unless @capacity_amount.positive?
|
45
|
+
|
46
|
+
delay = error_info.hints[:retry_after]
|
47
|
+
delay ||= @backoff.call(retry_token.retry_count)
|
48
|
+
retry_token.retry_count += 1
|
49
|
+
retry_token.retry_delay = delay
|
50
|
+
retry_token
|
51
|
+
end
|
52
|
+
|
53
|
+
def record_success(retry_token)
|
54
|
+
@client_rate_limiter.update_sending_rate(false)
|
55
|
+
@retry_quota.release(@capacity_amount)
|
56
|
+
retry_token
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Retry
|
5
|
+
# @api private
|
6
|
+
# Used only in 'adaptive' retry mode
|
7
|
+
class ClientRateLimiter
|
8
|
+
MIN_CAPACITY = 1
|
9
|
+
MIN_FILL_RATE = 0.5
|
10
|
+
SMOOTH = 0.8
|
11
|
+
# How much to scale back after a throttling response
|
12
|
+
BETA = 0.7
|
13
|
+
# Controls how aggressively we scale up after being throttled
|
14
|
+
SCALE_CONSTANT = 0.4
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@mutex = Mutex.new
|
18
|
+
@fill_rate = nil
|
19
|
+
@max_capacity = nil
|
20
|
+
@current_capacity = 0
|
21
|
+
@last_timestamp = nil
|
22
|
+
@enabled = false
|
23
|
+
@measured_tx_rate = 0
|
24
|
+
@last_tx_rate_bucket = monotonic_seconds
|
25
|
+
@request_count = 0
|
26
|
+
@last_max_rate = 0
|
27
|
+
@last_throttle_time = monotonic_seconds
|
28
|
+
@calculated_rate = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def token_bucket_acquire(amount, wait_to_fill: true)
|
32
|
+
# Client side throttling is not enabled until we see a
|
33
|
+
# throttling error
|
34
|
+
return unless @enabled
|
35
|
+
|
36
|
+
@mutex.synchronize do
|
37
|
+
token_bucket_refill
|
38
|
+
|
39
|
+
# Next see if we have enough capacity for the requested amount
|
40
|
+
while @current_capacity < amount
|
41
|
+
raise CapacityNotAvailableError unless wait_to_fill
|
42
|
+
|
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
|
+
# The fill_rate is from the token bucket
|
56
|
+
@last_max_rate = rate_to_use
|
57
|
+
calculate_time_window
|
58
|
+
@last_throttle_time = monotonic_seconds
|
59
|
+
@calculated_rate = cubic_throttle(rate_to_use)
|
60
|
+
enable_token_bucket
|
61
|
+
else
|
62
|
+
calculate_time_window
|
63
|
+
@calculated_rate = cubic_success(monotonic_seconds)
|
64
|
+
end
|
65
|
+
|
66
|
+
new_rate = [@calculated_rate, 2 * @measured_tx_rate].min
|
67
|
+
token_bucket_update_rate(new_rate)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def monotonic_seconds
|
74
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
75
|
+
end
|
76
|
+
|
77
|
+
def token_bucket_refill
|
78
|
+
timestamp = monotonic_seconds
|
79
|
+
unless @last_timestamp
|
80
|
+
@last_timestamp = timestamp
|
81
|
+
return
|
82
|
+
end
|
83
|
+
|
84
|
+
fill_amount = (timestamp - @last_timestamp) * @fill_rate
|
85
|
+
@current_capacity = [
|
86
|
+
@max_capacity, @current_capacity + fill_amount
|
87
|
+
].min
|
88
|
+
|
89
|
+
@last_timestamp = timestamp
|
90
|
+
end
|
91
|
+
|
92
|
+
def token_bucket_update_rate(new_rps)
|
93
|
+
# Refill based on our current rate before we update to the
|
94
|
+
# new fill rate
|
95
|
+
token_bucket_refill
|
96
|
+
@fill_rate = [new_rps, MIN_FILL_RATE].max
|
97
|
+
@max_capacity = [new_rps, MIN_CAPACITY].max
|
98
|
+
# When we scale down we can't have a current capacity that exceeds our
|
99
|
+
# max_capacity.
|
100
|
+
@current_capacity = [@current_capacity, @max_capacity].min
|
101
|
+
end
|
102
|
+
|
103
|
+
def enable_token_bucket
|
104
|
+
@enabled = true
|
105
|
+
end
|
106
|
+
|
107
|
+
def update_measured_rate
|
108
|
+
t = monotonic_seconds
|
109
|
+
time_bucket = (t * 2).floor / 2.0
|
110
|
+
@request_count += 1
|
111
|
+
return unless time_bucket > @last_tx_rate_bucket
|
112
|
+
|
113
|
+
current_rate = @request_count / (time_bucket - @last_tx_rate_bucket)
|
114
|
+
@measured_tx_rate = (current_rate * SMOOTH) +
|
115
|
+
(@measured_tx_rate * (1 - SMOOTH))
|
116
|
+
@request_count = 0
|
117
|
+
@last_tx_rate_bucket = time_bucket
|
118
|
+
end
|
119
|
+
|
120
|
+
def calculate_time_window
|
121
|
+
# This is broken out into a separate calculation because it only
|
122
|
+
# gets updated when @last_max_rate changes so it can be cached.
|
123
|
+
base = ((@last_max_rate * (1 - BETA)) / SCALE_CONSTANT)
|
124
|
+
@time_window = base**(1.0 / 3)
|
125
|
+
end
|
126
|
+
|
127
|
+
def cubic_success(timestamp)
|
128
|
+
dt = timestamp - @last_throttle_time
|
129
|
+
(SCALE_CONSTANT * ((dt - @time_window)**3)) + @last_max_rate
|
130
|
+
end
|
131
|
+
|
132
|
+
def cubic_throttle(rate_to_use)
|
133
|
+
rate_to_use * BETA
|
134
|
+
end
|
135
|
+
|
136
|
+
def rate_to_use
|
137
|
+
if @enabled
|
138
|
+
[@measured_tx_rate, @fill_rate].min
|
139
|
+
else
|
140
|
+
@measured_tx_rate
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Retry
|
5
|
+
# Computes an exponential backoff delay for a retry attempt.
|
6
|
+
class ExponentialBackoff
|
7
|
+
# Max backoff (in seconds)
|
8
|
+
MAX_BACKOFF = 20
|
9
|
+
|
10
|
+
def call(attempts)
|
11
|
+
[Kernel.rand * (2**attempts), MAX_BACKOFF].min || 0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Retry
|
5
|
+
# @api private
|
6
|
+
# Used in 'standard' and 'adaptive' retry modes.
|
7
|
+
class RetryQuota
|
8
|
+
INITIAL_RETRY_TOKENS = 500
|
9
|
+
RETRY_COST = 5
|
10
|
+
NO_RETRY_INCREMENT = 1
|
11
|
+
TIMEOUT_RETRY_COST = 10
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@mutex = Mutex.new
|
15
|
+
@max_capacity = INITIAL_RETRY_TOKENS
|
16
|
+
@available_capacity = @max_capacity
|
17
|
+
end
|
18
|
+
|
19
|
+
# Check if there is sufficient capacity to retry and return it.
|
20
|
+
# If there is insufficient capacity, return 0
|
21
|
+
# @return [Integer] The amount of capacity checked out
|
22
|
+
def checkout_capacity(error_info)
|
23
|
+
@mutex.synchronize do
|
24
|
+
capacity_amount =
|
25
|
+
if error_info.error_type == 'Transient'
|
26
|
+
TIMEOUT_RETRY_COST
|
27
|
+
else
|
28
|
+
RETRY_COST
|
29
|
+
end
|
30
|
+
|
31
|
+
# unable to acquire capacity
|
32
|
+
return 0 if capacity_amount > @available_capacity
|
33
|
+
|
34
|
+
@available_capacity -= capacity_amount
|
35
|
+
capacity_amount
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# capacity_amount refers to the amount of capacity requested from
|
40
|
+
# the last retry. It can either be RETRY_COST, TIMEOUT_RETRY_COST,
|
41
|
+
# or unset.
|
42
|
+
def release(capacity_amount)
|
43
|
+
# Implementation note: The release() method is called for
|
44
|
+
# every API call. In the common case where the request is
|
45
|
+
# successful and we're at full capacity, we can avoid locking.
|
46
|
+
# We can't exceed max capacity so there's no work we have to do.
|
47
|
+
return if @available_capacity == @max_capacity
|
48
|
+
|
49
|
+
@mutex.synchronize do
|
50
|
+
@available_capacity += capacity_amount || NO_RETRY_INCREMENT
|
51
|
+
@available_capacity = [@available_capacity, @max_capacity].min
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Retry
|
5
|
+
# Standard retry strategy for retrying requests.
|
6
|
+
class Standard
|
7
|
+
# @param [#call] backoff (ExponentialBackoff) A callable object that
|
8
|
+
# calculates a backoff delay for a retry attempt.
|
9
|
+
# @param [Integer] max_attempts (3) The maximum number of attempts that
|
10
|
+
# will be made for a single request, including the initial attempt.
|
11
|
+
def initialize(backoff: ExponentialBackoff.new, max_attempts: 3)
|
12
|
+
super()
|
13
|
+
@backoff = backoff
|
14
|
+
@max_attempts = max_attempts
|
15
|
+
|
16
|
+
# instance state
|
17
|
+
@retry_quota = RetryQuota.new
|
18
|
+
@capacity_amount = 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def acquire_initial_retry_token(_token_scope = nil)
|
22
|
+
Token.new(retry_count: 0)
|
23
|
+
end
|
24
|
+
|
25
|
+
def refresh_retry_token(retry_token, error_info)
|
26
|
+
return unless error_info.retryable?
|
27
|
+
|
28
|
+
return if retry_token.retry_count >= @max_attempts - 1
|
29
|
+
|
30
|
+
@capacity_amount = @retry_quota.checkout_capacity(error_info)
|
31
|
+
return unless @capacity_amount.positive?
|
32
|
+
|
33
|
+
delay = error_info.hints[:retry_after]
|
34
|
+
delay ||= @backoff.call(retry_token.retry_count)
|
35
|
+
retry_token.retry_count += 1
|
36
|
+
retry_token.retry_delay = delay
|
37
|
+
retry_token
|
38
|
+
end
|
39
|
+
|
40
|
+
def record_success(retry_token)
|
41
|
+
@retry_quota.release(@capacity_amount)
|
42
|
+
retry_token
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/hearth/retry.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'retry/adaptive'
|
4
|
+
require_relative 'retry/capacity_not_available_error'
|
5
|
+
require_relative 'retry/client_rate_limiter'
|
6
|
+
require_relative 'retry/exponential_backoff'
|
7
|
+
require_relative 'retry/retry_quota'
|
8
|
+
require_relative 'retry/standard'
|
9
|
+
|
10
|
+
module Hearth
|
11
|
+
module Retry
|
12
|
+
# Represents a token that can be used to retry an operation.
|
13
|
+
# @!attribute retry_count
|
14
|
+
# The number of times the operation has been retried.
|
15
|
+
# @return [Integer]
|
16
|
+
# @!attribute retry_delay
|
17
|
+
# The delay before the next retry.
|
18
|
+
# @return [Numeric]
|
19
|
+
Token = Struct.new(:retry_count, :retry_delay, keyword_init: true) do
|
20
|
+
# @option args [Integer] :retry_count (0)
|
21
|
+
# @option args [Numeric] :retry_delay (0)
|
22
|
+
def initialize(*args)
|
23
|
+
super
|
24
|
+
self.retry_count ||= 0
|
25
|
+
self.retry_delay ||= 0
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Signers
|
5
|
+
# A signer that does not sign requests.
|
6
|
+
class Anonymous < Signers::Base
|
7
|
+
def sign(request:, identity:, properties:)
|
8
|
+
# Do nothing.
|
9
|
+
end
|
10
|
+
|
11
|
+
def reset(request:, properties:)
|
12
|
+
# Do nothing.
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hearth
|
4
|
+
module Signers
|
5
|
+
# A signer that signs requests using the HTTP API Key Auth scheme.
|
6
|
+
class HTTPApiKey < Signers::Base
|
7
|
+
def sign(request:, identity:, properties:)
|
8
|
+
case properties[:in]
|
9
|
+
when 'header'
|
10
|
+
value = "#{properties[:scheme]} #{identity.key}".strip
|
11
|
+
request.headers[properties[:name]] = value
|
12
|
+
when 'query'
|
13
|
+
name = properties[:name]
|
14
|
+
request.append_query_param(name, identity.key)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def reset(request:, properties:)
|
19
|
+
case properties[:in]
|
20
|
+
when 'header'
|
21
|
+
request.headers.delete(properties[:name])
|
22
|
+
when 'query'
|
23
|
+
name = properties[:name]
|
24
|
+
request.remove_query_param(name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|