Class: Backoff
- Inherits:
-
Delegator
show all
- Defined in:
- lib/backoff.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(object, exception_classes, logger, options = {}) ⇒ Backoff
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/backoff.rb', line 8
def initialize(object, exception_classes, logger, options = {})
@exception_classes = exception_classes
@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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
23
24
25
|
# File 'lib/backoff.rb', line 23
def method_missing(sym, *args, &block)
_with_backoff { super }
end
|
Class Method Details
.wrap(object, exception_classes, logger, options) ⇒ Object
3
4
5
6
|
# File 'lib/backoff.rb', line 3
def self.wrap(object, exception_classes, logger, options)
return object if object.is_a?(self.class)
new(object, exception_classes, logger, options)
end
|
Instance Method Details
#__getobj__ ⇒ Object
19
20
21
|
# File 'lib/backoff.rb', line 19
def __getobj__
@delegate_sd_obj
end
|
#_with_backoff(backoff = @initial_backoff) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/backoff.rb', line 27
def _with_backoff(backoff = @initial_backoff)
yield
rescue *@exception_classes => e
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
|