Class: Rack::Idempotent

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-idempotent.rb,
lib/rack-idempotent/version.rb

Defined Under Namespace

Classes: HTTPException, RetryLimitExceeded

Constant Summary collapse

RETRY_LIMIT =
5
IDEMPOTENT_HTTP_CODES =
[502, 503, 504, 408]
IDEMPOTENT_ERROR_CLASSES =
[Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH]
VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Idempotent

Returns a new instance of Idempotent.



27
28
29
# File 'lib/rack-idempotent.rb', line 27

def initialize(app)
  @app= app
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rack-idempotent.rb', line 31

def call(env)
  env['client.retries'] = 0
  status, headers, body = nil
  idempotent_exceptions = []
  begin
    dup_env = env.dup
    status, headers, body = @app.call(dup_env)
    raise HTTPException.new(status, headers, body) if IDEMPOTENT_HTTP_CODES.include?(status)
    env.merge!(dup_env)
    [status, headers, body]
  rescue *IDEMPOTENT_ERROR_CLASSES, HTTPException => ie
    idempotent_exceptions << ie
    if env['client.retries'] > RETRY_LIMIT - 1
      raise(RetryLimitExceeded.new(idempotent_exceptions))
    else
      env['client.retries'] += 1
      retry
    end
  end
end