omq 0.29.1 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +162 -0
  3. data/README.md +3 -3
  4. data/lib/omq/constants.rb +9 -1
  5. data/lib/omq/drop_queue.rb +17 -0
  6. data/lib/omq/engine/connection_lifecycle.rb +34 -6
  7. data/lib/omq/engine/endpoints.rb +107 -0
  8. data/lib/omq/engine/heartbeat.rb +157 -15
  9. data/lib/omq/engine/monitoring.rb +95 -0
  10. data/lib/omq/engine/pump_tasks.rb +102 -0
  11. data/lib/omq/engine/reconnect.rb +10 -9
  12. data/lib/omq/engine/recv_pump.rb +18 -4
  13. data/lib/omq/engine/socket_lifecycle.rb +18 -4
  14. data/lib/omq/engine.rb +238 -302
  15. data/lib/omq/monitor_queue.rb +74 -0
  16. data/lib/omq/options.rb +78 -6
  17. data/lib/omq/routing/channel.rb +11 -5
  18. data/lib/omq/routing/client.rb +3 -1
  19. data/lib/omq/routing/closable_queues.rb +28 -0
  20. data/lib/omq/routing/conn_send_pump.rb +11 -9
  21. data/lib/omq/routing/dealer.rb +1 -0
  22. data/lib/omq/routing/dish.rb +5 -3
  23. data/lib/omq/routing/fan_out.rb +29 -21
  24. data/lib/omq/routing/gather.rb +4 -1
  25. data/lib/omq/routing/identity_routing.rb +136 -0
  26. data/lib/omq/routing/pair.rb +21 -12
  27. data/lib/omq/routing/peer.rb +30 -30
  28. data/lib/omq/routing/prefix_set.rb +239 -0
  29. data/lib/omq/routing/pub.rb +2 -1
  30. data/lib/omq/routing/pull.rb +2 -0
  31. data/lib/omq/routing/push.rb +1 -0
  32. data/lib/omq/routing/radio.rb +65 -54
  33. data/lib/omq/routing/rep.rb +4 -2
  34. data/lib/omq/routing/req.rb +8 -0
  35. data/lib/omq/routing/round_robin.rb +22 -17
  36. data/lib/omq/routing/router.rb +46 -28
  37. data/lib/omq/routing/scatter.rb +1 -0
  38. data/lib/omq/routing/server.rb +21 -25
  39. data/lib/omq/routing/sub.rb +26 -7
  40. data/lib/omq/routing/xpub.rb +25 -2
  41. data/lib/omq/routing/xsub.rb +78 -29
  42. data/lib/omq/routing.rb +52 -2
  43. data/lib/omq/socket.rb +4 -1
  44. data/lib/omq/transport/accept_loop.rb +60 -0
  45. data/lib/omq/transport/compressed_connection.rb +6 -0
  46. data/lib/omq/transport/inproc/pipe.rb +7 -0
  47. data/lib/omq/transport/inproc.rb +27 -33
  48. data/lib/omq/transport/ipc.rb +69 -7
  49. data/lib/omq/transport/tcp.rb +8 -5
  50. data/lib/omq/transport/udp.rb +92 -17
  51. data/lib/omq/version.rb +1 -1
  52. data/lib/omq.rb +2 -0
  53. metadata +11 -9
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "async/notification"
4
+
5
+ module OMQ
6
+ # Event queue behind {Socket#monitor}.
7
+ #
8
+ # Lifecycle events never block the engine fiber that emits them, so a
9
+ # slow monitor block can't stall accept loops, handshakes or teardown.
10
+ # Verbose message events (opt-in tracing) wait while
11
+ # {VERBOSE_BACKLOG} events are pending: tracing a busy socket applies
12
+ # backpressure instead of growing without bound.
13
+ #
14
+ class MonitorQueue
15
+ # Pending events above which verbose emitters wait.
16
+ VERBOSE_BACKLOG = 64
17
+
18
+
19
+ def initialize
20
+ @queue = Thread::Queue.new
21
+ @drained = Async::Notification.new
22
+ end
23
+
24
+
25
+ # Appends a lifecycle event (or the +nil+ end-of-stream marker).
26
+ # Never blocks.
27
+ #
28
+ # @param event [MonitorEvent, nil]
29
+ # @return [void]
30
+ #
31
+ def push(event)
32
+ @queue.push(event)
33
+ rescue ClosedQueueError
34
+ # monitor stopped
35
+ end
36
+
37
+ alias << push
38
+ alias enqueue push
39
+
40
+
41
+ # Appends a verbose event, waiting while the consumer is behind.
42
+ #
43
+ # @param event [MonitorEvent]
44
+ # @return [void]
45
+ #
46
+ def push_verbose(event)
47
+ while @queue.size >= VERBOSE_BACKLOG && !@queue.closed?
48
+ @drained.wait
49
+ end
50
+
51
+ push(event)
52
+ end
53
+
54
+
55
+ # @return [MonitorEvent, nil] the next event; nil marks the end
56
+ #
57
+ def dequeue
58
+ event = @queue.pop
59
+ @drained.signal
60
+ event
61
+ end
62
+
63
+
64
+ # Stops accepting events and releases waiting verbose emitters.
65
+ #
66
+ # @return [void]
67
+ #
68
+ def close
69
+ @queue.close
70
+ @drained.signal
71
+ end
72
+
73
+ end
74
+ end
data/lib/omq/options.rb CHANGED
@@ -24,9 +24,11 @@ module OMQ
24
24
  @write_timeout = nil
