Class: ZZQ::Engine::SocketLifecycle
- Inherits:
-
Object
- Object
- ZZQ::Engine::SocketLifecycle
- Defined in:
- lib/zzq/engine/socket_lifecycle.rb
Overview
Owns the socket-level state: ‘:new → :open → :closing → :closed`.
Adapted from NNQ::Engine::SocketLifecycle, minus the shared-IO- thread fallback (ZZQ is pure-Async).
Defined Under Namespace
Classes: InvalidTransition
Constant Summary collapse
- STATES =
%i[new open closing closed].freeze
- TRANSITIONS =
{ new: %i[open closed].freeze, open: %i[closing closed].freeze, closing: %i[closed].freeze, closed: [].freeze, }.freeze
Instance Attribute Summary collapse
-
#all_peers_gone ⇒ Object
readonly
Returns the value of attribute all_peers_gone.
-
#barrier ⇒ Object
readonly
Returns the value of attribute barrier.
-
#parent_task ⇒ Object
readonly
Returns the value of attribute parent_task.
-
#peer_connected ⇒ Object
readonly
Returns the value of attribute peer_connected.
-
#reconnect_enabled ⇒ Object
Returns the value of attribute reconnect_enabled.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#capture_parent_task ⇒ Object
Must be called from within an Async::Task.
- #closed? ⇒ Boolean
- #closing? ⇒ Boolean
- #finish_closing! ⇒ Object
-
#initialize ⇒ SocketLifecycle
constructor
A new instance of SocketLifecycle.
- #open? ⇒ Boolean
- #resolve_all_peers_gone_if_empty(connections) ⇒ Object
- #start_closing! ⇒ Object
Constructor Details
#initialize ⇒ SocketLifecycle
Returns a new instance of SocketLifecycle.
33 34 35 36 37 38 39 40 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 33 def initialize @state = :new @parent_task = nil @peer_connected = Async::Promise.new @all_peers_gone = Async::Promise.new @reconnect_enabled = true @barrier = nil end |
Instance Attribute Details
#all_peers_gone ⇒ Object (readonly)
Returns the value of attribute all_peers_gone.
28 29 30 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 28 def all_peers_gone @all_peers_gone end |
#barrier ⇒ Object (readonly)
Returns the value of attribute barrier.
29 30 31 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 29 def @barrier end |
#parent_task ⇒ Object (readonly)
Returns the value of attribute parent_task.
26 27 28 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 26 def parent_task @parent_task end |
#peer_connected ⇒ Object (readonly)
Returns the value of attribute peer_connected.
27 28 29 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 27 def peer_connected @peer_connected end |
#reconnect_enabled ⇒ Object
Returns the value of attribute reconnect_enabled.
30 31 32 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 30 def reconnect_enabled @reconnect_enabled end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
25 26 27 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 25 def state @state end |
Instance Method Details
#alive? ⇒ Boolean
46 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 46 def alive? = @state == :new || @state == :open |
#capture_parent_task ⇒ Object
Must be called from within an Async::Task. Raises NotInAsyncContext otherwise.
51 52 53 54 55 56 57 58 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 51 def capture_parent_task return false if @parent_task task = Async::Task.current? or raise NotInAsyncContext @parent_task = task @barrier = Async::Barrier.new(parent: @parent_task) transition!(:open) true end |
#closed? ⇒ Boolean
45 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 45 def closed? = @state == :closed |
#closing? ⇒ Boolean
44 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 44 def closing? = @state == :closing |
#finish_closing! ⇒ Object
62 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 62 def finish_closing! = transition!(:closed) |
#open? ⇒ Boolean
43 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 43 def open? = @state == :open |
#resolve_all_peers_gone_if_empty(connections) ⇒ Object
65 66 67 68 69 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 65 def resolve_all_peers_gone_if_empty(connections) return unless @peer_connected.resolved? && connections.empty? return if @all_peers_gone.resolved? @all_peers_gone.resolve(true) end |
#start_closing! ⇒ Object
61 |
# File 'lib/zzq/engine/socket_lifecycle.rb', line 61 def start_closing! = transition!(:closing) |