-
Notifications
You must be signed in to change notification settings - Fork 3
Retry N times with progressive backoffs before Fail
Paul Bergeron edited this page May 2, 2013
·
2 revisions
Rhod supports retying up to N times. By default it uses a logarithmic backoff:
backoff = Rhod::Backoffs::Logarithmic.new(1.3)
backoffs = []; 5.times { backoffs << backoff.next }; backoffs
# => [0.7570232465074598, 2.403267722339301, 3.444932048942182, 4.208673319629471, 4.811984719351674]
Rhod also comes with exponential, random and constant backoffs. You can also subclass Rhod::Backoffs::Backoff to create your own. See lib/rhod/backoffs/ for examples.
Example, open a remote reasource, fail once it has failed 10 times, with the default (logarithmic) backoff explictly stated:
require 'open-uri'
require 'rhod'
Rhod.create_profile(:retry_logarithmic, retries: 10, backoffs: :l)
Rhod.with_retry_logarithmic do
open("http://google.com").read
end
Example, open a remote reasource, fail once it has failed 10 times, waiting 0.2 seconds between attempts:
require 'open-uri'
require 'rhod'
Rhod.create_profile(:retry_constant, retries: 10, backoffs: 0.2)
Rhod.with_retry_constant do
open("http://google.com").read
end
Example, open a remote reasource, fail once it has failed 10 times, with an exponetially growing wait time between attempts:
require 'open-uri'
require 'rhod'
Rhod.create_profile(:retry_exponetially, retries: 10, backoffs: :^)
Rhod.with_retry_exponetially do
open("http://google.com").read
end
Example, open a remote reasource, fail once it has failed 10 times, with a random wait between 1 and 5 seconds on each attempt
require 'open-uri'
require 'rhod'
Rhod.create_profile(:retry_randomly, retries: 10, backoffs: 'r1..5')
Rhod.with_retry_randomly do
open("http://google.com").read
end