mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-05 00:02:38 -04:00
Merge branch 'fix-jitter-tests' into 'master'
Fix jitter tests See merge request honeyryderchuck/httpx!172
This commit is contained in:
commit
57b74aa144
@ -22,9 +22,16 @@ module HTTPX
|
||||
Parser::Error,
|
||||
Errno::EINVAL,
|
||||
Errno::ETIMEDOUT].freeze
|
||||
DEFAULT_JITTER = ->(interval) { interval * (0.5 * (1 + rand)) }
|
||||
|
||||
def self.extra_options(options)
|
||||
options.merge(max_retries: MAX_RETRIES)
|
||||
if ENV.key?("HTTPX_NO_JITTER")
|
||||
def self.extra_options(options)
|
||||
options.merge(max_retries: MAX_RETRIES)
|
||||
end
|
||||
else
|
||||
def self.extra_options(options)
|
||||
options.merge(max_retries: MAX_RETRIES, retry_jitter: DEFAULT_JITTER)
|
||||
end
|
||||
end
|
||||
|
||||
module OptionsMethods
|
||||
@ -38,6 +45,13 @@ module HTTPX
|
||||
value
|
||||
end
|
||||
|
||||
def option_retry_jitter(value)
|
||||
# return early if callable
|
||||
raise TypeError, ":retry_jitter must be callable" unless value.respond_to?(:call)
|
||||
|
||||
value
|
||||
end
|
||||
|
||||
def option_max_retries(value)
|
||||
num = Integer(value)
|
||||
raise TypeError, ":max_retries must be positive" unless num.positive?
|
||||
@ -87,8 +101,10 @@ module HTTPX
|
||||
retry_after = retry_after.call(request, response) if retry_after.respond_to?(:call)
|
||||
|
||||
if retry_after
|
||||
# apply some jitter
|
||||
retry_after *= (0.5 * (1 + rand))
|
||||
# apply jitter
|
||||
if (jitter = request.options.retry_jitter)
|
||||
retry_after = jitter.call(retry_after)
|
||||
end
|
||||
|
||||
log { "retrying after #{retry_after} secs..." }
|
||||
pool.after(retry_after) do
|
||||
|
@ -4,6 +4,7 @@ module HTTPX
|
||||
MAX_RETRIES: Integer
|
||||
IDEMPOTENT_METHODS: Array[verb]
|
||||
RETRYABLE_ERRORS: Array[singleton(StandardError)]
|
||||
DEFAULT_JITTER: ^(Numeric) -> Numeric
|
||||
|
||||
interface _RetryCallback
|
||||
def call: (response) -> bool?
|
||||
@ -12,6 +13,8 @@ module HTTPX
|
||||
interface _RetriesOptions
|
||||
def retry_after: () -> Numeric?
|
||||
|
||||
def retry_jitter: () -> ^(Numeric) -> Numeric
|
||||
|
||||
def max_retries: () -> Integer?
|
||||
|
||||
def retry_change_requests: () -> boolish
|
||||
|
@ -24,7 +24,7 @@ class HTTPXAwsSigv4Test < Minitest::Test
|
||||
assert_in_delta(amz_date, Time.now.utc, 3)
|
||||
|
||||
# date already set
|
||||
date = Time.now.utc - 60 * 60 * 24
|
||||
date = Time.now.utc - (60 * 60 * 24)
|
||||
date_amz = date.strftime("%Y%m%dT%H%M%SZ")
|
||||
x_date_request = sigv4_session.build_request(:get, "http://domain.com", headers: { "x-amz-date" => date_amz })
|
||||
verify_header(x_date_request.headers, "x-amz-date", date_amz)
|
||||
|
@ -197,7 +197,7 @@ module Requests
|
||||
c3 = HTTPX::Plugins::Cookies::Cookie.new("a", "b", path: "/cookies")
|
||||
assert [c3, c2, c1].sort == [c3, c1, c2]
|
||||
|
||||
c4 = HTTPX::Plugins::Cookies::Cookie.new("a", "b", created_at: (Time.now - 60 * 60 * 24))
|
||||
c4 = HTTPX::Plugins::Cookies::Cookie.new("a", "b", created_at: (Time.now - (60 * 60 * 24)))
|
||||
assert [c4, c3, c2, c1].sort == [c3, c4, c1, c2]
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user