Skip to content

Instantly share code, notes, and snippets.

@minademian
Forked from khamidou/exponential_backoff.rb
Created April 21, 2022 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minademian/12e19c69619d745c58842d1d4d3579b3 to your computer and use it in GitHub Desktop.
Save minademian/12e19c69619d745c58842d1d4d3579b3 to your computer and use it in GitHub Desktop.
Exponential backoff with jitter in Ruby
# Exponential backoff in Ruby
begin
make_request
rescue RequestError => e
if retries <= max_retries
retries += 1
sleep 2 ** retries + rand(20)
retry
else
raise "Timeout: #{e.message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment