Class: Stoplight::Infrastructure::Memory::Storage::State
- Inherits:
-
Object
- Object
- Stoplight::Infrastructure::Memory::Storage::State
- Defined in:
- lib/stoplight/infrastructure/memory/storage/state.rb
Overview
Thread-safe in-memory state storage for a single light.
Manages light state transitions and ensures notification deduplication through atomic transition detection. Each transition method returns whether the calling thread was first to trigger that transition, enabling exactly-once notification semantics.
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(clock:, cool_off_time:) ⇒ State
constructor
A new instance of State.
- #set_state(state) ⇒ Object
- #state_snapshot ⇒ Object
-
#transition_to_color(color) ⇒ Object
Combined method that performs the state transition based on color.
Constructor Details
Instance Method Details
#clear ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/stoplight/infrastructure/memory/storage/state.rb', line 74 def clear mutex.synchronize do self.locked_state = Stoplight::State::UNLOCKED self.recovered_at = nil self.recovery_scheduled_after = nil self.breached_at = nil self.recovery_started_at = nil end end |
#set_state(state) ⇒ Object
39 40 41 42 43 |
# File 'lib/stoplight/infrastructure/memory/storage/state.rb', line 39 def set_state(state) mutex.synchronize do self.locked_state = state end end |
#state_snapshot ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/stoplight/infrastructure/memory/storage/state.rb', line 45 def state_snapshot mutex.synchronize do Domain::StateSnapshot.new( time: clock.current_time, locked_state:, recovery_scheduled_after:, recovery_started_at:, breached_at: ) end end |
#transition_to_color(color) ⇒ Object
Combined method that performs the state transition based on color
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/stoplight/infrastructure/memory/storage/state.rb', line 61 def transition_to_color(color) case color when Color::GREEN transition_to_green when Color::YELLOW transition_to_yellow when Color::RED transition_to_red else raise ArgumentError, "Invalid color: #{color}" end end |