25
25
  @reconnect_interval = 0.1 # seconds; Range sets backoff min..max
26
26
  @heartbeat_interval = nil # seconds, nil = disabled
27
- @heartbeat_ttl = nil # seconds, nil = use heartbeat_interval
27
+ @heartbeat_ttl = nil # seconds, nil = advertise no TTL
28
28
  @heartbeat_timeout = nil # seconds, nil = use heartbeat_interval
29
29
  @max_message_size = nil # bytes, nil = unlimited
30
+ @max_message_parts = 1024 # frames per received message, nil = unlimited
31
+ @max_peer_subscriptions = 65_536 # prefixes/groups one peer may hold, nil = unlimited
30
32
  @conflate = false
31
33
  @sndbuf = nil # bytes, nil = OS default
32
34
  @rcvbuf = nil # bytes, nil = OS default
@@ -52,16 +54,22 @@ module OMQ
52
54
  # @return [Numeric, nil] read timeout in seconds (nil = no timeout)
53
55
  # @!attribute write_timeout
54
56
  # @return [Numeric, nil] write timeout in seconds (nil = no timeout)
55
- # @!attribute reconnect_interval
56
- # @return [Numeric, Range] reconnect interval in seconds, or Range for exponential backoff
57
57
  # @!attribute heartbeat_interval
58
58
  # @return [Numeric, nil] PING interval in seconds (nil = disabled)
59
59
  # @!attribute heartbeat_ttl
60
- # @return [Numeric, nil] TTL advertised in PING (nil = use heartbeat_interval)
60
+ # @return [Numeric, nil] TTL advertised in PING (nil = none)
61
61
  # @!attribute heartbeat_timeout
62
62
  # @return [Numeric, nil] time without traffic before closing (nil = use heartbeat_interval)
63
63
  # @!attribute max_message_size
64
64
  # @return [Integer, nil] maximum message size in bytes
65
+ # @!attribute max_message_parts
66
+ # @return [Integer, nil] most frames a received message may have
67
+ # (default 1024, like omq.rs; nil = unlimited). A peer that sends
68
+ # more is disconnected.
69
+ # @!attribute max_peer_subscriptions
70
+ # @return [Integer, nil] most subscription prefixes (PUB/XPUB) or
71
+ # groups (RADIO) one peer may hold (default 65536, like omq.rs;
72
+ # nil = unlimited). A peer that asks for more is disconnected.
65
73
  # @!attribute sndbuf
66
74
  # @return [Integer, nil] SO_SNDBUF size in bytes (nil = OS default)
67
75
  # @!attribute rcvbuf
