Class: Semian::CircuitBreaker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/semian/circuit_breaker.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:) ⇒ CircuitBreaker

Returns a new instance of CircuitBreaker.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/semian/circuit_breaker.rb', line 9

def initialize(name, exceptions:, success_threshold:, error_threshold:, error_timeout:, implementation:)
  @name = name.to_sym
  @success_count_threshold = success_threshold
  @error_count_threshold = error_threshold
  @error_timeout = error_timeout
  @exceptions = exceptions

  @errors = implementation::SlidingWindow.new(max_size: @error_count_threshold)
  @successes = implementation::Integer.new
  @state = implementation::State.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/semian/circuit_breaker.rb', line 7

def name
  @name
end

Instance Method Details

#acquireObject

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/semian/circuit_breaker.rb', line 21

def acquire
  return yield if disabled?

  half_open if open? && error_timeout_expired?

  raise OpenCircuitError unless request_allowed?

  result = nil
  begin
    result = yield
  rescue *@exceptions => error
    mark_failed(error)
    raise error
  else
    mark_success
  end
  result
end

#destroyObject



68
69
70
71
72
# File 'lib/semian/circuit_breaker.rb', line 68

def destroy
  @errors.destroy
  @successes.destroy
  @state.destroy
end

#mark_failed(_error) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/semian/circuit_breaker.rb', line 47

def mark_failed(_error)
  push_time(@errors)
  if closed?
    open if error_threshold_reached?
  elsif half_open?
    open
  end
end

#mark_successObject



56
57
58
59
60
# File 'lib/semian/circuit_breaker.rb', line 56

def mark_success
  return unless half_open?
  @successes.increment
  close if success_threshold_reached?
end

#request_allowed?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/semian/circuit_breaker.rb', line 40

def request_allowed?
  closed? ||
    half_open? ||
    # The circuit breaker is officially open, but it will transition to half-open on the next attempt.
    (open? && error_timeout_expired?)
end

#resetObject



62
63
64
65
66
# File 'lib/semian/circuit_breaker.rb', line 62

def reset
  @errors.clear
  @successes.reset
  close
end