lib/auth0/mixins/httpproxy.rb in auth0-5.9.0 vs lib/auth0/mixins/httpproxy.rb in auth0-5.10.0

- old
+ new

@@ -6,15 +6,16 @@ module Mixins # here's the proxy for Rest calls based on rest-client, we're building all request on that gem # for now, if you want to feel free to use your own http client module HTTPProxy attr_accessor :headers, :base_uri, :timeout, :retry_count - DEAFULT_RETRIES = 3 + DEFAULT_RETRIES = 3 MAX_ALLOWED_RETRIES = 10 MAX_REQUEST_RETRY_JITTER = 250 MAX_REQUEST_RETRY_DELAY = 1000 - MIN_REQUEST_RETRY_DELAY = 100 + MIN_REQUEST_RETRY_DELAY = 250 + BASE_DELAY = 100 # proxying requests from instance methods to HTTP class methods %i(get post post_file put patch delete delete_with_body).each do |method| define_method(method) do |uri, body = {}, extra_headers = {}| body = body.delete_if { |_, v| v.nil? } @@ -24,18 +25,18 @@ end end def retry_options sleep_timer = lambda do |attempt| - wait = 1000 * 2**attempt # Exponential delay with each subsequent request attempt. - wait += rand(wait..wait+MAX_REQUEST_RETRY_JITTER) # Add jitter to the delay window. + wait = BASE_DELAY * (2**attempt-1) # Exponential delay with each subsequent request attempt. + wait += rand(wait+1..wait+MAX_REQUEST_RETRY_JITTER) # Add jitter to the delay window. wait = [MAX_REQUEST_RETRY_DELAY, wait].min # Cap delay at MAX_REQUEST_RETRY_DELAY. wait = [MIN_REQUEST_RETRY_DELAY, wait].max # Ensure delay is no less than MIN_REQUEST_RETRY_DELAY. wait / 1000.to_f.round(2) # convert ms to seconds end - tries = 1 + [Integer(retry_count || DEAFULT_RETRIES), MAX_ALLOWED_RETRIES].min # Cap retries at MAX_ALLOWED_RETRIES + tries = 1 + [Integer(retry_count || DEFAULT_RETRIES), MAX_ALLOWED_RETRIES].min # Cap retries at MAX_ALLOWED_RETRIES { tries: tries, sleep: sleep_timer, on: Auth0::RateLimitEncountered @@ -70,18 +71,16 @@ end end def request(method, uri, body = {}, extra_headers = {}) result = if method == :get - # Mutate the headers property to add parameters. - add_headers({params: body}) - # Merge custom headers into existing ones for this req. - # This prevents future calls from using them. - get_headers = headers.merge extra_headers - # Make the call with extra_headers, if provided. + @headers ||= {} + get_headers = @headers.merge({params: body}).merge(extra_headers) call(:get, encode_uri(uri), timeout, get_headers) elsif method == :delete - call(:delete, encode_uri(uri), timeout, add_headers({params: body})) + @headers ||= {} + delete_headers = @headers.merge({ params: body }) + call(:delete, encode_uri(uri), timeout, delete_headers) elsif method == :delete_with_body call(:delete, encode_uri(uri), timeout, headers, body.to_json) elsif method == :post_file body.merge!(multipart: true) # Ignore the default Content-Type headers and let the HTTP client define them