@@ -75,13 +83,45 @@ module OMQ
75
83
  :linger, :identity,
76
84
  :router_mandatory, :conflate,
77
85
  :read_timeout, :write_timeout,
78
- :reconnect_interval,
79
86
  :heartbeat_interval, :heartbeat_ttl, :heartbeat_timeout,
80
- :max_message_size,
87
+ :max_message_size, :max_message_parts,
88
+ :max_peer_subscriptions,
81
89
  :sndbuf, :rcvbuf,
82
90
  :on_mute,
83
91
  :mechanism
84
92
 
93
+
94
+ # @return [Numeric, Range] reconnect interval in seconds, or Range
95
+ # for exponential backoff (begin..max)
96
+ #
97
+ attr_reader :reconnect_interval
98
+
99
+
100
+ # Sets the reconnect interval. An endless Range backs off without a
101
+ # cap and is stored with a +Float::INFINITY+ end.
102
+ #
103
+ # @param value [Numeric, Range] interval in seconds, or begin..max
104
+ # @raise [ArgumentError] unless the interval (or Range begin) is a
105
+ # positive finite number and the Range end is not below its begin
106
+ #
107
+ def reconnect_interval=(value)
108
+ @reconnect_interval = validate_reconnect_interval(value)
109
+ end
110
+
111
+
112
+ # Seconds one connect or handshake attempt may take: the reconnect
113
+ # interval, or the Range end (its begin when endless), but at least
114
+ # 0.5.
115
+ #
116
+ # @return [Numeric]
117
+ #
118
+ def attempt_timeout
119
+ ri = @reconnect_interval
120
+ ri = ri.end.finite? ? ri.end : ri.begin if ri.is_a?(Range)
121
+ [ri, 0.5].max
122
+ end
123
+
124
+
85
125
  # @return [Boolean] true if router_mandatory is set
86
126
  alias_method :router_mandatory?, :router_mandatory
87
127
 
@@ -96,5 +136,37 @@ module OMQ
96
136
 
97
137
  # @param val [Numeric, nil] alias for #write_timeout=
98
138
  alias_method :send_timeout=, :write_timeout=
139
+
140
+
141
+ private
142
+
143
+
144
+ def validate_reconnect_interval(value)
145
+ case value
146
+ when Range
147
+ min = value.begin
148
+ max = value.end || Float::INFINITY
149
+
150
+ unless positive_finite?(min) && max.is_a?(Numeric) && max >= min
151
+ raise ArgumentError, "reconnect_interval Range needs a positive finite begin and an end >= begin, got #{value.inspect}"
152
+ end
153
+
154
+ min..max
155
+ when Numeric
156
+ unless positive_finite?(value)
157
+ raise ArgumentError, "reconnect_interval must be a positive finite number, got #{value.inspect}"
158
+ end
159
+
160
+ value
161
+ else
162
+ raise ArgumentError, "reconnect_interval must be a Numeric or a Range, got #{value.inspect}"
163
+ end
164
+ end
165
+
166
+
167
+ def positive_finite?(value)
168
+ value.is_a?(Numeric) && value.positive? && value.finite?
169
+ end
170
+
99
171
  end
100
172
  end
@@ -5,6 +5,8 @@ module OMQ
5
5
  # CHANNEL socket routing: exclusive 1-to-1 bidirectional.
6
6
  #
7
7
  class Channel
8
+ include ClosableQueues
9
+
8
10
  # @return [Async::LimitedQueue]
9
11
  #
10
12
  attr_reader :recv_queue
@@ -43,10 +45,11 @@ module OMQ
43
45
  # @raise [RuntimeError] if a connection already exists
44
46
  #
45
47
  def connection_added(connection)
46
- raise "CHANNEL allows only one peer" if @connection
48
+ raise PeerRefusedError, "CHANNEL allows only one peer" if @connection
47
49
  @connection = connection
48
50
 
49
- @engine.start_recv_pump(connection, @recv_queue)
51
+ # Single-frame socket: drop multipart input from a bad peer.
52
+ @engine.start_recv_pump(connection, @recv_queue) { |msg| msg if msg.size == 1 }
50
53
 
