lib/httpx/plugins/circuit_breaker.rb in httpx-0.23.4 vs lib/httpx/plugins/circuit_breaker.rb in httpx-0.24.0
- old
+ new
@@ -29,10 +29,20 @@
def initialize_dup(orig)
super
@circuit_store = orig.instance_variable_get(:@circuit_store).dup
end
+ %i[circuit_open].each do |meth|
+ class_eval(<<-MOD, __FILE__, __LINE__ + 1)
+ def on_#{meth}(&blk) # def on_circuit_open(&blk)
+ on(:#{meth}, &blk) # on(:circuit_open, &blk)
+ end # end
+ MOD
+ end
+
+ private
+
def send_requests(*requests)
# @type var short_circuit_responses: Array[response]
short_circuit_responses = []
# run all requests through the circuit breaker, see if the circuit is
@@ -57,21 +67,25 @@
short_circuit_responses
end
def on_response(request, response)
+ emit(:circuit_open, request) if try_circuit_open(request, response)
+
+ super
+ end
+
+ def try_circuit_open(request, response)
if response.is_a?(ErrorResponse)
case response.error
when RequestTimeoutError
@circuit_store.try_open(request.uri, response)
else
@circuit_store.try_open(request.origin, response)
end
elsif (break_on = request.options.circuit_breaker_break_on) && break_on.call(response)
@circuit_store.try_open(request.uri, response)
end
-
- super
end
end
module OptionsMethods
def option_circuit_breaker_max_attempts(value)