Module: Velix::Retry

Included in:
Client
Defined in:
lib/velix/retry.rb

Constant Summary collapse

RETRYABLE_STATUSES =
[429, 503].freeze
IDEMPOTENT_METHODS =
%w[GET].freeze

Instance Method Summary collapse

Instance Method Details

#with_retry(max_retries:, method: "GET") ⇒ Object

Retries are only safe for:

  • idempotent HTTP methods (GET), regardless of status
  • RETRYABLE_STATUSES (429/503), where the server guarantees the request was not processed, even for non-idempotent methods (POST)

Any other 5xx on a non-idempotent method (POST/PATCH/PUT/DELETE) is raised immediately — retrying could duplicate a checkin/enroll.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/velix/retry.rb', line 15

def with_retry(max_retries:, method: "GET")
  attempts = 0
  begin
    attempts += 1
    yield
  rescue RateLimitError, ServerError => e
    raise unless retryable?(method, e.status)
    raise if attempts >= max_retries

    wait = (2**(attempts - 1)) + rand
    sleep(wait)
    retry
  end
end