Class: OMQ::Routing::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/omq/routing/router.rb

Overview

ROUTER socket routing: identity-based routing.

Prepends peer identity frame on receive. Uses first frame as routing identity on send.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ Router

Returns a new instance of Router.

Parameters:



21
22
23
24
25
26
27
28
29
# File 'lib/omq/routing/router.rb', line 21

def initialize(engine)
  @engine                  = engine
  @recv_queue              = Routing.build_queue(engine.options.recv_hwm, :block)
  @connections_by_identity = {}
  @identity_by_connection  = {}
  @conn_queues             = {}  # connection => per-connection send queue
  @conn_send_tasks         = {}  # connection => send pump task
  @tasks                   = []
end

Instance Attribute Details

#recv_queueAsync::LimitedQueue (readonly)

Returns:

  • (Async::LimitedQueue)


16
17
18
# File 'lib/omq/routing/router.rb', line 16

def recv_queue
  @recv_queue
end

Instance Method Details

#connection_added(connection) ⇒ Object

Parameters:

  • connection (Connection)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/omq/routing/router.rb', line 52

def connection_added(connection)
  identity = connection.peer_identity
  identity = SecureRandom.bytes(5) if identity.nil? || identity.empty?
  @connections_by_identity[identity] = connection
  @identity_by_connection[connection] = identity

  task = @engine.start_recv_pump(connection, @recv_queue) { |msg| [identity, *msg] }
  @tasks << task if task

  q = Routing.build_queue(@engine.options.send_hwm, :block)
  @conn_queues[connection] = q
  @conn_send_tasks[connection] = ConnSendPump.start(@engine, connection, q, @tasks)
end

#connection_removed(connection) ⇒ Object

Parameters:

  • connection (Connection)


69
70
71
72
73
74
# File 'lib/omq/routing/router.rb', line 69

def connection_removed(connection)
  identity = @identity_by_connection.delete(connection)
  @connections_by_identity.delete(identity) if identity
  @conn_queues.delete(connection)
  @conn_send_tasks.delete(connection)&.stop
end

#dequeue_recvArray<String>?

Dequeues the next received message. Blocks until one is available.

Returns:

  • (Array<String>, nil)


36
37
38
# File 'lib/omq/routing/router.rb', line 36

def dequeue_recv
  @recv_queue.dequeue
end

#enqueue(parts) ⇒ Object

Enqueues a message for sending. The first frame is the routing identity.

Parameters:

  • parts (Array<String>)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omq/routing/router.rb', line 81

def enqueue(parts)
  identity = parts.first
  if @engine.options.router_mandatory?
    unless @connections_by_identity[identity]
      raise SocketError, "no route to identity #{identity.inspect}"
    end
  end
  conn = @connections_by_identity[identity]
  return unless conn  # silently drop if peer disconnected
  @conn_queues[conn]&.enqueue(parts[1..])
end

#send_queues_drained?Boolean

Returns true when all per-connection send queues are empty.

Returns:

  • (Boolean)

    true when all per-connection send queues are empty



106
107
108
# File 'lib/omq/routing/router.rb', line 106

def send_queues_drained?
  @conn_queues.values.all?(&:empty?)
end

#stopvoid

This method returns an undefined value.

Stops all background tasks.



98
99
100
101
# File 'lib/omq/routing/router.rb', line 98

def stop
  @tasks.each(&:stop)
  @tasks.clear
end

#unblock_recvvoid

This method returns an undefined value.

Wakes a blocked #dequeue_recv with a nil sentinel.



45
46
47
# File 'lib/omq/routing/router.rb', line 45

def unblock_recv
  @recv_queue.enqueue(nil)
end