51
54
  unless connection.is_a?(Transport::Inproc::Pipe)
52
55
  @send_queue = Routing.build_queue(@engine.options.send_hwm, :block)
@@ -97,9 +100,12 @@ module OMQ
97
100
  batch = []
98
101
 
99
102
  loop do
100
- Routing.dequeue_batch(@send_queue, batch)
101
- batch.each { |parts| conn.write_message(parts) }
102
- conn.flush
103
+ Routing.dequeue_batch(@send_queue, batch) or break
104
+ @engine.sending_batch do
105
+ batch.each { |parts| conn.write_message(parts) }
106
+ conn.flush
107
+ end
108
+
103
109
  batch.clear
104
110
  end
105
111
  end
@@ -8,6 +8,7 @@ module OMQ
8
8
  #
9
9
  class Client
10
10
  include RoundRobin
11
+ include ClosableQueues
11
12
 
12
13
 
13
14
  # @return [Async::LimitedQueue]
@@ -46,7 +47,8 @@ module OMQ
46
47
  #
47
48
  def connection_added(connection)
48
49
  @connections << connection
49
- @engine.start_recv_pump(connection, @recv_queue)
50
+ # Single-frame socket: drop multipart input from a bad peer.
51
+ @engine.start_recv_pump(connection, @recv_queue) { |msg| msg if msg.size == 1 }
50
52
  add_round_robin_send_connection(connection)
51
53
  end
52
54
 
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OMQ
4
+ module Routing
5
+ # Closes the queues a routing strategy owns once its socket is closed.
6
+ #
7
+ # Fibers blocked in a send raise +Async::Queue::ClosedError+ (or
8
+ # +ClosedQueueError+ for a {DropQueue}), which {Engine#enqueue_send}
9
+ # turns into {SocketClosedError}. Fibers blocked in a receive get
10
+ # +nil+, like +Thread::Queue#pop+ on a closed queue.
11
+ #
12
+ # Relies on the ivar names the built-in strategies share:
13
+ # +@recv_queue+, +@send_queue+, +@staging_queue+ and the
14
+ # per-connection +@conn_queues+ Hash.
15
+ #
16
+ module ClosableQueues
17
+ # @return [void]
18
+ #
19
+ def close_queues
20
+ @recv_queue&.close
21
+ @send_queue&.close
22
+ @staging_queue&.close
23
+ @conn_queues&.each_value(&:close)
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -4,8 +4,8 @@ module OMQ
4
4
  module Routing
5
5
  # Starts a dedicated send pump for one per-connection send queue.
6
6
  #
7
- # Used by Router and Rep, which have per-connection queues but do not
8
- # include the RoundRobin mixin.
7
+ # Used by {IdentityRouting} (ROUTER, SERVER, PEER) and Rep, which
8
+ # have per-peer queues but do not include the RoundRobin mixin.
9
9
  #
10
10
  module ConnSendPump
11
11
  # Spawns the pump task on the connection's lifecycle barrier so it
@@ -21,15 +21,17 @@ module OMQ
21
21
  batch = []
22
22
 
23
23
  loop do
24
- Routing.dequeue_batch(q, batch)
24
+ Routing.dequeue_batch(q, batch) or break
25
25
 
26
- if batch.size == 1
27
- conn.write_message batch.first
28
- else
29
- conn.write_messages batch
30
- end
26
+ engine.sending_batch do
27
+ if batch.size == 1
28
+ conn.write_message batch.first
29
+ else
30
+ conn.write_messages batch
31
+ end
31
32
 
32
- conn.flush
33
+ conn.flush
34
+ end
33
35
 
34
36
  batch.each do |parts|
35
37
  engine.emit_verbose_msg_sent(conn, parts)
@@ -8,6 +8,7 @@ module OMQ
8
8
  #
9
9
  class Dealer
10
10
  include RoundRobin
11
+ include ClosableQueues
11
12
 
12
13
 
13
14
  # @return [Async::LimitedQueue]
@@ -8,6 +8,8 @@ module OMQ
8
8
  # Receives two-frame messages (group + body) from RADIO.
