Class: Logtide::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/logtide/retry_policy.rb

Overview

Decides whether a delivery failure is retryable and how long to wait (spec 002 section 6). Retryable: network errors and 408/429/5xx; never any other 4xx. Backoff is exponential with full jitter, never below the base.

Constant Summary collapse

RETRYABLE_STATUSES =
[408, 429, 500, 502, 503, 504].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base:, max_backoff:, max_retries:, rng: -> { rand }) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.



12
13
14
15
16
17
# File 'lib/logtide/retry_policy.rb', line 12

def initialize(base:, max_backoff:, max_retries:, rng: -> { rand })
  @base = base
  @max_backoff = max_backoff
  @max_retries = max_retries
  @rng = rng
end

Instance Attribute Details

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



10
11
12
# File 'lib/logtide/retry_policy.rb', line 10

def max_retries
  @max_retries
end

Instance Method Details

#delay_for(attempt, retry_after: nil) ⇒ Object

Seconds to wait before the next attempt. A server-provided Retry-After wins.



24
25
26
27
28
29
30
# File 'lib/logtide/retry_policy.rb', line 24

def delay_for(attempt, retry_after: nil)
  return retry_after if retry_after

  capped = [@base * (2**attempt), @max_backoff].min
  low = [@base, capped].min
  low + (@rng.call * (capped - low))
end

#retryable_status?(status) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/logtide/retry_policy.rb', line 19

def retryable_status?(status)
  RETRYABLE_STATUSES.include?(status)
end