Class: Brow::BackoffPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/brow/backoff_policy.rb

Constant Summary collapse

MIN_TIMEOUT_MS =

Private: The default minimum timeout between intervals in milliseconds.

100
MAX_TIMEOUT_MS =

Private: The default maximum timeout between intervals in milliseconds.

10_000
MULTIPLIER =

Private: The value to multiply the current interval with for each retry attempt.

1.5
RANDOMIZATION_FACTOR =

Private: The randomization factor to use to create a range around the retry interval.

0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BackoffPolicy

Public: Create new instance of backoff policy.

options - The Hash of options.

:min_timeout_ms - The minimum backoff timeout.
:max_timeout_ms - The maximum backoff timeout.
:multiplier - The value to multiply the current interval with for each
              retry attempt.
:randomization_factor - The randomization factor to use to create a range
                        around the retry interval.


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/brow/backoff_policy.rb', line 34

def initialize(options = {})
  @min_timeout_ms = options.fetch(:min_timeout_ms) {
    ENV.fetch("BROW_BACKOFF_MIN_TIMEOUT_MS", MIN_TIMEOUT_MS).to_i
  }
  @max_timeout_ms = options.fetch(:max_timeout_ms) {
    ENV.fetch("BROW_BACKOFF_MAX_TIMEOUT_MS", MAX_TIMEOUT_MS).to_i
  }
  @multiplier = options.fetch(:multiplier) {
    ENV.fetch("BROW_BACKOFF_MULTIPLIER", MULTIPLIER).to_f
  }
  @randomization_factor = options.fetch(:randomization_factor) {
    ENV.fetch("BROW_BACKOFF_RANDOMIZATION_FACTOR", RANDOMIZATION_FACTOR).to_f
  }

  unless @min_timeout_ms >= 0
    raise ArgumentError, ":min_timeout_ms must be >= 0 but was #{@min_timeout_ms.inspect}"
  end

  unless @max_timeout_ms >= 0
    raise ArgumentError, ":max_timeout_ms must be >= 0 but was #{@max_timeout_ms.inspect}"
  end

  unless @min_timeout_ms <= max_timeout_ms
    raise ArgumentError, ":min_timeout_ms (#{@min_timeout_ms.inspect}) must be <= :max_timeout_ms (#{@max_timeout_ms.inspect})"
  end

  @attempts = 0
end

Instance Attribute Details

#attemptsObject (readonly)

Private



23
24
25
# File 'lib/brow/backoff_policy.rb', line 23

def attempts
  @attempts
end

#max_timeout_msObject (readonly)

Private



20
21
22
# File 'lib/brow/backoff_policy.rb', line 20

def max_timeout_ms
  @max_timeout_ms
end

#min_timeout_msObject (readonly)

Private



20
21
22
# File 'lib/brow/backoff_policy.rb', line 20

def min_timeout_ms
  @min_timeout_ms
end

#multiplierObject (readonly)

Private



20
21
22
# File 'lib/brow/backoff_policy.rb', line 20

def multiplier
  @multiplier
end

#randomization_factorObject (readonly)

Private



20
21
22
# File 'lib/brow/backoff_policy.rb', line 20

def randomization_factor
  @randomization_factor
end

Instance Method Details

#next_intervalObject

Public: Returns the next backoff interval in milliseconds.



64
65
66
67
68
69
70
71
# File 'lib/brow/backoff_policy.rb', line 64

def next_interval
  interval = @min_timeout_ms * (@multiplier**@attempts)
  interval = add_jitter(interval, @randomization_factor)

  @attempts += 1

  [interval, @max_timeout_ms].min
end

#resetObject



73
74
75
# File 'lib/brow/backoff_policy.rb', line 73

def reset
  @attempts = 0
end