Class: BreakerMachines::Storage::BackendState

Inherits:
Object
  • Object
show all
Defined in:
lib/breaker_machines/storage/backend_state.rb

Overview

Manages the health state of a single storage backend using a state machine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, threshold:, timeout:) ⇒ BackendState

Returns a new instance of BackendState.



10
11
12
13
14
15
16
17
# File 'lib/breaker_machines/storage/backend_state.rb', line 10

def initialize(name, threshold:, timeout:)
  @name = name
  @threshold = threshold
  @timeout = timeout
  @failure_count = 0
  @last_failure_at = nil
  @health = :healthy
end

Instance Attribute Details

#failure_countObject (readonly)

Returns the value of attribute failure_count.



7
8
9
# File 'lib/breaker_machines/storage/backend_state.rb', line 7

def failure_count
  @failure_count
end

#healthObject

Returns the value of attribute health.



8
9
10
# File 'lib/breaker_machines/storage/backend_state.rb', line 8

def health
  @health
end

#last_failure_atObject (readonly)

Returns the value of attribute last_failure_at.



7
8
9
# File 'lib/breaker_machines/storage/backend_state.rb', line 7

def last_failure_at
  @last_failure_at
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/breaker_machines/storage/backend_state.rb', line 7

def name
  @name
end

Instance Method Details

#record_failureObject



44
45
46
47
48
# File 'lib/breaker_machines/storage/backend_state.rb', line 44

def record_failure
  @failure_count += 1
  @last_failure_at = BreakerMachines.monotonic_time
  trip
end

#threshold_reached?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/breaker_machines/storage/backend_state.rb', line 50

def threshold_reached?
  @failure_count >= @threshold
end

#unhealthy_due_to_timeout?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/breaker_machines/storage/backend_state.rb', line 54

def unhealthy_due_to_timeout?
  return false unless unhealthy?

  unhealthy_until = instance_variable_get(:@unhealthy_until)
  return false unless unhealthy_until

  if BreakerMachines.monotonic_time > unhealthy_until
    recover
    false
  else
    true
  end
end