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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +162 -0
- data/README.md +3 -3
- data/lib/omq/constants.rb +9 -1
- data/lib/omq/drop_queue.rb +17 -0
- data/lib/omq/engine/connection_lifecycle.rb +34 -6
- data/lib/omq/engine/endpoints.rb +107 -0
- data/lib/omq/engine/heartbeat.rb +157 -15
- data/lib/omq/engine/monitoring.rb +95 -0
- data/lib/omq/engine/pump_tasks.rb +102 -0
- data/lib/omq/engine/reconnect.rb +10 -9
- data/lib/omq/engine/recv_pump.rb +18 -4
- data/lib/omq/engine/socket_lifecycle.rb +18 -4
- data/lib/omq/engine.rb +238 -302
- data/lib/omq/monitor_queue.rb +74 -0
- data/lib/omq/options.rb +78 -6
- data/lib/omq/routing/channel.rb +11 -5
- data/lib/omq/routing/client.rb +3 -1
- data/lib/omq/routing/closable_queues.rb +28 -0
- data/lib/omq/routing/conn_send_pump.rb +11 -9
- data/lib/omq/routing/dealer.rb +1 -0
- data/lib/omq/routing/dish.rb +5 -3
- data/lib/omq/routing/fan_out.rb +29 -21
- data/lib/omq/routing/gather.rb +4 -1
- data/lib/omq/routing/identity_routing.rb +136 -0
- data/lib/omq/routing/pair.rb +21 -12
- data/lib/omq/routing/peer.rb +30 -30
- data/lib/omq/routing/prefix_set.rb +239 -0
- data/lib/omq/routing/pub.rb +2 -1
- data/lib/omq/routing/pull.rb +2 -0
- data/lib/omq/routing/push.rb +1 -0
- data/lib/omq/routing/radio.rb +65 -54
- data/lib/omq/routing/rep.rb +4 -2
- data/lib/omq/routing/req.rb +8 -0
- data/lib/omq/routing/round_robin.rb +22 -17
- data/lib/omq/routing/router.rb +46 -28
- data/lib/omq/routing/scatter.rb +1 -0
- data/lib/omq/routing/server.rb +21 -25
- data/lib/omq/routing/sub.rb +26 -7
- data/lib/omq/routing/xpub.rb +25 -2
- data/lib/omq/routing/xsub.rb +78 -29
- data/lib/omq/routing.rb +52 -2
- data/lib/omq/socket.rb +4 -1
- data/lib/omq/transport/accept_loop.rb +60 -0
- data/lib/omq/transport/compressed_connection.rb +6 -0
- data/lib/omq/transport/inproc/pipe.rb +7 -0
- data/lib/omq/transport/inproc.rb +27 -33
- data/lib/omq/transport/ipc.rb +69 -7
- data/lib/omq/transport/tcp.rb +8 -5
- data/lib/omq/transport/udp.rb +92 -17
- data/lib/omq/version.rb +1 -1
- data/lib/omq.rb +2 -0
- metadata +11 -9
data/lib/omq/engine.rb
CHANGED
|
@@ -8,6 +8,9 @@ require_relative "engine/reconnect"
|
|
|
8
8
|
require_relative "engine/connection_lifecycle"
|
|
9
9
|
require_relative "engine/socket_lifecycle"
|
|
10
10
|
require_relative "engine/maintenance"
|
|
11
|
+
require_relative "engine/endpoints"
|
|
12
|
+
require_relative "engine/monitoring"
|
|
13
|
+
require_relative "engine/pump_tasks"
|
|
11
14
|
|
|
12
15
|
module OMQ
|
|
13
16
|
# Per-socket orchestrator.
|
|
@@ -16,6 +19,11 @@ module OMQ
|
|
|
16
19
|
# OMQ::Socket instance. Each socket type creates one Engine.
|
|
17
20
|
#
|
|
18
21
|
class Engine
|
|
22
|
+
include Endpoints
|
|
23
|
+
include Monitoring
|
|
24
|
+
include PumpTasks
|
|
25
|
+
|
|
26
|
+
|
|
19
27
|
# Scheme → transport module registry.
|
|
20
28
|
# Plugins add entries via +Engine.transports["scheme"] = MyTransport+.
|
|
21
29
|
#
|
|
@@ -60,7 +68,7 @@ module OMQ
|
|
|
60
68
|
|
|
61
69
|
|
|
62
70
|
# @!attribute [w] monitor_queue
|
|
63
|
-
# @param value [
|
|
71
|
+
# @param value [MonitorQueue, nil] queue for monitor events
|
|
64
72
|
#
|
|
65
73
|
attr_writer :monitor_queue
|
|
66
74
|
|
|
@@ -86,21 +94,58 @@ module OMQ
|
|
|
86
94
|
@routing = nil
|
|
87
95
|
@connections = {} # connection => ConnectionLifecycle
|
|
88
96
|
@dialers = {} # endpoint => Dialer (reconnect intent + connect logic)
|
|
97
|
+
@dialer_barriers = {} # endpoint => Async::Barrier (its reconnect + connection tasks)
|
|
89
98
|
@listeners = {} # endpoint => Listener
|
|
99
|
+
@bind_aliases = {} # endpoint as passed to #bind => listener endpoint
|
|
90
100
|
@lifecycle = SocketLifecycle.new
|
|
91
101
|
@fatal_error = nil
|
|
92
102
|
@monitor_queue = nil
|
|
93
103
|
@verbose_monitor = false
|
|
104
|
+
@drain_signal = nil # set while #close waits for send queues
|
|
105
|
+
@batches_sending = 0 # batches pumps took off a queue and haven't flushed
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# @return [Async::Promise] resolves with the first peer that connects
|
|
110
|
+
#
|
|
111
|
+
def peer_connected
|
|
112
|
+
@lifecycle.peer_connected
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# @return [Async::Promise] resolves once every peer is gone
|
|
117
|
+
#
|
|
118
|
+
def all_peers_gone
|
|
119
|
+
@lifecycle.all_peers_gone
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
# @return [Async::Task, nil] the task the socket's tasks run under
|
|
124
|
+
#
|
|
125
|
+
def parent_task
|
|
126
|
+
@lifecycle.parent_task
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# @return [Async::Barrier, nil] the socket-level barrier
|
|
131
|
+
#
|
|
132
|
+
def barrier
|
|
133
|
+
@lifecycle.barrier
|
|
94
134
|
end
|
|
95
135
|
|
|
96
136
|
|
|
97
|
-
#
|
|
98
|
-
|
|
99
|
-
def
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
137
|
+
# @return [Boolean] true once the socket is closed
|
|
138
|
+
#
|
|
139
|
+
def closed?
|
|
140
|
+
@lifecycle.closed?
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# @return [Boolean] true if the socket runs on the shared IO thread
|
|
145
|
+
#
|
|
146
|
+
def on_io_thread?
|
|
147
|
+
@lifecycle.on_io_thread
|
|
148
|
+
end
|
|
104
149
|
|
|
105
150
|
|
|
106
151
|
# Enables or disables auto-reconnect for dropped connections.
|
|
@@ -155,34 +200,6 @@ module OMQ
|
|
|
155
200
|
end
|
|
156
201
|
|
|
157
202
|
|
|
158
|
-
# Returns the transport object (Dialer or Listener) for an endpoint.
|
|
159
|
-
# Used by {ConnectionLifecycle#ready!} to call +#wrap_connection+.
|
|
160
|
-
#
|
|
161
|
-
# @param endpoint [String]
|
|
162
|
-
# @return [Dialer, Listener, nil]
|
|
163
|
-
#
|
|
164
|
-
def transport_object_for(endpoint)
|
|
165
|
-
@dialers[endpoint] || @listeners[endpoint]
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
# Spawns a ruby:// reconnect retry task under the socket's parent task.
|
|
170
|
-
#
|
|
171
|
-
# @param endpoint [String]
|
|
172
|
-
# @yield [interval] the retry loop body
|
|
173
|
-
#
|
|
174
|
-
def spawn_inproc_retry(endpoint)
|
|
175
|
-
ri = @options.reconnect_interval
|
|
176
|
-
ivl = ri.is_a?(Range) ? ri.begin : ri
|
|
177
|
-
ann = "inproc reconnect #{endpoint}"
|
|
178
|
-
|
|
179
|
-
@lifecycle.barrier.async(transient: true, annotation: ann) do
|
|
180
|
-
yield ivl
|
|
181
|
-
rescue Async::Stop, Async::Cancel
|
|
182
|
-
end
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
|
|
186
203
|
# Binds to an endpoint.
|
|
187
204
|
#
|
|
188
205
|
# @param endpoint [String] e.g. "tcp://127.0.0.1:5555", "ruby://foo"
|
|
@@ -198,6 +215,7 @@ module OMQ
|
|
|
198
215
|
start_accept_loops(listener)
|
|
199
216
|
|
|
200
217
|
@listeners[listener.endpoint] = listener
|
|
218
|
+
remember_bind_alias(endpoint, listener.endpoint)
|
|
201
219
|
emit_monitor_event(:listening, endpoint: listener.endpoint)
|
|
202
220
|
URI.parse(listener.endpoint)
|
|
203
221
|
rescue => error
|
|
@@ -217,14 +235,12 @@ module OMQ
|
|
|
217
235
|
endpoint, transport = resolve_endpoint(endpoint)
|
|
218
236
|
validate_endpoint!(endpoint, transport)
|
|
219
237
|
|
|
220
|
-
dialer
|
|
238
|
+
dialer = transport.dialer(endpoint, self, **opts)
|
|
239
|
+
@dialers[endpoint] = dialer
|
|
221
240
|
|
|
222
241
|
if endpoint.start_with?("ruby://")
|
|
223
|
-
|
|
224
|
-
dialer.connect
|
|
225
|
-
@dialers[endpoint] = dialer
|
|
242
|
+
connect_inproc(endpoint, dialer)
|
|
226
243
|
else
|
|
227
|
-
@dialers[endpoint] = dialer
|
|
228
244
|
emit_monitor_event(:connect_delayed, endpoint: endpoint)
|
|
229
245
|
schedule_reconnect(endpoint, delay: 0)
|
|
230
246
|
end
|
|
@@ -233,8 +249,8 @@ module OMQ
|
|
|
233
249
|
end
|
|
234
250
|
|
|
235
251
|
|
|
236
|
-
# Disconnects from an endpoint.
|
|
237
|
-
#
|
|
252
|
+
# Disconnects from an endpoint. Stops its reconnect loop and any
|
|
253
|
+
# handshake in progress, then closes its connections.
|
|
238
254
|
#
|
|
239
255
|
# @param endpoint [String]
|
|
240
256
|
# @return [void]
|
|
@@ -242,20 +258,26 @@ module OMQ
|
|
|
242
258
|
def disconnect(endpoint)
|
|
243
259
|
endpoint = canonical_endpoint(endpoint)
|
|
244
260
|
@dialers.delete(endpoint)
|
|
261
|
+
@dialer_barriers.delete(endpoint)&.stop
|
|
245
262
|
close_connections_at(endpoint)
|
|
263
|
+
drop_route(endpoint)
|
|
246
264
|
end
|
|
247
265
|
|
|
248
266
|
|
|
249
267
|
# Unbinds from an endpoint. Stops the listener and closes all
|
|
250
|
-
# connections that were accepted on it.
|
|
268
|
+
# connections that were accepted on it. Accepts the resolved
|
|
269
|
+
# endpoint (as returned by {#bind}) or the string passed to
|
|
270
|
+
# {#bind}, e.g. +tcp://localhost:5555+.
|
|
251
271
|
#
|
|
252
272
|
# @param endpoint [String]
|
|
253
273
|
# @return [void]
|
|
254
274
|
#
|
|
255
275
|
def unbind(endpoint)
|
|
256
276
|
endpoint = canonical_endpoint(endpoint)
|
|
277
|
+
endpoint = @bind_aliases.fetch(endpoint, endpoint)
|
|
257
278
|
listener = @listeners.delete(endpoint) or return
|
|
258
279
|
|
|
280
|
+
@bind_aliases.delete_if { |_, bound| bound == endpoint }
|
|
259
281
|
listener.stop
|
|
260
282
|
close_connections_at(endpoint)
|
|
261
283
|
end
|
|
@@ -279,8 +301,16 @@ module OMQ
|
|
|
279
301
|
# @return [void]
|
|
280
302
|
#
|
|
281
303
|
def handle_connected(io, endpoint: nil)
|
|
304
|
+
barrier = @dialer_barriers[endpoint]
|
|
305
|
+
|
|
306
|
+
# Disconnected (or closed) while the dial was in flight.
|
|
307
|
+
unless barrier && @dialers.key?(endpoint)
|
|
308
|
+
io.close rescue nil
|
|
309
|
+
return
|
|
310
|
+
end
|
|
311
|
+
|
|
282
312
|
emit_monitor_event(:connected, endpoint: endpoint)
|
|
283
|
-
spawn_connection(io, as_server: false, endpoint: endpoint)
|
|
313
|
+
spawn_connection(io, as_server: false, endpoint: endpoint, parent: barrier)
|
|
284
314
|
end
|
|
285
315
|
|
|
286
316
|
|
|
@@ -288,20 +318,82 @@ module OMQ
|
|
|
288
318
|
# Skips ZMTP handshake — just registers with routing strategy.
|
|
289
319
|
#
|
|
290
320
|
# @param pipe [Transport::Inproc::Pipe]
|
|
321
|
+
# @return [Boolean] false if the routing strategy refused the peer
|
|
322
|
+
#
|
|
323
|
+
def connection_ready(pipe, endpoint: nil, dialed: false)
|
|
324
|
+
ConnectionLifecycle.new(self, endpoint: endpoint, dialed: dialed).ready_direct!(pipe)
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
# @param conn [Protocol::ZMTP::Connection, Transport::Inproc::Pipe]
|
|
329
|
+
# @return [String, nil] the endpoint this socket dialed to get +conn+,
|
|
330
|
+
# nil for an accepted connection
|
|
331
|
+
#
|
|
332
|
+
def dialed_endpoint(conn)
|
|
333
|
+
lifecycle = @connections[conn]
|
|
334
|
+
lifecycle.endpoint if lifecycle&.dialed?
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
# @param endpoint [String, nil]
|
|
339
|
+
# @return [Boolean] true if a lost connection to +endpoint+ gets
|
|
340
|
+
# redialed
|
|
341
|
+
#
|
|
342
|
+
def redials?(endpoint)
|
|
343
|
+
return false unless endpoint && @dialers.key?(endpoint)
|
|
344
|
+
|
|
345
|
+
@lifecycle.open? && @lifecycle.reconnect_enabled
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
# Drops the send queue a routing strategy keeps for a dialed
|
|
350
|
+
# endpoint between connections (ROUTER, SERVER, PEER).
|
|
351
|
+
#
|
|
352
|
+
# @param endpoint [String, nil]
|
|
291
353
|
# @return [void]
|
|
292
354
|
#
|
|
293
|
-
def
|
|
294
|
-
|
|
355
|
+
def drop_route(endpoint)
|
|
356
|
+
@routing.drop_route(endpoint) if endpoint && @routing.respond_to?(:drop_route)
|
|
357
|
+
wake_drain
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
# Wakes {#drain_send_queues} to check the send queues again. Send
|
|
362
|
+
# pumps call it after each batch, teardown when a queue goes away.
|
|
363
|
+
# A no-op unless {#close} is waiting.
|
|
364
|
+
#
|
|
365
|
+
# @return [void]
|
|
366
|
+
#
|
|
367
|
+
def wake_drain
|
|
368
|
+
@drain_signal&.signal
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
# Runs a send pump's write of a batch it took off a queue. Until the
|
|
373
|
+
# block returns, {#drain_send_queues} counts the batch as unsent, so
|
|
374
|
+
# #close doesn't tear the connection down under the write.
|
|
375
|
+
#
|
|
376
|
+
# @yield the write and flush
|
|
377
|
+
# @return [Object] the block's value
|
|
378
|
+
#
|
|
379
|
+
def sending_batch
|
|
380
|
+
@batches_sending += 1
|
|
381
|
+
yield
|
|
382
|
+
ensure
|
|
383
|
+
@batches_sending -= 1
|
|
384
|
+
wake_drain
|
|
295
385
|
end
|
|
296
386
|
|
|
297
387
|
|
|
298
388
|
# Dequeues the next received message. Blocks until available.
|
|
299
389
|
#
|
|
300
|
-
# @return [Array<String
|
|
390
|
+
# @return [Array<String>, nil] message parts, or nil once the socket
|
|
391
|
+
# is closed (like +Thread::Queue#pop+ on a closed queue)
|
|
301
392
|
# @raise if a background pump task crashed
|
|
302
393
|
#
|
|
303
394
|
def dequeue_recv
|
|
304
395
|
raise @fatal_error if @fatal_error
|
|
396
|
+
return if @lifecycle.closed?
|
|
305
397
|
|
|
306
398
|
msg = routing.dequeue_recv
|
|
307
399
|
raise @fatal_error if msg.nil? && @fatal_error
|
|
@@ -323,10 +415,16 @@ module OMQ
|
|
|
323
415
|
# @param parts [Array<String>]
|
|
324
416
|
# @return [void]
|
|
325
417
|
# @raise if a background pump task crashed
|
|
418
|
+
# @raise [SocketClosedError] if the socket is closing or closed,
|
|
419
|
+
# including while blocked at HWM
|
|
326
420
|
#
|
|
327
421
|
def enqueue_send(parts)
|
|
328
422
|
raise @fatal_error if @fatal_error
|
|
423
|
+
raise_if_closed!
|
|
424
|
+
|
|
329
425
|
routing.enqueue(parts)
|
|
426
|
+
rescue Async::Queue::ClosedError, ClosedQueueError
|
|
427
|
+
raise SocketClosedError, "#{@socket_type} socket closed while sending"
|
|
330
428
|
end
|
|
331
429
|
|
|
332
430
|
|
|
@@ -369,12 +467,9 @@ module OMQ
|
|
|
369
467
|
# and the endpoint is still dialed.
|
|
370
468
|
#
|
|
371
469
|
def maybe_reconnect(endpoint)
|
|
372
|
-
return unless
|
|
373
|
-
return unless @lifecycle.open? && @lifecycle.reconnect_enabled
|
|
470
|
+
return unless redials?(endpoint)
|
|
374
471
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
Reconnect.schedule(dialer, @options, @lifecycle.parent_task, self)
|
|
472
|
+
Reconnect.schedule(@dialers[endpoint], @options, dialer_barrier(endpoint), self)
|
|
378
473
|
end
|
|
379
474
|
|
|
380
475
|
|
|
@@ -386,6 +481,7 @@ module OMQ
|
|
|
386
481
|
# @return [void]
|
|
387
482
|
#
|
|
388
483
|
def close
|
|
484
|
+
return close_unopened if @lifecycle.state == :new
|
|
389
485
|
return unless @lifecycle.open?
|
|
390
486
|
|
|
391
487
|
@lifecycle.start_closing!
|
|
@@ -403,6 +499,7 @@ module OMQ
|
|
|
403
499
|
|
|
404
500
|
stop_listeners
|
|
405
501
|
tear_down_barrier
|
|
502
|
+
close_routing_queues
|
|
406
503
|
emit_monitor_event(:closed)
|
|
407
504
|
close_monitor_queue
|
|
408
505
|
end
|
|
@@ -425,100 +522,12 @@ module OMQ
|
|
|
425
522
|
|
|
426
523
|
stop_listeners
|
|
427
524
|
tear_down_barrier
|
|
525
|
+
close_routing_queues
|
|
428
526
|
emit_monitor_event(:closed)
|
|
429
527
|
close_monitor_queue
|
|
430
528
|
end
|
|
431
529
|
|
|
432
530
|
|
|
433
|
-
# Spawns a transient pump task with error propagation.
|
|
434
|
-
#
|
|
435
|
-
# Unexpected exceptions are caught and forwarded to
|
|
436
|
-
# {#signal_fatal_error} so blocked callers (send/recv)
|
|
437
|
-
# see the real error instead of deadlocking.
|
|
438
|
-
#
|
|
439
|
-
# @param annotation [String] task annotation for debugging
|
|
440
|
-
# @param parent [Async::Task, Async::Barrier] parent for the spawned
|
|
441
|
-
# task. Defaults to the current task. Routing strategies that own
|
|
442
|
-
# loose pump tasks (Radio, Channel) pass their own +Async::Barrier+
|
|
443
|
-
# so a single +barrier.stop+ tears the lot down at strategy
|
|
444
|
-
# shutdown without per-task bookkeeping.
|
|
445
|
-
# @yield the pump loop body
|
|
446
|
-
# @return [Async::Task]
|
|
447
|
-
#
|
|
448
|
-
def spawn_pump_task(annotation:, parent: Async::Task.current, &block)
|
|
449
|
-
parent.async(transient: true, annotation: annotation) do
|
|
450
|
-
yield
|
|
451
|
-
rescue Async::Stop, Async::Cancel, Protocol::ZMTP::Error, *CONNECTION_LOST
|
|
452
|
-
# normal shutdown / expected disconnect
|
|
453
|
-
rescue => error
|
|
454
|
-
signal_fatal_error(error)
|
|
455
|
-
end
|
|
456
|
-
end
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
# Spawns a per-connection pump task on the connection's own
|
|
460
|
-
# lifecycle barrier. When any pump on the barrier exits (e.g. the
|
|
461
|
-
# send pump sees EPIPE and calls {#connection_lost}), {ConnectionLifecycle#tear_down!}
|
|
462
|
-
# calls `barrier.stop` which cancels every sibling pump for that
|
|
463
|
-
# connection — so a dead peer can no longer leave orphan send
|
|
464
|
-
# pumps blocked on `dequeue` waiting for messages that will never
|
|
465
|
-
# be written.
|
|
466
|
-
#
|
|
467
|
-
# @param conn [Protocol::ZMTP::Connection, Transport::Inproc::Pipe]
|
|
468
|
-
# @param annotation [String]
|
|
469
|
-
#
|
|
470
|
-
def spawn_conn_pump_task(conn, annotation:, &block)
|
|
471
|
-
lifecycle = @connections[conn]
|
|
472
|
-
|
|
473
|
-
unless lifecycle
|
|
474
|
-
return spawn_pump_task(annotation: annotation, &block)
|
|
475
|
-
end
|
|
476
|
-
|
|
477
|
-
lifecycle.barrier.async(transient: true, annotation: annotation) do
|
|
478
|
-
yield
|
|
479
|
-
rescue Async::Stop, Async::Cancel
|
|
480
|
-
# normal shutdown / sibling tore us down
|
|
481
|
-
rescue Protocol::ZMTP::Error, *CONNECTION_LOST => error
|
|
482
|
-
# expected disconnect — stash reason for the :disconnected
|
|
483
|
-
# monitor event, then let the lifecycle reconnect as usual
|
|
484
|
-
lifecycle.record_disconnect_reason(error)
|
|
485
|
-
rescue => error
|
|
486
|
-
signal_fatal_error(error)
|
|
487
|
-
end
|
|
488
|
-
end
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
# Wraps an unexpected pump error as {OMQ::SocketDeadError} and
|
|
492
|
-
# unblocks any callers waiting on the recv queue. The original
|
|
493
|
-
# error is preserved as +#cause+ so callers can surface the real
|
|
494
|
-
# reason.
|
|
495
|
-
#
|
|
496
|
-
# @param error [Exception]
|
|
497
|
-
#
|
|
498
|
-
def signal_fatal_error(error)
|
|
499
|
-
return unless @lifecycle.open?
|
|
500
|
-
|
|
501
|
-
@fatal_error = build_fatal_error(error)
|
|
502
|
-
routing.unblock_recv rescue nil
|
|
503
|
-
@lifecycle.peer_connected.resolve(nil) rescue nil
|
|
504
|
-
end
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
# Constructs a SocketDeadError whose +cause+ is +error+. Uses the
|
|
508
|
-
# raise-in-rescue idiom because Ruby only sets +cause+ on an
|
|
509
|
-
# exception when it is raised from inside a rescue block -- works
|
|
510
|
-
# regardless of the original caller's +$!+ state.
|
|
511
|
-
def build_fatal_error(error)
|
|
512
|
-
raise error
|
|
513
|
-
rescue
|
|
514
|
-
begin
|
|
515
|
-
raise SocketDeadError, "#{@socket_type} socket killed: #{error.message}"
|
|
516
|
-
rescue SocketDeadError => wrapped
|
|
517
|
-
wrapped
|
|
518
|
-
end
|
|
519
|
-
end
|
|
520
|
-
|
|
521
|
-
|
|
522
531
|
# Captures the socket's task tree root and starts the socket-level
|
|
523
532
|
# maintenance task. If +parent+ is given, it is used as the parent
|
|
524
533
|
# for every task spawned under this socket (connection supervisors,
|
|
@@ -533,6 +542,8 @@ module OMQ
|
|
|
533
542
|
# @param parent [#async, nil] optional Async parent
|
|
534
543
|
#
|
|
535
544
|
def capture_parent_task(parent: nil)
|
|
545
|
+
raise_if_closed!
|
|
546
|
+
|
|
536
547
|
task = @lifecycle.capture_parent_task(parent: parent, linger: @options.linger)
|
|
537
548
|
|
|
538
549
|
return unless task
|
|
@@ -541,87 +552,6 @@ module OMQ
|
|
|
541
552
|
end
|
|
542
553
|
|
|
543
554
|
|
|
544
|
-
# Emits a lifecycle event to the monitor queue, if one is attached.
|
|
545
|
-
#
|
|
546
|
-
# @param type [Symbol] event type (e.g. :listening, :connected, :disconnected)
|
|
547
|
-
# @param endpoint [String, nil] the endpoint involved
|
|
548
|
-
# @param detail [Hash, nil] extra context
|
|
549
|
-
# @return [void]
|
|
550
|
-
#
|
|
551
|
-
def emit_monitor_event(type, endpoint: nil, detail: nil)
|
|
552
|
-
return unless @monitor_queue
|
|
553
|
-
|
|
554
|
-
event = MonitorEvent.new type: type, endpoint: endpoint, detail: detail
|
|
555
|
-
|
|
556
|
-
@monitor_queue << event
|
|
557
|
-
rescue Async::Stop, ClosedQueueError
|
|
558
|
-
end
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
# Emits a verbose-only monitor event (e.g. message traces).
|
|
562
|
-
# Only emitted when {Socket#monitor} was called with +verbose: true+.
|
|
563
|
-
# Uses +**detail+ to avoid Hash allocation when verbose is off.
|
|
564
|
-
#
|
|
565
|
-
# @param type [Symbol] event type (e.g. :message_sent, :message_received)
|
|
566
|
-
# @param detail [Hash] extra context forwarded as keyword args
|
|
567
|
-
# @return [void]
|
|
568
|
-
#
|
|
569
|
-
def emit_verbose_monitor_event(type, **detail)
|
|
570
|
-
return unless @verbose_monitor
|
|
571
|
-
emit_monitor_event(type, detail: detail)
|
|
572
|
-
end
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
# Emits a :message_sent verbose event and enriches it with the
|
|
576
|
-
# on-wire (post-compression) byte size if +conn+ exposes
|
|
577
|
-
# +last_wire_size_out+ (installed by ZMTP-Zstd etc.).
|
|
578
|
-
def emit_verbose_msg_sent(conn, parts)
|
|
579
|
-
return unless @verbose_monitor
|
|
580
|
-
|
|
581
|
-
detail = { parts: parts }
|
|
582
|
-
|
|
583
|
-
if conn.respond_to? :last_wire_size_out
|
|
584
|
-
detail[:wire_size] = conn.last_wire_size_out
|
|
585
|
-
end
|
|
586
|
-
|
|
587
|
-
emit_monitor_event :message_sent, detail: detail
|
|
588
|
-
end
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
# Emits a :message_received verbose event and enriches it with the
|
|
592
|
-
# on-wire (pre-decompression) byte size if +conn+ exposes
|
|
593
|
-
# +last_wire_size_in+.
|
|
594
|
-
def emit_verbose_msg_received(conn, parts)
|
|
595
|
-
return unless @verbose_monitor
|
|
596
|
-
|
|
597
|
-
detail = { parts: parts }
|
|
598
|
-
|
|
599
|
-
if conn.respond_to? :last_wire_size_in
|
|
600
|
-
detail[:wire_size] = conn.last_wire_size_in
|
|
601
|
-
end
|
|
602
|
-
|
|
603
|
-
emit_monitor_event :message_received, detail: detail
|
|
604
|
-
end
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
# Looks up the transport module for an endpoint URI.
|
|
608
|
-
#
|
|
609
|
-
# @param endpoint [String] endpoint URI (e.g. "tcp://...", "ruby://...")
|
|
610
|
-
# @return [Module] the transport module
|
|
611
|
-
# @raise [ArgumentError] if the scheme is not registered
|
|
612
|
-
#
|
|
613
|
-
def transport_for(endpoint)
|
|
614
|
-
scheme = scheme_for(endpoint)
|
|
615
|
-
transport = registered_transport(scheme)
|
|
616
|
-
|
|
617
|
-
unless transport
|
|
618
|
-
raise ArgumentError, "unsupported transport: #{endpoint}"
|
|
619
|
-
end
|
|
620
|
-
|
|
621
|
-
transport
|
|
622
|
-
end
|
|
623
|
-
|
|
624
|
-
|
|
625
555
|
private
|
|
626
556
|
|
|
627
557
|
|
|
@@ -634,12 +564,15 @@ module OMQ
|
|
|
634
564
|
# @param io [#read, #write, #close] accepted or dialed socket
|
|
635
565
|
# @param as_server [Boolean] true for accepted connections
|
|
636
566
|
# @param endpoint [String, nil] the endpoint URI, for monitor events
|
|
567
|
+
# @param parent [Async::Barrier, nil] owner of the connection task:
|
|
568
|
+
# the dialer's barrier for dialed connections, so {#disconnect}
|
|
569
|
+
# also stops a handshake in progress
|
|
637
570
|
#
|
|
638
|
-
def spawn_connection(io, as_server:, endpoint: nil)
|
|
639
|
-
|
|
571
|
+
def spawn_connection(io, as_server:, endpoint: nil, parent: @lifecycle.barrier)
|
|
572
|
+
parent&.async(transient: true, annotation: "conn #{endpoint}") do
|
|
640
573
|
done = Async::Promise.new
|
|
641
574
|
transport = endpoint ? transport_for(endpoint) : nil
|
|
642
|
-
lifecycle = ConnectionLifecycle.new(self, endpoint: endpoint, done: done, transport: transport)
|
|
575
|
+
lifecycle = ConnectionLifecycle.new(self, endpoint: endpoint, done: done, transport: transport, dialed: !as_server)
|
|
643
576
|
lifecycle.handshake!(io, as_server: as_server)
|
|
644
577
|
done.wait
|
|
645
578
|
rescue Async::Stop, Async::Cancel
|
|
@@ -656,31 +589,35 @@ module OMQ
|
|
|
656
589
|
|
|
657
590
|
# Blocks until the routing strategy reports all send queues drained
|
|
658
591
|
# or +timeout+ seconds have elapsed. Called from the linger path on
|
|
659
|
-
# close so pending outgoing messages get a chance to flush.
|
|
592
|
+
# close so pending outgoing messages get a chance to flush. Sleeps
|
|
593
|
+
# until {#wake_drain} says something changed.
|
|
660
594
|
#
|
|
661
595
|
# @param timeout [Numeric, nil] max seconds to wait; nil = no limit
|
|
662
596
|
#
|
|
663
|
-
# TODO: replace the 1 ms busy-poll with a promise/condition that
|
|
664
|
-
# the send pump resolves when its queue hits empty. The loop exists
|
|
665
|
-
# because there is currently no signal for "send queue fully
|
|
666
|
-
# drained"; fixing it cleanly requires plumbing a notifier through
|
|
667
|
-
# every routing strategy, so it is flagged rather than fixed here.
|
|
668
|
-
#
|
|
669
597
|
def drain_send_queues(timeout)
|
|
670
598
|
return unless @routing.respond_to?(:send_queues_drained?)
|
|
671
599
|
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
600
|
+
deadline = timeout&.finite? ? Async::Clock.now + timeout : nil # nil, Infinity: no limit
|
|
601
|
+
@drain_signal = Async::Notification.new
|
|
602
|
+
|
|
603
|
+
until @batches_sending.zero? && @routing.send_queues_drained?
|
|
604
|
+
if deadline
|
|
605
|
+
remaining = deadline - Async::Clock.now
|
|
606
|
+
break if remaining <= 0
|
|
675
607
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
608
|
+
Async::Task.current.with_timeout(remaining) { @drain_signal.wait }
|
|
609
|
+
else
|
|
610
|
+
@drain_signal.wait
|
|
611
|
+
end
|
|
679
612
|
end
|
|
613
|
+
rescue Async::TimeoutError
|
|
614
|
+
# linger expired
|
|
680
615
|
rescue Async::Stop
|
|
681
616
|
# Parent task is being cancelled — stop draining and let close
|
|
682
617
|
# proceed with the rest of teardown instead of propagating the
|
|
683
618
|
# cancellation out of the ensure path.
|
|
619
|
+
ensure
|
|
620
|
+
@drain_signal = nil
|
|
684
621
|
end
|
|
685
622
|
|
|
686
623
|
|
|
@@ -693,73 +630,37 @@ module OMQ
|
|
|
693
630
|
#
|
|
694
631
|
def schedule_reconnect(endpoint, delay: nil)
|
|
695
632
|
dialer = @dialers[endpoint]
|
|
696
|
-
Reconnect.schedule(dialer, @options,
|
|
697
|
-
end
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
# Delegates endpoint validation to the transport if it defines one.
|
|
701
|
-
# Called from {#connect} so a bad URI fails fast instead of landing
|
|
702
|
-
# in a reconnect loop.
|
|
703
|
-
#
|
|
704
|
-
# @param endpoint [String]
|
|
705
|
-
# @raise [ArgumentError] if the transport rejects the endpoint
|
|
706
|
-
#
|
|
707
|
-
def validate_endpoint!(endpoint, transport)
|
|
708
|
-
if transport.respond_to?(:validate_endpoint!)
|
|
709
|
-
transport.validate_endpoint!(endpoint)
|
|
710
|
-
end
|
|
633
|
+
Reconnect.schedule(dialer, @options, dialer_barrier(endpoint), self, delay: delay)
|
|
711
634
|
end
|
|
712
635
|
|
|
713
636
|
|
|
714
|
-
#
|
|
715
|
-
#
|
|
637
|
+
# Returns the barrier that owns +endpoint+'s reconnect loop and
|
|
638
|
+
# dialed connection tasks, nested in the socket barrier so
|
|
639
|
+
# {#close} stops them too.
|
|
716
640
|
#
|
|
717
641
|
# @param endpoint [String]
|
|
718
|
-
# @return [
|
|
719
|
-
#
|
|
720
|
-
def resolve_endpoint(endpoint)
|
|
721
|
-
transport = transport_for(endpoint)
|
|
722
|
-
if transport.respond_to?(:canonical_endpoint)
|
|
723
|
-
endpoint = transport.canonical_endpoint(endpoint)
|
|
724
|
-
end
|
|
725
|
-
|
|
726
|
-
[endpoint, transport]
|
|
727
|
-
end
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
# Best-effort normalization for disconnect/unbind. Unknown schemes
|
|
731
|
-
# stay as-is so those calls remain no-ops instead of raising.
|
|
732
|
-
#
|
|
733
|
-
# @param endpoint [String]
|
|
734
|
-
# @return [String]
|
|
735
|
-
#
|
|
736
|
-
def canonical_endpoint(endpoint)
|
|
737
|
-
transport = registered_transport(scheme_for(endpoint))
|
|
738
|
-
return endpoint unless transport&.respond_to?(:canonical_endpoint)
|
|
739
|
-
|
|
740
|
-
transport.canonical_endpoint(endpoint)
|
|
741
|
-
end
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
# Looks up a registered transport. If no real inproc transport is
|
|
745
|
-
# registered, inproc:// falls back to the Ruby in-process transport.
|
|
642
|
+
# @return [Async::Barrier]
|
|
746
643
|
#
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
#
|
|
750
|
-
def registered_transport(scheme)
|
|
751
|
-
self.class.transports[scheme] ||
|
|
752
|
-
(scheme == "inproc" ? self.class.transports["ruby"] : nil)
|
|
644
|
+
def dialer_barrier(endpoint)
|
|
645
|
+
@dialer_barriers[endpoint] ||= Async::Barrier.new(parent: @lifecycle.barrier)
|
|
753
646
|
end
|
|
754
647
|
|
|
755
648
|
|
|
756
|
-
#
|
|
649
|
+
# Ruby in-process connects are synchronous: link now, or wait up to
|
|
650
|
+
# one reconnect interval for a bind, then keep retrying in the
|
|
651
|
+
# background like any other dialer.
|
|
757
652
|
#
|
|
758
653
|
# @param endpoint [String]
|
|
759
|
-
# @
|
|
654
|
+
# @param dialer [Transport::Inproc::Dialer]
|
|
760
655
|
#
|
|
761
|
-
def
|
|
762
|
-
|
|
656
|
+
def connect_inproc(endpoint, dialer)
|
|
657
|
+
dialer.connect(wait: true)
|
|
658
|
+
rescue Errno::ECONNREFUSED
|
|
659
|
+
emit_monitor_event(:connect_delayed, endpoint: endpoint)
|
|
660
|
+
schedule_reconnect(endpoint)
|
|
661
|
+
rescue
|
|
662
|
+
@dialers.delete(endpoint)
|
|
663
|
+
raise
|
|
763
664
|
end
|
|
764
665
|
|
|
765
666
|
|
|
@@ -784,6 +685,22 @@ module OMQ
|
|
|
784
685
|
def stop_listeners
|
|
785
686
|
@listeners.each_value(&:stop)
|
|
786
687
|
@listeners.clear
|
|
688
|
+
@bind_aliases.clear
|
|
689
|
+
end
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
# Lets {#unbind} find a listener by the string the caller passed to
|
|
693
|
+
# {#bind}. Ephemeral-port requests (+:0+, +:*+) can bind many
|
|
694
|
+
# listeners, so they don't get an alias.
|
|
695
|
+
#
|
|
696
|
+
# @param requested [String] canonical endpoint passed to #bind
|
|
697
|
+
# @param bound [String] the listener's resolved endpoint
|
|
698
|
+
#
|
|
699
|
+
def remember_bind_alias(requested, bound)
|
|
700
|
+
return if requested == bound
|
|
701
|
+
return if requested.end_with?(":0", ":*")
|
|
702
|
+
|
|
703
|
+
@bind_aliases[requested] = bound
|
|
787
704
|
end
|
|
788
705
|
|
|
789
706
|
|
|
@@ -800,21 +717,40 @@ module OMQ
|
|
|
800
717
|
# Cascades teardown through the socket-level barrier. Stopping the
|
|
801
718
|
# barrier cancels every tracked task: connection supervisors (whose
|
|
802
719
|
# `ensure lost!` runs the ordered disconnect side effects), accept
|
|
803
|
-
# loops, heartbeat, maintenance
|
|
804
|
-
#
|
|
720
|
+
# loops, heartbeat, maintenance, and the per-endpoint dialer
|
|
721
|
+
# barriers with their reconnect loops.
|
|
805
722
|
#
|
|
806
723
|
def tear_down_barrier
|
|
807
724
|
@lifecycle.barrier&.stop
|
|
808
725
|
end
|
|
809
726
|
|
|
810
727
|
|
|
811
|
-
#
|
|
812
|
-
# +nil+ sentinel, so consumers iterating with {Socket#each_event}
|
|
813
|
-
# can exit cleanly.
|
|
728
|
+
# @raise [SocketClosedError] once {#close} or {#stop} has started
|
|
814
729
|
#
|
|
815
|
-
def
|
|
816
|
-
return
|
|
817
|
-
|
|
730
|
+
def raise_if_closed!
|
|
731
|
+
return if @lifecycle.alive?
|
|
732
|
+
|
|
733
|
+
raise SocketClosedError, "#{@socket_type} socket is closed"
|
|
818
734
|
end
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
# Closes a socket that never bound or connected: there are no tasks
|
|
738
|
+
# to stop, but later calls must see it closed.
|
|
739
|
+
#
|
|
740
|
+
def close_unopened
|
|
741
|
+
@lifecycle.finish_closing!
|
|
742
|
+
close_routing_queues
|
|
743
|
+
emit_monitor_event(:closed)
|
|
744
|
+
close_monitor_queue
|
|
745
|
+
end
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
# Wakes fibers blocked in a send (they raise {SocketClosedError}) or
|
|
749
|
+
# a receive (they get +nil+).
|
|
750
|
+
#
|
|
751
|
+
def close_routing_queues
|
|
752
|
+
@routing.close_queues if @routing.respond_to?(:close_queues)
|
|
753
|
+
end
|
|
754
|
+
|
|
819
755
|
end
|
|
820
756
|
end
|