lib/circuitbox/excon_middleware.rb in circuitbox-1.1.1 vs lib/circuitbox/excon_middleware.rb in circuitbox-2.0.0.pre1
- old
+ new
@@ -30,34 +30,34 @@
@opts = default_options.merge(opts)
super(stack)
end
def error_call(datum)
- circuit(datum).run!(run_options(datum)) do
+ circuit(datum).run do
raise RequestFailed
end
rescue Circuitbox::Error => exception
circuit_open_value(datum, datum[:response], exception)
end
def request_call(datum)
- circuit(datum).run!(run_options(datum)) do
+ circuit(datum).run do
@stack.request_call(datum)
end
end
def response_call(datum)
- circuit(datum).run!(run_options(datum)) do
+ circuit(datum).run do
raise RequestFailed if open_circuit?(datum[:response])
end
@stack.response_call(datum)
rescue Circuitbox::Error => exception
circuit_open_value(datum, datum[:response], exception)
end
def identifier
- @identifier ||= opts.fetch(:identifier, ->(env) { env[:path] })
+ @identifier ||= opts.fetch(:identifier, ->(env) { env[:host] })
end
def exceptions
circuit_breaker_options[:exceptions]
end
@@ -67,14 +67,10 @@
def circuit(datum)
id = identifier.respond_to?(:call) ? identifier.call(datum) : identifier
circuitbox.circuit id, circuit_breaker_options
end
- def run_options(datum)
- opts.merge(datum)[:circuit_breaker_run_options] || {}
- end
-
def open_circuit?(response)
opts[:open_circuit].call(response)
end
def circuitbox
@@ -84,28 +80,23 @@
def circuit_open_value(env, response, exception)
env[:circuit_breaker_default_value] || default_value.call(response, exception)
end
def circuit_breaker_options
- return @circuit_breaker_options if @circuit_breaker_options
-
- @circuit_breaker_options = opts.fetch(:circuit_breaker_options, {})
- @circuit_breaker_options.merge!(
- exceptions: opts.fetch(:exceptions, DEFAULT_EXCEPTIONS)
- )
+ @circuit_breaker_options ||= begin
+ options = opts.fetch(:circuit_breaker_options, {})
+ options.merge!(
+ exceptions: opts.fetch(:exceptions, DEFAULT_EXCEPTIONS)
+ )
+ end
end
def default_value
- return @default_value if @default_value
-
- default = opts.fetch(:default_value) do
- lambda { |response, exception| NullResponse.new(response, exception) }
+ @default_value ||= begin
+ default = opts.fetch(:default_value) do
+ lambda { |response, exception| NullResponse.new(response, exception) }
+ end
+ default.respond_to?(:call) ? default : lambda { |*| default }
end
-
- @default_value = if default.respond_to?(:call)
- default
- else
- lambda { |*| default }
- end
end
end
end