Module: Uploadcare::Internal::ThrottleHandler
- Included in:
- Api::Rest, Api::Upload
- Defined in:
- lib/uploadcare/internal/throttle_handler.rb
Overview
Handles API rate limiting (throttling) with automatic retry.
This module is included in API base classes to provide automatic retry logic when the API returns a throttle error (HTTP 429). It respects the retry-after header (via ThrottleError#timeout) and implements exponential backoff.
Instance Method Summary collapse
-
#handle_throttling(max_attempts: nil) { ... } ⇒ Object
Execute a block with automatic retry on throttle errors.
Instance Method Details
#handle_throttling(max_attempts: nil) { ... } ⇒ Object
Execute a block with automatic retry on throttle errors.
Wraps an HTTP request and automatically retries if a ThrottleError is raised. Sleep duration between retries is determined by the error's timeout value with exponential backoff.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/uploadcare/internal/throttle_handler.rb', line 21 def handle_throttling(max_attempts: nil) attempts = max_attempts if attempts.nil? attempts = respond_to?(:config) ? config.max_throttle_attempts : Uploadcare.configuration.max_throttle_attempts end attempts = attempts.to_i raise ArgumentError, 'max_attempts must be at least 1' if attempts < 1 (attempts - 1).times do |index| return yield rescue Uploadcare::Exception::ThrottleError => e sleep(e.timeout * (2**index)) end yield end |