Class: Clowk::Http::RetryMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/clowk/http/retry_middleware.rb

Constant Summary collapse

RETRYABLE_ERRORS =
[
  EOFError,
  Errno::ECONNRESET,
  Errno::ETIMEDOUT,
  IOError,
  Net::OpenTimeout,
  Net::ReadTimeout,
  Net::WriteTimeout,
  SocketError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, logger: nil) ⇒ RetryMiddleware

Returns a new instance of RetryMiddleware.



19
20
21
22
# File 'lib/clowk/http/retry_middleware.rb', line 19

def initialize(app, logger: nil, **)
  @app = app
  @logger = logger || LoggerMiddleware::NullLogger.new
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/clowk/http/retry_middleware.rb', line 24

def call(env)
  attempts = env.fetch(:retry_attempts, 0)
  interval = env.fetch(:retry_interval, 0.0)
  current_attempt = 0

  begin
    current_attempt += 1
    env[:attempt] = current_attempt
    app.call(env)
  rescue *RETRYABLE_ERRORS => error
    raise error if current_attempt > attempts

    logger.info("[Clowk::Http] retry=#{current_attempt} error=#{error.class}")
    sleep(interval) if interval.positive?
    retry
  end
end