lib/backoff.rb in backoff-0.1.1 vs lib/backoff.rb in backoff-0.2.0
- old
+ new
@@ -10,10 +10,12 @@
@delegate_sd_obj = object
@logger = logger
@sleeper = options[:sleeper] || Kernel.method(:sleep)
@initial_backoff = options[:initial_backoff] || 1
@multiplier = options[:multiplier] || 2
+ @random = options[:random] || Random.new
+ @jitter = options[:jitter] || lambda {|delay| @random.rand(0..delay.to_f) }
end
def __getobj__
@delegate_sd_obj
end
@@ -23,10 +25,11 @@
end
def _with_backoff(backoff = @initial_backoff)
yield
rescue *@exception_classes => e
- @logger.error "Got #{e.class}, sleeping #{backoff}"
+ jittered_backoff = @jitter.call(backoff)
+ @logger.error "Got #{e.class}, sleeping #{jittered_backoff}"
@sleeper.call(backoff)
@logger.info "Woke up after #{e.class} retrying again"
backoff *= @multiplier
retry
end