9
9
  #
10
10
  class Dish
11
+ include ClosableQueues
12
+
11
13
  # @return [Async::LimitedQueue]
12
14
  #
13
15
  attr_reader :recv_queue
@@ -45,7 +47,7 @@ module OMQ
45
47
  #
46
48
  def connection_added(connection)
47
49
  @connections << connection
48
- @groups.each do |group|
50
+ @groups.to_a.each do |group|
49
51
  connection.send_command(Protocol::ZMTP::Codec::Command.join(group))
50
52
  end
51
53
  @engine.start_recv_pump(connection, @recv_queue)
@@ -72,7 +74,7 @@ module OMQ
72
74
  #
73
75
  def join(group)
74
76
  @groups << group
75
- @connections.each do |conn|
77
+ @connections.dup.each do |conn|
76
78
  conn.send_command(Protocol::ZMTP::Codec::Command.join(group))
77
79
  end
78
80
  end
@@ -84,7 +86,7 @@ module OMQ
84
86
  #
85
87
  def leave(group)
86
88
  @groups.delete(group)
87
- @connections.each do |conn|
89
+ @connections.dup.each do |conn|
88
90
  conn.send_command(Protocol::ZMTP::Codec::Command.leave(group))
89
91
  end
90
92
  end
@@ -38,9 +38,9 @@ module OMQ
38
38
 
39
39
  def init_fan_out(engine)
40
40
  @connections = Set.new
41
- @subscriptions = {} # connection => Set of prefixes
42
- @subscribe_all = Set.new # connections subscribed to "" (match-all fast path)
41
+ @subscriptions = {} # connection => PrefixSet
43
42
  @conn_queues = {} # connection => per-connection send queue
43
+ @fan_out_queues = [].freeze # snapshot of @conn_queues values
44
44
  @conflate = engine.options.conflate
45
45
  @subscriber_joined = Async::Promise.new
46
46
  @latest = {} if @conflate
@@ -50,10 +50,8 @@ module OMQ
50
50
  # @return [Boolean] whether the connection is subscribed to the topic
51
51
  #
52
52
  def subscribed?(conn, topic)
53
- return true if @subscribe_all.include?(conn)
54
53
  subs = @subscriptions[conn]
55
- return false unless subs
56
- subs.any? { |prefix| topic.start_with?(prefix) }
54
+ subs ? subs.match?(topic) : false
57
55
  end
58
56
 
59
57
 
@@ -65,8 +63,9 @@ module OMQ
65
63
  # @param prefix [String]
66
64
  #
67
65
  def on_subscribe(conn, prefix)
68
- @subscriptions[conn] << prefix.b.freeze
69
- @subscribe_all.add(conn) if prefix.empty?
66
+ Routing.check_peer_subscriptions!(@subscriptions[conn], prefix, @engine.options.max_peer_subscriptions)
67
+
68
+ @subscriptions[conn] << prefix
70
69
  @subscriber_joined.resolve(conn) unless @subscriber_joined.resolved?
71
70
  end
72
71
 
@@ -79,7 +78,6 @@ module OMQ
79
78
  #
80
79
  def on_cancel(conn, prefix)
81
80
  @subscriptions[conn]&.delete(prefix)
82
- @subscribe_all.delete(conn) if prefix.empty?
83
81
  end
84
82
 
85
83
 
@@ -91,6 +89,7 @@ module OMQ
91
89
  def add_fan_out_send_connection(conn)
92
90
  q = Routing.build_queue(@engine.options.send_hwm, @engine.options.on_mute)
93
91
  @conn_queues[conn] = q
92
+ @fan_out_queues = @conn_queues.values.freeze
94
93
  start_conn_send_pump(conn, q)
95
94
  end
96
95
 
@@ -102,8 +101,8 @@ module OMQ
102
101
  # @param conn [Protocol::ZMTP::Connection]
103
102
  #
104
103
  def remove_fan_out_send_connection(conn)
105
- @subscribe_all.delete(conn)
106
- @conn_queues.delete(conn)
104
+ @conn_queues.delete(conn)&.close
105
+ @fan_out_queues = @conn_queues.values.freeze
107
106
  end
