ruby-auth0 icon indicating copy to clipboard operation
ruby-auth0 copied to clipboard

exponential backoff is not applied

Open luckpoint opened this issue 3 years ago • 1 comments

Describe the problem

I exerted the related code from ruby-auth0/lib/auth0/mixins/httpproxy.rb

DEAFULT_RETRIES = 3
MAX_ALLOWED_RETRIES = 10
MAX_REQUEST_RETRY_JITTER = 250
MAX_REQUEST_RETRY_DELAY = 1000
MIN_REQUEST_RETRY_DELAY = 100

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 = [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

MAX_ALLOWED_RETRIES.times { |attempt|
  p sleep_timer.call(attempt)
}

# => 1.0
# => 1.0
# => 1.0
# => 1.0
# => 1.0
# => 1.0
# => 1.0
# => 1.0
# => 1.0
# => 1.0

exponential backoff is not applied and wait is always 1.0.

What was the expected behavior?

if I comment some codes out, it will work as commented.

DEAFULT_RETRIES = 3
MAX_ALLOWED_RETRIES = 10
MAX_REQUEST_RETRY_JITTER = 250
MAX_REQUEST_RETRY_DELAY = 1000
MIN_REQUEST_RETRY_DELAY = 100

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 = [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

MAX_ALLOWED_RETRIES.times { |attempt|
  p sleep_timer.call(attempt)
}

# => 2.144
# => 4.171
# => 8.135
# => 16.049
# => 32.177
# => 64.19
# => 128.167
# => 256.134
# => 512.122
# => 1024.223

exponential backoff is applied.

luckpoint avatar Jul 14 '22 11:07 luckpoint

Thanks for the report @luckpoint, and thanks for your patience. Let me dive into it this week and make some changes.

stevehobbsdev avatar Aug 02 '22 21:08 stevehobbsdev