Class: HTTP::Retriable::DelayCalculator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/http/retriable/delay_calculator.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

RFC2822_DATE_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/^
  (?:Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s+
  (?:0[1-9]|[1-2]?[0-9]|3[01])\s+
  (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+
  (?:19[0-9]{2}|[2-9][0-9]{3})\s+
  (?:2[0-3]|[0-1][0-9]):(?:[0-5][0-9]):(?:60|[0-5][0-9])\s+
  GMT
$/x

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ DelayCalculator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DelayCalculator.



7
8
9
10
11
12
13
14
# File 'lib/http/retriable/delay_calculator.rb', line 7

def initialize(opts)
  @max_delay = opts.fetch(:max_delay, Float::MAX).to_f
  if (delay = opts[:delay]).respond_to?(:call)
    @delay_proc = opts.fetch(:delay)
  else
    @delay = delay
  end
end

Instance Method Details

#calculate_delay_from_iteration(iteration) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/http/retriable/delay_calculator.rb', line 47

def calculate_delay_from_iteration(iteration)
  if @delay_proc
    @delay_proc.call(iteration)
  elsif @delay
    @delay
  else
    delay = (2**(iteration - 1)) - 1
    delay_noise = rand
    delay + delay_noise
  end
end

#call(iteration, response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
19
20
21
22
23
24
# File 'lib/http/retriable/delay_calculator.rb', line 16

def call(iteration, response)
  delay = if response && (retry_header = response.headers["Retry-After"])
            delay_from_retry_header(retry_header)
          else
            calculate_delay_from_iteration(iteration)
          end

  ensure_dealy_in_bounds(delay)
end

#delay_from_retry_header(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Spec for Retry-After header https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After



37
38
39
40
41
42
43
44
45
# File 'lib/http/retriable/delay_calculator.rb', line 37

def delay_from_retry_header(value)
  value = value.to_s.strip

  case value
  when RFC2822_DATE_REGEX then DateTime.rfc2822(value).to_time - Time.now.utc
  when /^\d+$/            then value.to_i
  else 0
  end
end

#ensure_dealy_in_bounds(delay) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



59
60
61
# File 'lib/http/retriable/delay_calculator.rb', line 59

def ensure_dealy_in_bounds(delay)
  delay.clamp(0, @max_delay)
end