Class: CircuitBreaker::Shield

Inherits:
Object
  • Object
show all
Defined in:
lib/circuit_breaker-ruby/shield.rb

Defined Under Namespace

Modules: States

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Shield

Returns a new instance of Shield.



16
17
18
19
20
21
22
23
24
# File 'lib/circuit_breaker-ruby/shield.rb', line 16

def initialize(**options)
  @failure_count = 0
  @total_count = 0
  @failure_threshold = options.fetch(:failure_threshold, config.failure_threshold)
  @failure_threshold_percentage = options.fetch(:failure_threshold_percentage, config.failure_threshold_percentage)
  @invocation_timeout = options.fetch(:invocation_timeout, config.invocation_timeout)
  @retry_timeout = options.fetch(:retry_timeout, config.retry_timeout)
  @callback = options[:callback]
end

Instance Attribute Details

#failure_countObject (readonly)

Returns the value of attribute failure_count.



9
10
11
# File 'lib/circuit_breaker-ruby/shield.rb', line 9

def failure_count
  @failure_count
end

#failure_thresholdObject (readonly)

Returns the value of attribute failure_threshold.



9
10
11
# File 'lib/circuit_breaker-ruby/shield.rb', line 9

def failure_threshold
  @failure_threshold
end

#failure_threshold_percentageObject (readonly)

Returns the value of attribute failure_threshold_percentage.



9
10
11
# File 'lib/circuit_breaker-ruby/shield.rb', line 9

def failure_threshold_percentage
  @failure_threshold_percentage
end

#invocation_timeoutObject (readonly)

Returns the value of attribute invocation_timeout.



9
10
11
# File 'lib/circuit_breaker-ruby/shield.rb', line 9

def invocation_timeout
  @invocation_timeout
end

#retry_timeoutObject (readonly)

Returns the value of attribute retry_timeout.



9
10
11
# File 'lib/circuit_breaker-ruby/shield.rb', line 9

def retry_timeout
  @retry_timeout
end

#total_countObject (readonly)

Returns the value of attribute total_count.



9
10
11
# File 'lib/circuit_breaker-ruby/shield.rb', line 9

def total_count
  @total_count
end

Instance Method Details

#configObject



26
27
28
# File 'lib/circuit_breaker-ruby/shield.rb', line 26

def config
  CircuitBreaker.config
end

#protect(options = {}, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/circuit_breaker-ruby/shield.rb', line 34

def protect(options = {}, &block)
  update_config!(options)

  case prev_state = state
  when States::CLOSED, States::HALF_OPEN
    connect(&block)
  when States::OPEN
    raise CircuitBreaker::Open
  end
end

#update_config!(options) ⇒ Object



30
31
32
# File 'lib/circuit_breaker-ruby/shield.rb', line 30

def update_config!(options)
  CircuitBreaker::Config.update(self, options)
end