lib/httpx/plugins/circuit_breaker.rb in httpx-0.24.7 vs lib/httpx/plugins/circuit_breaker.rb in httpx-1.0.0

- old
+ new

@@ -14,12 +14,16 @@ require_relative "circuit_breaker/circuit" require_relative "circuit_breaker/circuit_store" end def self.extra_options(options) - options.merge(circuit_breaker_max_attempts: 3, circuit_breaker_reset_attempts_in: 60, circuit_breaker_break_in: 60, - circuit_breaker_half_open_drip_rate: 1) + options.merge( + circuit_breaker_max_attempts: 3, + circuit_breaker_reset_attempts_in: 60, + circuit_breaker_break_in: 60, + circuit_breaker_half_open_drip_rate: 1 + ) end module InstanceMethods def initialize(*) super @@ -45,17 +49,17 @@ # @type var short_circuit_responses: Array[response] short_circuit_responses = [] # run all requests through the circuit breaker, see if the circuit is # open for any of them. - real_requests = requests.each_with_object([]) do |req, real_reqs| + real_requests = requests.each_with_index.with_object([]) do |(req, idx), real_reqs| short_circuit_response = @circuit_store.try_respond(req) if short_circuit_response.nil? real_reqs << req next end - short_circuit_responses[requests.index(req)] = short_circuit_response + short_circuit_responses[idx] = short_circuit_response end # run requests for the remainder unless real_requests.empty? responses = super(*real_requests) @@ -82,9 +86,12 @@ 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) + else + @circuit_store.try_close(request.uri) + nil end end end module OptionsMethods