Class: Notion::Middleware::Retry
- Inherits:
-
Object
- Object
- Notion::Middleware::Retry
- Defined in:
- lib/notion/middleware/retry.rb
Constant Summary collapse
- RETRYABLE =
[429, 529].freeze
- IDEMPOTENT_RETRYABLE =
[500, 502, 503, 504].freeze
Instance Method Summary collapse
- #call(request) ⇒ Object
-
#initialize(app, max_attempts:, cap:, sleeper: Kernel.method(:sleep), random: Random.new) ⇒ Retry
constructor
A new instance of Retry.
Constructor Details
#initialize(app, max_attempts:, cap:, sleeper: Kernel.method(:sleep), random: Random.new) ⇒ Retry
Returns a new instance of Retry.
9 10 11 12 13 14 15 |
# File 'lib/notion/middleware/retry.rb', line 9 def initialize(app, max_attempts:, cap:, sleeper: Kernel.method(:sleep), random: Random.new) @app = app @max_attempts = max_attempts @cap = cap @sleeper = sleeper @random = random end |
Instance Method Details
#call(request) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/notion/middleware/retry.rb', line 17 def call(request) attempt = 0 loop do attempt += 1 response = @app.call(request) unless attempt < @max_attempts && retryable?(response, request) return response.with(attempt: attempt, retried: attempt > 1) end @sleeper.call(delay(response, attempt)) end end |