Class: Circuitbox::ExconMiddleware

Inherits:
Excon::Middleware::Base
  • Object
show all
Defined in:
lib/circuitbox/excon_middleware.rb

Defined Under Namespace

Classes: NullResponse, RequestFailed

Constant Summary collapse

DEFAULT_EXCEPTIONS =
[
  Excon::Errors::Timeout,
  RequestFailed
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack, opts = {}) ⇒ ExconMiddleware

Returns a new instance of ExconMiddleware.



29
30
31
32
33
34
# File 'lib/circuitbox/excon_middleware.rb', line 29

def initialize(stack, opts = {})
  @stack = stack
  default_options = { open_circuit: ->(response) { response[:status] >= 400 } }
  @opts = default_options.merge(opts)
  super(stack)
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



27
28
29
# File 'lib/circuitbox/excon_middleware.rb', line 27

def opts
  @opts
end

Instance Method Details

#error_call(datum) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/circuitbox/excon_middleware.rb', line 36

def error_call(datum)
  circuit(datum).run do
    raise RequestFailed
  end
rescue Circuitbox::Error => e
  circuit_open_value(datum, datum[:response], e)
end

#exceptionsObject



63
64
65
# File 'lib/circuitbox/excon_middleware.rb', line 63

def exceptions
  circuit_breaker_options[:exceptions]
end

#identifierObject



59
60
61
# File 'lib/circuitbox/excon_middleware.rb', line 59

def identifier
  @identifier ||= opts.fetch(:identifier, ->(env) { env[:host] })
end

#request_call(datum) ⇒ Object



44
45
46
47
48
# File 'lib/circuitbox/excon_middleware.rb', line 44

def request_call(datum)
  circuit(datum).run do
    @stack.request_call(datum)
  end
end

#response_call(datum) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/circuitbox/excon_middleware.rb', line 50

def response_call(datum)
  circuit(datum).run do
    raise RequestFailed if open_circuit?(datum[:response])
  end
  @stack.response_call(datum)
rescue Circuitbox::Error => e
  circuit_open_value(datum, datum[:response], e)
end