omq 0.29.1 → 0.30.1
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 +181 -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 +255 -304
- 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 +105 -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
|
@@ -12,8 +12,7 @@ module OMQ
|
|
|
12
12
|
# which is strictly better than libzmq's strict per-pipe round-robin
|
|
13
13
|
# for PUSH-style patterns.
|
|
14
14
|
#
|
|
15
|
-
# See doc/
|
|
16
|
-
# full reasoning.
|
|
15
|
+
# See doc/ARCHITECTURE.md "Send queues and HWM".
|
|
17
16
|
#
|
|
18
17
|
# Including classes must call `init_round_robin(engine)` from
|
|
19
18
|
# their #initialize.
|
|
@@ -22,11 +21,12 @@ module OMQ
|
|
|
22
21
|
BATCH_MSG_CAP = 256
|
|
23
22
|
BATCH_BYTE_CAP = 512 * 1024
|
|
24
23
|
|
|
25
|
-
# @return [Boolean] true when the shared send queue is empty
|
|
26
|
-
#
|
|
24
|
+
# @return [Boolean] true when the shared send queue is empty.
|
|
25
|
+
# Batches a pump is still writing are counted by
|
|
26
|
+
# {Engine#sending_batch}.
|
|
27
27
|
#
|
|
28
28
|
def send_queues_drained?
|
|
29
|
-
@send_queue.empty?
|
|
29
|
+
@send_queue.empty?
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
|
|
@@ -41,7 +41,6 @@ module OMQ
|
|
|
41
41
|
@connections = []
|
|
42
42
|
@send_queue = Routing.build_queue(engine.options.send_hwm, :block)
|
|
43
43
|
@direct_pipe = nil
|
|
44
|
-
@in_flight = 0 # messages dequeued but not yet written
|
|
45
44
|
end
|
|
46
45
|
|
|
47
46
|
|
|
@@ -85,11 +84,18 @@ module OMQ
|
|
|
85
84
|
# and writes directly to the peer's recv queue. Otherwise blocks
|
|
86
85
|
# on the shared bounded send queue (backpressure when full).
|
|
87
86
|
#
|
|
87
|
+
# If the peer closed before this socket noticed, the message goes
|
|
88
|
+
# to the send queue and waits for the next peer.
|
|
89
|
+
#
|
|
88
90
|
def enqueue_round_robin(parts)
|
|
89
91
|
pipe = @direct_pipe
|
|
90
92
|
|
|
91
93
|
if pipe&.direct_recv_queue
|
|
92
|
-
|
|
94
|
+
begin
|
|
95
|
+
pipe.send_message(transform_send(parts))
|
|
96
|
+
rescue IOError
|
|
97
|
+
@send_queue.enqueue(parts)
|
|
98
|
+
end
|
|
93
99
|
else
|
|
94
100
|
@send_queue.enqueue(parts)
|
|
95
101
|
end
|
|
@@ -135,14 +141,8 @@ module OMQ
|
|
|
135
141
|
batch = []
|
|
136
142
|
|
|
137
143
|
loop do
|
|
138
|
-
dequeue_batch_capped(batch)
|
|
139
|
-
@
|
|
140
|
-
|
|
141
|
-
begin
|
|
142
|
-
write_batch(conn, batch)
|
|
143
|
-
ensure
|
|
144
|
-
@in_flight -= batch.size
|
|
145
|
-
end
|
|
144
|
+
dequeue_batch_capped(batch) or break
|
|
145
|
+
@engine.sending_batch { write_batch(conn, batch) }
|
|
146
146
|
|
|
147
147
|
batch.each do |parts|
|
|
148
148
|
@engine.emit_verbose_msg_sent(conn, parts)
|
|
@@ -155,9 +155,12 @@ module OMQ
|
|
|
155
155
|
end
|
|
156
156
|
|
|
157
157
|
|
|
158
|
+
# @return [Array, nil] +batch+, or nil once the send queue is closed
|
|
159
|
+
#
|
|
158
160
|
def dequeue_batch_capped(batch = [])
|
|
159
|
-
|
|
160
|
-
|
|
161
|
+
first = @send_queue.dequeue or return
|
|
162
|
+
batch << first
|
|
163
|
+
bytes = batch_bytes(first)
|
|
161
164
|
|
|
162
165
|
while batch.size < BATCH_MSG_CAP && bytes < BATCH_BYTE_CAP
|
|
163
166
|
msg = @send_queue.dequeue(timeout: 0)
|
|
@@ -165,6 +168,8 @@ module OMQ
|
|
|
165
168
|
batch << msg
|
|
166
169
|
bytes += batch_bytes(msg)
|
|
167
170
|
end
|
|
171
|
+
|
|
172
|
+
batch
|
|
168
173
|
end
|
|
169
174
|
|
|
170
175
|
|
data/lib/omq/routing/router.rb
CHANGED
|
@@ -8,9 +8,13 @@ module OMQ
|
|
|
8
8
|
# ROUTER socket routing: identity-based routing.
|
|
9
9
|
#
|
|
10
10
|
# Prepends peer identity frame on receive. Uses first frame as
|
|
11
|
-
# routing identity on send.
|
|
11
|
+
# routing identity on send. A dialed peer keeps its identity and
|
|
12
|
+
# queue across reconnects ({IdentityRouting}).
|
|
12
13
|
#
|
|
13
14
|
class Router
|
|
15
|
+
include ClosableQueues
|
|
16
|
+
include IdentityRouting
|
|
17
|
+
|
|
14
18
|
# @return [Async::LimitedQueue]
|
|
15
19
|
#
|
|
16
20
|
attr_reader :recv_queue
|
|
@@ -19,11 +23,10 @@ module OMQ
|
|
|
19
23
|
# @param engine [Engine]
|
|
20
24
|
#
|
|
21
25
|
def initialize(engine)
|
|
22
|
-
@engine
|
|
23
|
-
@recv_queue
|
|
24
|
-
@
|
|
25
|
-
|
|
26
|
-
@conn_queues = {}
|
|
26
|
+
@engine = engine
|
|
27
|
+
@recv_queue = Routing.build_queue(engine.options.recv_hwm, :block)
|
|
28
|
+
@next_routing_id = SecureRandom.random_number(1 << 32)
|
|
29
|
+
init_identity_routing(engine)
|
|
27
30
|
end
|
|
28
31
|
|
|
29
32
|
|
|
@@ -48,49 +51,64 @@ module OMQ
|
|
|
48
51
|
# @param connection [Protocol::ZMTP::Connection]
|
|
49
52
|
#
|
|
50
53
|
def connection_added(connection)
|
|
51
|
-
identity = connection.
|
|
52
|
-
identity = SecureRandom.bytes(5) if identity.nil? || identity.empty?
|
|
53
|
-
@connections_by_identity[identity] = connection
|
|
54
|
-
@identity_by_connection[connection] = identity
|
|
54
|
+
identity = attach_route(connection, requested_identity(connection)).id
|
|
55
55
|
|
|
56
56
|
@engine.start_recv_pump(connection, @recv_queue) { |msg| [identity, *msg] }
|
|
57
|
-
|
|
58
|
-
q = Routing.build_queue(@engine.options.send_hwm, :block)
|
|
59
|
-
@conn_queues[connection] = q
|
|
60
|
-
ConnSendPump.start(@engine, connection, q)
|
|
61
57
|
end
|
|
62
58
|
|
|
63
59
|
|
|
64
60
|
# @param connection [Protocol::ZMTP::Connection]
|
|
65
61
|
#
|
|
66
62
|
def connection_removed(connection)
|
|
67
|
-
|
|
68
|
-
@connections_by_identity.delete(identity) if identity
|
|
69
|
-
@conn_queues.delete(connection)
|
|
63
|
+
detach_route(connection)
|
|
70
64
|
end
|
|
71
65
|
|
|
72
66
|
|
|
73
67
|
# Enqueues a message for sending. The first frame is the routing identity.
|
|
74
68
|
#
|
|
69
|
+
# Like libzmq, a ROUTER without +router_mandatory+ drops a message
|
|
70
|
+
# whose peer is unknown or at HWM. With +router_mandatory+ it
|
|
71
|
+
# raises for an unknown peer and blocks at HWM. A dialed peer that
|
|
72
|
+
# is down is still known: its sends queue up.
|
|
73
|
+
#
|
|
75
74
|
# @param parts [Array<String>]
|
|
76
75
|
#
|
|
77
76
|
def enqueue(parts)
|
|
78
|
-
identity
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
identity = parts.first
|
|
78
|
+
mandatory = @engine.options.router_mandatory?
|
|
79
|
+
route = route_to(identity)
|
|
80
|
+
|
|
81
|
+
unless route
|
|
82
|
+
raise SocketError, "no route to identity #{identity.inspect}" if mandatory
|
|
83
|
+
return
|
|
83
84
|
end
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
|
|
86
|
+
queue = route.queue
|
|
87
|
+
return if !mandatory && queue.is_a?(Async::LimitedQueue) && queue.limited?
|
|
88
|
+
|
|
89
|
+
Routing.enqueue_to_peer(queue, parts[1..])
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# The identity the peer set, unless empty or zero-prefixed (those
|
|
97
|
+
# are reserved for generated ids).
|
|
98
|
+
#
|
|
99
|
+
def requested_identity(connection)
|
|
100
|
+
identity = connection.peer_identity
|
|
101
|
+
identity if identity && !identity.empty? && !identity.getbyte(0).zero?
|
|
87
102
|
end
|
|
88
103
|
|
|
89
104
|
|
|
90
|
-
#
|
|
105
|
+
# Routing id for a peer without a usable identity: a zero byte and
|
|
106
|
+
# a 32-bit counter, as in libzmq. Peers can't claim identities
|
|
107
|
+
# starting with a zero byte, so these never collide with theirs.
|
|
91
108
|
#
|
|
92
|
-
def
|
|
93
|
-
@
|
|
109
|
+
def generate_routing_id
|
|
110
|
+
@next_routing_id = (@next_routing_id + 1) & 0xFFFF_FFFF
|
|
111
|
+
[0, @next_routing_id].pack("CN")
|
|
94
112
|
end
|
|
95
113
|
|
|
96
114
|
end
|
data/lib/omq/routing/scatter.rb
CHANGED
data/lib/omq/routing/server.rb
CHANGED
|
@@ -8,9 +8,13 @@ module OMQ
|
|
|
8
8
|
# 4-byte routing IDs.
|
|
9
9
|
#
|
|
10
10
|
# Prepends routing ID on receive. Strips routing ID on send and
|
|
11
|
-
# routes to the identified connection.
|
|
11
|
+
# routes to the identified connection. A dialed peer keeps its
|
|
12
|
+
# routing ID and queue across reconnects ({IdentityRouting}).
|
|
12
13
|
#
|
|
13
14
|
class Server
|
|
15
|
+
include ClosableQueues
|
|
16
|
+
include IdentityRouting
|
|
17
|
+
|
|
14
18
|
# @return [Async::LimitedQueue]
|
|
15
19
|
#
|
|
16
20
|
attr_reader :recv_queue
|
|
@@ -19,11 +23,9 @@ module OMQ
|
|
|
19
23
|
# @param engine [Engine]
|
|
20
24
|
#
|
|
21
25
|
def initialize(engine)
|
|
22
|
-
@engine
|
|
23
|
-
@recv_queue
|
|
24
|
-
|
|
25
|
-
@routing_id_by_connection = {}
|
|
26
|
-
@conn_queues = {}
|
|
26
|
+
@engine = engine
|
|
27
|
+
@recv_queue = Routing.build_queue(engine.options.recv_hwm, :block)
|
|
28
|
+
init_identity_routing(engine)
|
|
27
29
|
end
|
|
28
30
|
|
|
29
31
|
|
|
@@ -48,41 +50,35 @@ module OMQ
|
|
|
48
50
|
# @param connection [Protocol::ZMTP::Connection]
|
|
49
51
|
#
|
|
50
52
|
def connection_added(connection)
|
|
51
|
-
routing_id =
|
|
52
|
-
@connections_by_routing_id[routing_id] = connection
|
|
53
|
-
@routing_id_by_connection[connection] = routing_id
|
|
54
|
-
|
|
55
|
-
@engine.start_recv_pump(connection, @recv_queue) { |msg| [routing_id, *msg] }
|
|
53
|
+
routing_id = attach_route(connection).id
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
@
|
|
59
|
-
ConnSendPump.start(@engine, connection, q)
|
|
55
|
+
# Single-frame socket: drop multipart input from a bad peer.
|
|
56
|
+
@engine.start_recv_pump(connection, @recv_queue) { |msg| [routing_id, msg.first] if msg.size == 1 }
|
|
60
57
|
end
|
|
61
58
|
|
|
62
59
|
|
|
63
60
|
# @param connection [Protocol::ZMTP::Connection]
|
|
64
61
|
#
|
|
65
62
|
def connection_removed(connection)
|
|
66
|
-
|
|
67
|
-
@connections_by_routing_id.delete(routing_id) if routing_id
|
|
68
|
-
@conn_queues.delete(connection)
|
|
63
|
+
detach_route(connection)
|
|
69
64
|
end
|
|
70
65
|
|
|
71
66
|
|
|
72
67
|
# @param parts [Array<String>]
|
|
73
68
|
#
|
|
74
69
|
def enqueue(parts)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
route = route_to(parts.first)
|
|
71
|
+
return unless route
|
|
72
|
+
|
|
73
|
+
Routing.enqueue_to_peer(route.queue, parts[1..])
|
|
79
74
|
end
|
|
80
75
|
|
|
81
76
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def generate_routing_id
|
|
81
|
+
SecureRandom.bytes(4)
|
|
86
82
|
end
|
|
87
83
|
|
|
88
84
|
end
|
data/lib/omq/routing/sub.rb
CHANGED
|
@@ -7,6 +7,8 @@ module OMQ
|
|
|
7
7
|
# Sends SUBSCRIBE/CANCEL commands to connected PUB peers.
|
|
8
8
|
#
|
|
9
9
|
class Sub
|
|
10
|
+
include ClosableQueues
|
|
11
|
+
|
|
10
12
|
|
|
11
13
|
# @return [Async::LimitedQueue]
|
|
12
14
|
#
|
|
@@ -19,7 +21,7 @@ module OMQ
|
|
|
19
21
|
@engine = engine
|
|
20
22
|
@connections = Set.new
|
|
21
23
|
@recv_queue = Routing.build_queue(engine.options.recv_hwm, :block)
|
|
22
|
-
@subscriptions =
|
|
24
|
+
@subscriptions = Hash.new(0) # prefix => times subscribed, like libzmq
|
|
23
25
|
end
|
|
24
26
|
|
|
25
27
|
|
|
@@ -46,7 +48,7 @@ module OMQ
|
|
|
46
48
|
def connection_added(connection)
|
|
47
49
|
@connections << connection
|
|
48
50
|
|
|
49
|
-
@subscriptions.each do |prefix|
|
|
51
|
+
@subscriptions.keys.each do |prefix|
|
|
50
52
|
send_subscribe(connection, prefix)
|
|
51
53
|
end
|
|
52
54
|
|
|
@@ -68,23 +70,33 @@ module OMQ
|
|
|
68
70
|
end
|
|
69
71
|
|
|
70
72
|
|
|
71
|
-
# Subscribes to a topic prefix.
|
|
73
|
+
# Subscribes to a topic prefix. Subscriptions are counted: each
|
|
74
|
+
# #subscribe needs its own #unsubscribe, as in libzmq.
|
|
72
75
|
#
|
|
73
76
|
# @param prefix [String]
|
|
74
77
|
#
|
|
75
78
|
def subscribe(prefix)
|
|
76
|
-
@subscriptions
|
|
77
|
-
@connections.each { |conn| send_subscribe(conn, prefix) }
|
|
79
|
+
@subscriptions[prefix] += 1
|
|
80
|
+
@connections.to_a.each { |conn| send_subscribe(conn, prefix) }
|
|
78
81
|
end
|
|
79
82
|
|
|
80
83
|
|
|
81
|
-
# Unsubscribes from a topic prefix.
|
|
84
|
+
# Unsubscribes from a topic prefix. Peers are told only once the
|
|
85
|
+
# last subscription to +prefix+ is gone.
|
|
82
86
|
#
|
|
83
87
|
# @param prefix [String]
|
|
84
88
|
#
|
|
85
89
|
def unsubscribe(prefix)
|
|
90
|
+
count = @subscriptions[prefix]
|
|
91
|
+
return if count.zero?
|
|
92
|
+
|
|
93
|
+
if count > 1
|
|
94
|
+
@subscriptions[prefix] = count - 1
|
|
95
|
+
return
|
|
96
|
+
end
|
|
97
|
+
|
|
86
98
|
@subscriptions.delete(prefix)
|
|
87
|
-
@connections.each { |conn| send_cancel(conn, prefix) }
|
|
99
|
+
@connections.to_a.each { |conn| send_cancel(conn, prefix) }
|
|
88
100
|
end
|
|
89
101
|
|
|
90
102
|
|
|
@@ -94,12 +106,17 @@ module OMQ
|
|
|
94
106
|
# Sends a SUBSCRIBE to +conn+ using the wire form the peer understands:
|
|
95
107
|
# command-form for ZMTP 3.1+, legacy message-form for ZMTP 3.0.
|
|
96
108
|
#
|
|
109
|
+
# A peer that is going away can't take the command; its lifecycle
|
|
110
|
+
# cleans up, and the caller of #subscribe must not see the error.
|
|
111
|
+
#
|
|
97
112
|
def send_subscribe(conn, prefix)
|
|
98
113
|
if conn.peer_minor >= 1
|
|
99
114
|
conn.send_command(Protocol::ZMTP::Codec::Command.subscribe(prefix))
|
|
100
115
|
else
|
|
101
116
|
conn.send_message([Protocol::ZMTP::Codec::Subscription.body(prefix)])
|
|
102
117
|
end
|
|
118
|
+
rescue *CONNECTION_LOST
|
|
119
|
+
# peer disconnecting
|
|
103
120
|
end
|
|
104
121
|
|
|
105
122
|
|
|
@@ -109,6 +126,8 @@ module OMQ
|
|
|
109
126
|
else
|
|
110
127
|
conn.send_message([Protocol::ZMTP::Codec::Subscription.body(prefix, cancel: true)])
|
|
111
128
|
end
|
|
129
|
+
rescue *CONNECTION_LOST
|
|
130
|
+
# peer disconnecting
|
|
112
131
|
end
|
|
113
132
|
|
|
114
133
|
end
|
data/lib/omq/routing/xpub.rb
CHANGED
|
@@ -13,6 +13,7 @@ module OMQ
|
|
|
13
13
|
#
|
|
14
14
|
class XPub
|
|
15
15
|
include FanOut
|
|
16
|
+
include ClosableQueues
|
|
16
17
|
|
|
17
18
|
# @return [Async::LimitedQueue]
|
|
18
19
|
#
|
|
@@ -45,7 +46,7 @@ module OMQ
|
|
|
45
46
|
#
|
|
46
47
|
def connection_added(connection)
|
|
47
48
|
@connections << connection
|
|
48
|
-
@subscriptions[connection] =
|
|
49
|
+
@subscriptions[connection] = PrefixSet.new
|
|
49
50
|
start_subscription_listener(connection)
|
|
50
51
|
add_fan_out_send_connection(connection)
|
|
51
52
|
end
|
|
@@ -55,8 +56,9 @@ module OMQ
|
|
|
55
56
|
#
|
|
56
57
|
def connection_removed(connection)
|
|
57
58
|
@connections.delete(connection)
|
|
58
|
-
@subscriptions.delete(connection)
|
|
59
|
+
prefixes = @subscriptions.delete(connection)
|
|
59
60
|
remove_fan_out_send_connection(connection)
|
|
61
|
+
report_lost_subscriptions(prefixes)
|
|
60
62
|
end
|
|
61
63
|
|
|
62
64
|
|
|
@@ -80,6 +82,27 @@ module OMQ
|
|
|
80
82
|
|
|
81
83
|
# Expose unsubscription to application as data message.
|
|
82
84
|
#
|
|
85
|
+
# A lost subscriber's subscriptions end with it. Report a cancel
|
|
86
|
+
# per prefix, as libzmq does, so a forwarder can drop them
|
|
87
|
+
# upstream. Queued from its own task: teardown must not block on a
|
|
88
|
+
# full receive queue.
|
|
89
|
+
#
|
|
90
|
+
# @param prefixes [PrefixSet, nil]
|
|
91
|
+
#
|
|
92
|
+
def report_lost_subscriptions(prefixes)
|
|
93
|
+
return if prefixes.nil? || prefixes.empty?
|
|
94
|
+
return if @engine.closed?
|
|
95
|
+
|
|
96
|
+
cancels = prefixes.map { |prefix| ["\x00#{prefix}".b] }
|
|
97
|
+
|
|
98
|
+
@engine.barrier.async(transient: true, annotation: "xpub lost subscriptions") do
|
|
99
|
+
cancels.each { |msg| @recv_queue.enqueue(msg) }
|
|
100
|
+
rescue Async::Queue::ClosedError
|
|
101
|
+
# socket closed meanwhile
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
|
|
83
106
|
def on_cancel(conn, prefix)
|
|
84
107
|
super
|
|
85
108
|
@recv_queue.enqueue(["\x00#{prefix}".b])
|
data/lib/omq/routing/xsub.rb
CHANGED
|
@@ -9,6 +9,8 @@ module OMQ
|
|
|
9
9
|
# queue so subscription commands are delivered independently per peer.
|
|
10
10
|
#
|
|
11
11
|
class XSub
|
|
12
|
+
include ClosableQueues
|
|
13
|
+
|
|
12
14
|
|
|
13
15
|
# @return [Async::LimitedQueue]
|
|
14
16
|
#
|
|
@@ -22,6 +24,7 @@ module OMQ
|
|
|
22
24
|
@connections = Set.new
|
|
23
25
|
@recv_queue = Routing.build_queue(engine.options.recv_hwm, :block)
|
|
24
26
|
@conn_queues = {}
|
|
27
|
+
@subscriptions = Hash.new(0) # prefix => times subscribed, replayed to new peers
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
|
|
@@ -52,7 +55,7 @@ module OMQ
|
|
|
52
55
|
|
|
53
56
|
q = Routing.build_queue(@engine.options.send_hwm, :block)
|
|
54
57
|
@conn_queues[connection] = q
|
|
55
|
-
start_conn_send_pump(connection, q)
|
|
58
|
+
start_conn_send_pump(connection, q, @subscriptions.keys)
|
|
56
59
|
end
|
|
57
60
|
|
|
58
61
|
|
|
@@ -60,7 +63,7 @@ module OMQ
|
|
|
60
63
|
#
|
|
61
64
|
def connection_removed(connection)
|
|
62
65
|
@connections.delete(connection)
|
|
63
|
-
@conn_queues.delete(connection)
|
|
66
|
+
@conn_queues.delete(connection)&.close
|
|
64
67
|
end
|
|
65
68
|
|
|
66
69
|
|
|
@@ -68,9 +71,15 @@ module OMQ
|
|
|
68
71
|
#
|
|
69
72
|
# @param parts [Array<String>]
|
|
70
73
|
#
|
|
74
|
+
# Subscriptions are recorded, like libzmq's XSUB, so a publisher
|
|
75
|
+
# that connects (or reconnects) later gets them too. A cancel goes
|
|
76
|
+
# out only when the last subscription to its prefix is gone.
|
|
77
|
+
#
|
|
71
78
|
def enqueue(parts)
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
return unless track_subscription(parts.first)
|
|
80
|
+
|
|
81
|
+
@connections.to_a.each do |conn|
|
|
82
|
+
Routing.enqueue_to_peer(@conn_queues[conn], parts)
|
|
74
83
|
end
|
|
75
84
|
end
|
|
76
85
|
|
|
@@ -85,37 +94,77 @@ module OMQ
|
|
|
85
94
|
private
|
|
86
95
|
|
|
87
96
|
|
|
88
|
-
|
|
97
|
+
# Updates the subscription table for an outgoing message.
|
|
98
|
+
#
|
|
99
|
+
# @param frame [String, nil] first frame: 0x01/0x00 + prefix
|
|
100
|
+
# @return [Boolean] whether to forward the message to the peers
|
|
101
|
+
#
|
|
102
|
+
def track_subscription(frame)
|
|
103
|
+
return true if frame.nil? || frame.empty?
|
|
104
|
+
|
|
105
|
+
prefix = frame.byteslice(1..).b
|
|
106
|
+
|
|
107
|
+
case frame.getbyte(0)
|
|
108
|
+
when 0x01
|
|
109
|
+
@subscriptions[prefix] += 1
|
|
110
|
+
true
|
|
111
|
+
when 0x00
|
|
112
|
+
count = @subscriptions[prefix]
|
|
113
|
+
return false if count.zero?
|
|
114
|
+
|
|
115
|
+
if count > 1
|
|
116
|
+
@subscriptions[prefix] = count - 1
|
|
117
|
+
return false
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
@subscriptions.delete(prefix)
|
|
121
|
+
true
|
|
122
|
+
else
|
|
123
|
+
true
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
# @param replay [Array<String>] prefixes to subscribe first
|
|
129
|
+
#
|
|
130
|
+
def start_conn_send_pump(conn, q, replay)
|
|
89
131
|
@engine.spawn_conn_pump_task(conn, annotation: "send pump") do
|
|
132
|
+
replay.each do |prefix|
|
|
133
|
+
write_subscription(conn, 0x01, prefix)
|
|
134
|
+
end
|
|
135
|
+
|
|
90
136
|
loop do
|
|
91
|
-
parts = q.dequeue
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
next if frame.nil? || frame.empty?
|
|
95
|
-
|
|
96
|
-
flag = frame.getbyte(0)
|
|
97
|
-
prefix = frame.byteslice(1..) || "".b
|
|
98
|
-
|
|
99
|
-
case flag
|
|
100
|
-
when 0x01
|
|
101
|
-
if conn.peer_minor >= 1
|
|
102
|
-
conn.send_command(Protocol::ZMTP::Codec::Command.subscribe(prefix))
|
|
103
|
-
else
|
|
104
|
-
conn.send_message([frame])
|
|
105
|
-
end
|
|
106
|
-
when 0x00
|
|
107
|
-
if conn.peer_minor >= 1
|
|
108
|
-
conn.send_command(Protocol::ZMTP::Codec::Command.cancel(prefix))
|
|
109
|
-
else
|
|
110
|
-
conn.send_message([frame])
|
|
111
|
-
end
|
|
112
|
-
else
|
|
113
|
-
next
|
|
114
|
-
end
|
|
137
|
+
parts = q.dequeue or break # queue closed: peer gone
|
|
138
|
+
@engine.sending_batch { forward_subscription(conn, parts.first&.b) }
|
|
115
139
|
end
|
|
116
140
|
end
|
|
117
141
|
end
|
|
118
142
|
|
|
143
|
+
|
|
144
|
+
# Sends a queued 0x01/0x00 + prefix frame on; drops anything else.
|
|
145
|
+
#
|
|
146
|
+
def forward_subscription(conn, frame)
|
|
147
|
+
return if frame.nil? || frame.empty?
|
|
148
|
+
|
|
149
|
+
flag = frame.getbyte(0)
|
|
150
|
+
return unless flag == 0x01 || flag == 0x00
|
|
151
|
+
|
|
152
|
+
write_subscription(conn, flag, frame.byteslice(1..) || "".b)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
# Writes a SUBSCRIBE (+flag+ 0x01) or CANCEL (0x00) in the form
|
|
157
|
+
# the peer understands: a command for ZMTP 3.1, a message for 3.0.
|
|
158
|
+
#
|
|
159
|
+
def write_subscription(conn, flag, prefix)
|
|
160
|
+
if conn.peer_minor >= 1
|
|
161
|
+
command = flag == 0x01 ? Protocol::ZMTP::Codec::Command.subscribe(prefix) : Protocol::ZMTP::Codec::Command.cancel(prefix)
|
|
162
|
+
conn.send_command(command)
|
|
163
|
+
else
|
|
164
|
+
conn.send_message([flag.chr.b + prefix.b])
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
119
168
|
end
|
|
120
169
|
end
|
|
121
170
|
end
|
data/lib/omq/routing.rb
CHANGED
|
@@ -5,6 +5,9 @@ require "async/queue"
|
|
|
5
5
|
require "async/limited_queue"
|
|
6
6
|
require_relative "drop_queue"
|
|
7
7
|
require_relative "routing/conn_send_pump"
|
|
8
|
+
require_relative "routing/closable_queues"
|
|
9
|
+
require_relative "routing/prefix_set"
|
|
10
|
+
require_relative "routing/identity_routing"
|
|
8
11
|
|
|
9
12
|
module OMQ
|
|
10
13
|
# Routing strategies for each ZMQ socket type.
|
|
@@ -13,6 +16,19 @@ module OMQ
|
|
|
13
16
|
# the socket's send/recv queues.
|
|
14
17
|
#
|
|
15
18
|
module Routing
|
|
19
|
+
# Raised by #connection_added when a strategy can't take another
|
|
20
|
+
# peer (PAIR, CHANNEL). The connection is closed and, if this side
|
|
21
|
+
# dialed, redialed later.
|
|
22
|
+
#
|
|
23
|
+
class PeerRefusedError < RuntimeError
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Errors an enqueue raises once the queue was closed: the peer it
|
|
28
|
+
# belonged to is gone, or the socket closed.
|
|
29
|
+
QUEUE_CLOSED_ERRORS = [Async::Queue::ClosedError, ClosedQueueError].freeze
|
|
30
|
+
|
|
31
|
+
|
|
16
32
|
# Plugin registry for socket types not built into omq.
|
|
17
33
|
# Populated by sister gems via +Routing.register+.
|
|
18
34
|
#
|
|
@@ -36,6 +52,22 @@ module OMQ
|
|
|
36
52
|
end
|
|
37
53
|
|
|
38
54
|
|
|
55
|
+
# Refuses one more subscription prefix or group from a peer that
|
|
56
|
+
# already holds +limit+. The raised error drops the peer.
|
|
57
|
+
#
|
|
58
|
+
# @param held [Set<String>] what the peer holds now
|
|
59
|
+
# @param item [String] the prefix or group it asks for
|
|
60
|
+
# @param limit [Integer, nil] max_peer_subscriptions
|
|
61
|
+
# @raise [Protocol::ZMTP::Error]
|
|
62
|
+
#
|
|
63
|
+
def self.check_peer_subscriptions!(held, item, limit)
|
|
64
|
+
return unless limit
|
|
65
|
+
return if held.size < limit || held.include?(item)
|
|
66
|
+
|
|
67
|
+
raise Protocol::ZMTP::Error, "peer exceeds max_peer_subscriptions #{limit}"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
|
|
39
71
|
# Builds a send or recv queue based on the mute strategy.
|
|
40
72
|
#
|
|
41
73
|
# @param hwm [Integer] high water mark
|
|
@@ -66,15 +98,33 @@ module OMQ
|
|
|
66
98
|
#
|
|
67
99
|
# @param queue [Async::LimitedQueue]
|
|
68
100
|
# @param batch [Array]
|
|
69
|
-
# @return [
|
|
101
|
+
# @return [Array, nil] +batch+, or nil once +queue+ is closed and
|
|
102
|
+
# empty (the pump should stop)
|
|
70
103
|
#
|
|
71
104
|
def self.dequeue_batch(queue, batch = [])
|
|
72
|
-
|
|
105
|
+
first = queue.dequeue or return
|
|
106
|
+
batch << first
|
|
73
107
|
|
|
74
108
|
loop do
|
|
75
109
|
msg = queue.dequeue(timeout: 0) or break
|
|
76
110
|
batch << msg
|
|
77
111
|
end
|
|
112
|
+
|
|
113
|
+
batch
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# Enqueues into a per-connection queue, dropping the message if the
|
|
118
|
+
# peer left while the caller was blocked at HWM.
|
|
119
|
+
#
|
|
120
|
+
# @param queue [Async::LimitedQueue, DropQueue, nil]
|
|
121
|
+
# @param parts [Array<String>]
|
|
122
|
+
# @return [void]
|
|
123
|
+
#
|
|
124
|
+
def self.enqueue_to_peer(queue, parts)
|
|
125
|
+
queue&.enqueue(parts)
|
|
126
|
+
rescue *QUEUE_CLOSED_ERRORS
|
|
127
|
+
# peer disconnected; its queue is gone
|
|
78
128
|
end
|
|
79
129
|
|
|
80
130
|
|