Class: HTTPX::Plugins::CircuitBreaker::CircuitStore

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/plugins/circuit_breaker/circuit_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CircuitStore

Returns a new instance of CircuitStore.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/httpx/plugins/circuit_breaker/circuit_store.rb', line 7

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

Instance Method Details

#try_close(uri) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/httpx/plugins/circuit_breaker/circuit_store.rb', line 25

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

#try_open(uri, response) ⇒ Object



19
20
21
22
23
# File 'lib/httpx/plugins/circuit_breaker/circuit_store.rb', line 19

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

  circuit.try_open(response)
end

#try_respond(request) ⇒ Object

if circuit is open, it’ll respond with the stored response. if not, nil.



37
38
39
40
41
# File 'lib/httpx/plugins/circuit_breaker/circuit_store.rb', line 37

def try_respond(request)
  circuit = @circuits_mutex.synchronize { get_circuit_for_uri(request.uri) }

  circuit.respond
end