108
107
 
109
108
 
@@ -119,11 +118,14 @@ module OMQ
119
118
  # or other subscribers. Applications can opt in to :block for
120
119
  # strict backpressure.
121
120
  #
121
+ # Iterates a frozen snapshot: a :block enqueue can yield, and peers
122
+ # that join or leave meanwhile must not break the iteration.
123
+ #
122
124
  # @param parts [Array<String>]
123
125
  #
124
126
  def fan_out_enqueue(parts)
125
- @connections.each do |conn|
126
- @conn_queues[conn].enqueue(parts)
127
+ @fan_out_queues.each do |q|
128
+ Routing.enqueue_to_peer(q, parts)
127
129
  end
128
130
  end
129
131
 
@@ -181,12 +183,15 @@ module OMQ
181
183
  aux_batch = []
182
184
 
183
185
  loop do
184
- Routing.dequeue_batch(q, batch)
186
+ Routing.dequeue_batch(q, batch) or break
185
187
 
186
- if write_matching_batch(conn, batch, use_wire, aux_batch)
187
- conn.flush
188
- batch.each { |parts| @engine.emit_verbose_msg_sent(conn, parts) }
188
+ @engine.sending_batch do
189
+ if write_matching_batch(conn, batch, use_wire, aux_batch)
190
+ conn.flush
191
+ batch.each { |parts| @engine.emit_verbose_msg_sent(conn, parts) }
192
+ end
189
193
  end
194
+
190
195
  batch.clear
191
196
  end
192
197
  end
@@ -257,7 +262,7 @@ module OMQ
257
262
  batch = []
258
263
 
259
264
  loop do
260
- Routing.dequeue_batch(q, batch)
265
+ Routing.dequeue_batch(q, batch) or break
261
266
 
262
267
  # Keep only the latest message that matches the subscription.
263
268
  latest = batch.reverse.find do |parts|
@@ -265,11 +270,14 @@ module OMQ
265
270
  end
266
271
 
267
272
  batch.clear
268
- next unless latest
269
273
 
270
- conn.write_message(latest)
271
- conn.flush
272
- @engine.emit_verbose_msg_sent(conn, latest)
274
+ @engine.sending_batch do
275
+ if latest
276
+ conn.write_message(latest)
277
+ conn.flush
278
+ @engine.emit_verbose_msg_sent(conn, latest)
279
+ end
280
+ end
273
281
  end
274
282
  end
275
283
  end
@@ -5,6 +5,8 @@ module OMQ
5
5
  # GATHER socket routing: fair-queue receive from SCATTER peers.
6
6
  #
7
7
  class Gather
8
+ include ClosableQueues
9
+
8
10
  # @return [Async::LimitedQueue]
9
11
  #
10
12
  attr_reader :recv_queue
@@ -39,7 +41,8 @@ module OMQ
39
41
  # @param connection [Protocol::ZMTP::Connection]
40
42
  #
41
43
  def connection_added(connection)
42
- @engine.start_recv_pump(connection, @recv_queue)
44
+ # Single-frame socket: drop multipart input from a bad peer.
45
+ @engine.start_recv_pump(connection, @recv_queue) { |msg| msg if msg.size == 1 }
43
46
  end
44
47
 
