Sha256: 511a20ca6a0c3847ff8eb7895a0af56bcb3deddd5c19870399b4078f3e3007f7

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module HTTPX::Plugins::CircuitBreaker
  using HTTPX::URIExtensions

  class CircuitStore
    def initialize(options)
      @circuits = Hash.new do |h, k|
        h[k] = Circuit.new(
          options.circuit_breaker_max_attempts,
          options.circuit_breaker_reset_attempts_in,
          options.circuit_breaker_break_in,
          options.circuit_breaker_half_open_drip_rate
        )
      end
      @circuits_mutex = Thread::Mutex.new
    end

    def try_open(uri, response)
      circuit = @circuits_mutex.synchronize { get_circuit_for_uri(uri) }

      circuit.try_open(response)
    end

    def try_close(uri)
      circuit = @circuits_mutex.synchronize do
        return unless @circuits.key?(uri.origin) || @circuits.key?(uri.to_s)

        get_circuit_for_uri(uri)
      end

      circuit.try_close
    end

    # if circuit is open, it'll respond with the stored response.
    # if not, nil.
    def try_respond(request)
      circuit = @circuits_mutex.synchronize { get_circuit_for_uri(request.uri) }

      circuit.respond
    end

    private

    def get_circuit_for_uri(uri)
      if uri.respond_to?(:origin) && @circuits.key?(uri.origin)
        @circuits[uri.origin]
      else
        @circuits[uri.to_s]
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
httpx-1.3.4 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.3.3 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.3.2 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.3.1 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.3.0 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.2.6 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.2.4 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.2.3 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.2.2 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.2.1 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.2.0 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.1.5 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.1.4 lib/httpx/plugins/circuit_breaker/circuit_store.rb
httpx-1.1.3 lib/httpx/plugins/circuit_breaker/circuit_store.rb