Skip to content

Instantly share code, notes, and snippets.

@masroorhussainv
Created February 21, 2022 11:04
Show Gist options
  • Save masroorhussainv/e0b912ad165256fddb276f3d882d6808 to your computer and use it in GitHub Desktop.
Save masroorhussainv/e0b912ad165256fddb276f3d882d6808 to your computer and use it in GitHub Desktop.
Exponential Backoff Retrying Algorithm in Ruby [modified] - Polished Ruby Programming by Jeremy Evans
require 'net/http'
require 'uri'
uri = URI("http://example.local/file")
retries = 0
begin
Net::HTTP.get_response(uri)
rescue SocketError, SystemCallError, Net::HTTPBadResponse
retries += 1
raise if retries > 3
sleep(3 * (0.5 + rand/2) * 1.5**(retries-1))
retry
end
@masroorhussainv
Copy link
Author

With this approach, even with 10 retries, all retries will complete within 3 minutes.

pg-158, pg-159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment