module HTTPX::Plugins::CircuitBreaker::InstanceMethods

def initialize(*)

def initialize(*)
  super
  @circuit_store = CircuitStore.new(@options)
end

def initialize_dup(orig)

def initialize_dup(orig)
  super
  @circuit_store = orig.instance_variable_get(:@circuit_store).dup
end

def on_response(request, response)

def on_response(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

def send_requests(*requests)

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
  # open for any of them.
  real_requests = requests.each_with_object([]) do |req, 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
  end
  # run requests for the remainder
  unless real_requests.empty?
    responses = super(*real_requests)
    real_requests.each_with_index do |request, idx|
      short_circuit_responses[requests.index(request)] = responses[idx]
    end
  end
  short_circuit_responses
end