45
48
 
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OMQ
4
+ module Routing
5
+ # Send queues and routing ids for identity-routed sockets (ROUTER,
6
+ # SERVER, PEER).
7
+ #
8
+ # Each peer has a route: its routing id and a bounded send queue that
9
+ # its own pump drains. A dialed endpoint keeps its route across
10
+ # reconnects, like a libzmq pipe: sends to that id while the peer is
11
+ # down wait in the queue, and the redialed connection delivers them.
12
+ # A message the lost connection's pump had already taken is gone.
13
+ # Accepted peers lose their route with the connection, since a peer
14
+ # that comes back can't be told apart from a new one.
15
+ #
16
+ # Including classes call {#init_identity_routing} from #initialize
17
+ # and implement +#generate_routing_id+.
18
+ #
19
+ module IdentityRouting
20
+ # One peer's routing id and send queue. +endpoint+ is set for a
21
+ # dialed peer, +conn+ while it is connected.
22
+ Route = Struct.new(:id, :queue, :endpoint, :conn)
23
+
24
+
25
+ # @return [Boolean] true when every send queue is empty, including
26
+ # those of dialed peers that are down
27
+ #
28
+ def send_queues_drained?
29
+ @route_by_conn.each_value.all? { |route| route.queue.empty? } &&
30
+ @dial_routes.each_value.all? { |route| route.queue.empty? }
31
+ end
32
+
33
+
34
+ # Closes every queue, including those of dialed peers that are down.
35
+ #
36
+ # @return [void]
37
+ #
38
+ def close_queues
39
+ super
40
+ all_routes.each { |route| route.queue.close }
41
+ end
42
+
43
+
44
+ # Drops the route kept for a dialed endpoint. Called by
45
+ # {Engine#disconnect}, and when a redial won't happen.
46
+ #
47
+ # @param endpoint [String]
48
+ # @return [void]
49
+ #
50
+ def drop_route(endpoint)
51
+ route = @dial_routes[endpoint]
52
+ drop(route) if route && route.conn.nil?
53
+ end
54
+
55
+
56
+ private
57
+
58
+
59
+ def init_identity_routing(engine)
60
+ @routes = {} # routing id => Route
61
+ @route_by_conn = {} # connection => Route
62
+ @dial_routes = {} # endpoint => Route kept across redials
63
+ end
64
+
65
+
66
+ # Registers +conn+ and starts its send pump. A dialed connection
67
+ # takes over its endpoint's route, so queued sends go out.
68
+ #
69
+ # @param conn [Protocol::ZMTP::Connection, Transport::Inproc::Pipe]
70
+ # @param peer_id [String, nil] id the peer asked for (ROUTER)
71
+ # @return [Route]
72
+ #
73
+ def attach_route(conn, peer_id = nil)
74
+ endpoint = @engine.dialed_endpoint(conn)
75
+ route = endpoint && @dial_routes[endpoint]
76
+ route = nil if route&.conn
77
+ route ||= Route.new(nil, Routing.build_queue(@engine.options.send_hwm, :block), endpoint)
78
+ id = peer_id || route.id || generate_routing_id
79
+
80
+ unroute(route) if route.id && route.id != id
81
+
82
+ route.id = id
83
+ route.conn = conn
84
+ @routes[id] = route # a newer peer takes over the id
85
+ @route_by_conn[conn] = route
86
+ @dial_routes[endpoint] = route if endpoint
87
+
88
+ ConnSendPump.start(@engine, conn, route.queue)
89
+ route
90
+ end
91
+
92
+
93
+ # Unregisters +conn+. Its route stays if the endpoint will be
94
+ # redialed.
95
+ #
96
+ # @param conn [Protocol::ZMTP::Connection, Transport::Inproc::Pipe]
97
+ # @return [void]
98
+ #
99
+ def detach_route(conn)
100
+ route = @route_by_conn.delete(conn) or return
101
+ route.conn = nil
102
+
103
+ endpoint = route.endpoint
104
+ return if endpoint && @dial_routes[endpoint].equal?(route) && @engine.redials?(endpoint)
105
+
106
+ drop(route)
107
+ end
108
+
109
+
110
+ # @param id [String]
111
+ # @return [Route, nil]
112
+ #
113
+ def route_to(id)
114
+ @routes[id]
115
+ end
116
+
117
+
118
+ def drop(route)
119
+ unroute(route)
120
+ @dial_routes.delete(route.endpoint) if @dial_routes[route.endpoint].equal?(route)
121
+ route.queue.close
122
+ end
123
+
124
+
125
+ def unroute(route)
126
+ @routes.delete(route.id) if @routes[route.id].equal?(route)
127
+ end
128
+
129
+
130
+ def all_routes
131
+ (@route_by_conn.values + @dial_routes.values).uniq
132
+ end
133
+
134
+ end
135
+ end
136
+ end