Class: ZZQ::Engine::SocketLifecycle

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeSocketLifecycle

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_goneObject (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

#barrierObject (readonly)

Returns the value of attribute barrier.



29
30
31
# File 'lib/zzq/engine/socket_lifecycle.rb', line 29

def barrier
  @barrier
end

#parent_taskObject (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_connectedObject (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_enabledObject

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

#stateObject (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

Returns:

  • (Boolean)


46
# File 'lib/zzq/engine/socket_lifecycle.rb', line 46

def alive?   = @state == :new || @state == :open

#capture_parent_taskObject

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

Returns:

  • (Boolean)


45
# File 'lib/zzq/engine/socket_lifecycle.rb', line 45

def closed?  = @state == :closed

#closing?Boolean

Returns:

  • (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

Returns:

  • (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)