Class: HTTP::Retriable::DelayCalculator Private

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

Overview

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.

Calculates retry delays with support for Retry-After headers

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.

Pattern matching RFC 2822 formatted dates in Retry-After headers

/^
  (?: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(delay: nil, max_delay: Float::MAX) ⇒ HTTP::Retriable::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.

Initializes the delay calculator

Parameters:

  • delay (#call, Numeric, nil) (defaults to: nil)

    delay value or proc

  • max_delay (#to_f) (defaults to: Float::MAX)

    maximum delay cap



14
15
16
17
18
19
20
21
# File 'lib/http/retriable/delay_calculator.rb', line 14

def initialize(delay: nil, max_delay: Float::MAX)
  @max_delay = Float(max_delay)
  if delay.respond_to?(:call)
    @delay_proc = delay
  else
    @delay = delay
  end
end

Instance Method Details

#calculate_delay_from_iteration(iteration) ⇒ Numeric

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.

Calculates delay based on iteration number

Parameters:

  • iteration (Integer)

Returns:

  • (Numeric)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/http/retriable/delay_calculator.rb', line 69

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) ⇒ Numeric

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.

Calculates delay for the given iteration

Parameters:

Returns:

  • (Numeric)


29
30
31
32
33
34
35
36
37
# File 'lib/http/retriable/delay_calculator.rb', line 29

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_delay_in_bounds(delay)
end

#delay_from_retry_header(value) ⇒ Numeric

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.

Parses delay from Retry-After header value

Parameters:

  • value (String)

Returns:

  • (Numeric)


54
55
56
57
58
59
60
61
62
# File 'lib/http/retriable/delay_calculator.rb', line 54

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

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

#ensure_delay_in_bounds(delay) ⇒ Numeric

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.

Clamps delay to configured bounds

Parameters:

  • delay (Numeric)

Returns:

  • (Numeric)


86
87
88
# File 'lib/http/retriable/delay_calculator.rb', line 86

def ensure_delay_in_bounds(delay)
  Float(delay.clamp(0, @max_delay))
end