Created
February 21, 2022 11:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pg-158, pg-159