Class: KubeMQ::Transport::ConnectionStateMachine Private
- Inherits:
-
Object
- Object
- KubeMQ::Transport::ConnectionStateMachine
- Defined in:
- lib/kubemq/transport/state_machine.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class is thread-safe. All state reads and transitions are mutex-protected.
Finite state machine governing the gRPC connection lifecycle.
Tracks transitions between ConnectionState values and fires registered callbacks on state changes. Invalid transitions raise Error.
Valid transition graph:
IDLE -> CONNECTING, CLOSED
CONNECTING -> READY, CLOSED, RECONNECTING, IDLE
READY -> RECONNECTING, CLOSED
RECONNECTING -> CONNECTING, READY, CLOSED
CLOSED -> (terminal)
Constant Summary collapse
- VALID_TRANSITIONS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Map of each state to its permitted successor states.
{ ConnectionState::IDLE => [ConnectionState::CONNECTING, ConnectionState::CLOSED], ConnectionState::CONNECTING => [ConnectionState::READY, ConnectionState::CLOSED, ConnectionState::RECONNECTING, ConnectionState::IDLE], ConnectionState::READY => [ConnectionState::RECONNECTING, ConnectionState::CLOSED], ConnectionState::RECONNECTING => [ConnectionState::CONNECTING, ConnectionState::READY, ConnectionState::CLOSED], ConnectionState::CLOSED => [] }.freeze
Instance Attribute Summary collapse
-
#state ⇒ Integer
readonly
private
The current ConnectionState value.
Instance Method Summary collapse
-
#closed? ⇒ Boolean
private
Returns whether the connection has been permanently closed.
-
#current_state ⇒ Integer
private
Returns the current state with mutex synchronization.
-
#initialize ⇒ ConnectionStateMachine
constructor
private
Creates a new state machine starting in ConnectionState::IDLE.
-
#on_connected {|server_info| ... } ⇒ void
private
Registers a callback invoked when the connection reaches READY.
-
#on_disconnected {|reason| ... } ⇒ void
private
Registers a callback invoked when the connection reaches CLOSED.
-
#on_error {|error| ... } ⇒ void
private
Registers a callback invoked on invalid state transitions.
-
#on_reconnecting {|attempt| ... } ⇒ void
private
Registers a callback invoked when the connection enters RECONNECTING.
-
#ready? ⇒ Boolean
private
Returns whether the connection is in the READY state.
-
#transition!(new_state, **context) ⇒ void
private
Transitions to a new state, firing the appropriate callback.
Constructor Details
#initialize ⇒ ConnectionStateMachine
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new state machine starting in ConnectionState::IDLE.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kubemq/transport/state_machine.rb', line 40 def initialize @state = ConnectionState::IDLE @mutex = Mutex.new @callbacks = { on_connected: nil, on_disconnected: nil, on_reconnecting: nil, on_error: nil } end |
Instance Attribute Details
#state ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the current ConnectionState value.
37 38 39 |
# File 'lib/kubemq/transport/state_machine.rb', line 37 def state @state end |
Instance Method Details
#closed? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the connection has been permanently closed.
129 130 131 |
# File 'lib/kubemq/transport/state_machine.rb', line 129 def closed? current_state == ConnectionState::CLOSED end |
#current_state ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the current state with mutex synchronization.
115 116 117 |
# File 'lib/kubemq/transport/state_machine.rb', line 115 def current_state @mutex.synchronize { @state } end |
#on_connected {|server_info| ... } ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Registers a callback invoked when the connection reaches READY.
56 57 58 |
# File 'lib/kubemq/transport/state_machine.rb', line 56 def on_connected(&block) @mutex.synchronize { @callbacks[:on_connected] = block } end |
#on_disconnected {|reason| ... } ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Registers a callback invoked when the connection reaches CLOSED.
65 66 67 |
# File 'lib/kubemq/transport/state_machine.rb', line 65 def on_disconnected(&block) @mutex.synchronize { @callbacks[:on_disconnected] = block } end |
#on_error {|error| ... } ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Registers a callback invoked on invalid state transitions.
83 84 85 |
# File 'lib/kubemq/transport/state_machine.rb', line 83 def on_error(&block) @mutex.synchronize { @callbacks[:on_error] = block } end |
#on_reconnecting {|attempt| ... } ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Registers a callback invoked when the connection enters RECONNECTING.
74 75 76 |
# File 'lib/kubemq/transport/state_machine.rb', line 74 def on_reconnecting(&block) @mutex.synchronize { @callbacks[:on_reconnecting] = block } end |
#ready? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the connection is in the READY state.
122 123 124 |
# File 'lib/kubemq/transport/state_machine.rb', line 122 def ready? current_state == ConnectionState::READY end |
#transition!(new_state, **context) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Transitions to a new state, firing the appropriate callback.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/kubemq/transport/state_machine.rb', line 94 def transition!(new_state, **context) callback = nil @mutex.synchronize do valid = VALID_TRANSITIONS.fetch(@state, []) unless valid.include?(new_state) error = KubeMQ::Error.new("Invalid state transition from #{@state} to #{new_state}") err_cb = @callbacks[:on_error] err_cb&.call(error) raise error end old_state = @state @state = new_state callback = resolve_callback(new_state, old_state, context) end callback&.call end |