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/routing/pair.rb
CHANGED
|
@@ -10,6 +10,8 @@ module OMQ
|
|
|
10
10
|
# the in-flight batch is dropped (matching libzmq).
|
|
11
11
|
#
|
|
12
12
|
class Pair
|
|
13
|
+
include ClosableQueues
|
|
14
|
+
|
|
13
15
|
# @return [Async::LimitedQueue]
|
|
14
16
|
#
|
|
15
17
|
attr_reader :recv_queue
|
|
@@ -44,17 +46,17 @@ module OMQ
|
|
|
44
46
|
|
|
45
47
|
|
|
46
48
|
# @param connection [Protocol::ZMTP::Connection]
|
|
47
|
-
# @raise [
|
|
49
|
+
# @raise [PeerRefusedError] if a connection already exists
|
|
48
50
|
#
|
|
49
51
|
def connection_added(connection)
|
|
50
|
-
raise "PAIR allows only one peer" if @connection
|
|
52
|
+
raise PeerRefusedError, "PAIR allows only one peer" if @connection
|
|
51
53
|
@connection = connection
|
|
52
54
|
|
|
53
55
|
@engine.start_recv_pump(connection, @recv_queue)
|
|
54
56
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
# Inproc pipes take the direct path in #enqueue, but the pump
|
|
58
|
+
# still drains messages queued while there was no peer.
|
|
59
|
+
start_send_pump(connection)
|
|
58
60
|
end
|
|
59
61
|
|
|
60
62
|
|
|
@@ -67,9 +69,13 @@ module OMQ
|
|
|
67
69
|
|
|
68
70
|
# @param parts [Array<String>]
|
|
69
71
|
#
|
|
72
|
+
# Inproc peers get messages written straight into their receive
|
|
73
|
+
# queue, unless older messages still wait in the send queue.
|
|
74
|
+
#
|
|
70
75
|
def enqueue(parts)
|
|
71
76
|
conn = @connection
|
|
72
|
-
|
|
77
|
+
|
|
78
|
+
if conn.is_a?(Transport::Inproc::Pipe) && conn.direct_recv_queue && @send_queue.empty?
|
|
73
79
|
conn.send_message(parts)
|
|
74
80
|
else
|
|
75
81
|
@send_queue.enqueue(parts)
|
|
@@ -92,15 +98,18 @@ module OMQ
|
|
|
92
98
|
batch = []
|
|
93
99
|
|
|
94
100
|
loop do
|
|
95
|
-
Routing.dequeue_batch(@send_queue, batch)
|
|
101
|
+
Routing.dequeue_batch(@send_queue, batch) or break
|
|
102
|
+
|
|
103
|
+
@engine.sending_batch do
|
|
104
|
+
if batch.size == 1
|
|
105
|
+
conn.write_message(batch.first)
|
|
106
|
+
else
|
|
107
|
+
conn.write_messages(batch)
|
|
108
|
+
end
|
|
96
109
|
|
|
97
|
-
|
|
98
|
-
conn.write_message(batch.first)
|
|
99
|
-
else
|
|
100
|
-
conn.write_messages(batch)
|
|
110
|
+
conn.flush
|
|
101
111
|
end
|
|
102
112
|
|
|
103
|
-
conn.flush
|
|
104
113
|
batch.each do |parts|
|
|
105
114
|
@engine.emit_verbose_msg_sent(conn, parts)
|
|
106
115
|
end
|
data/lib/omq/routing/peer.rb
CHANGED
|
@@ -8,27 +8,24 @@ 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 Peer
|
|
15
|
+
include ClosableQueues
|
|
16
|
+
include IdentityRouting
|
|
17
|
+
|
|
14
18
|
# @return [Async::LimitedQueue]
|
|
15
19
|
#
|
|
16
20
|
attr_reader :recv_queue
|
|
17
21
|
|
|
18
22
|
|
|
19
|
-
# @return [Hash{String => Connection}] routing_id → connection
|
|
20
|
-
#
|
|
21
|
-
attr_reader :connections_by_routing_id
|
|
22
|
-
|
|
23
|
-
|
|
24
23
|
# @param engine [Engine]
|
|
25
24
|
#
|
|
26
25
|
def initialize(engine)
|
|
27
|
-
@engine
|
|
28
|
-
@recv_queue
|
|
29
|
-
|
|
30
|
-
@routing_id_by_connection = {}
|
|
31
|
-
@conn_queues = {}
|
|
26
|
+
@engine = engine
|
|
27
|
+
@recv_queue = Routing.build_queue(engine.options.recv_hwm, :block)
|
|
28
|
+
init_identity_routing(engine)
|
|
32
29
|
end
|
|
33
30
|
|
|
34
31
|
|
|
@@ -53,41 +50,44 @@ module OMQ
|
|
|
53
50
|
# @param connection [Protocol::ZMTP::Connection]
|
|
54
51
|
#
|
|
55
52
|
def connection_added(connection)
|
|
56
|
-
routing_id =
|
|
57
|
-
@connections_by_routing_id[routing_id] = connection
|
|
58
|
-
@routing_id_by_connection[connection] = routing_id
|
|
53
|
+
routing_id = attach_route(connection).id
|
|
59
54
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
q = Routing.build_queue(@engine.options.send_hwm, :block)
|
|
63
|
-
@conn_queues[connection] = q
|
|
64
|
-
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 }
|
|
65
57
|
end
|
|
66
58
|
|
|
67
59
|
|
|
68
60
|
# @param connection [Protocol::ZMTP::Connection]
|
|
69
61
|
#
|
|
70
62
|
def connection_removed(connection)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
63
|
+
detach_route(connection)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# @return [Hash{String => Connection}] routing_id → connected peer
|
|
68
|
+
#
|
|
69
|
+
def connections_by_routing_id
|
|
70
|
+
@routes.each_with_object({}) do |(id, route), map|
|
|
71
|
+
map[id] = route.conn if route.conn
|
|
72
|
+
end
|
|
74
73
|
end
|
|
75
74
|
|
|
76
75
|
|
|
77
76
|
# @param parts [Array<String>]
|
|
78
77
|
#
|
|
79
78
|
def enqueue(parts)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
79
|
+
route = route_to(parts.first)
|
|
80
|
+
return unless route
|
|
81
|
+
|
|
82
|
+
Routing.enqueue_to_peer(route.queue, parts[1..])
|
|
84
83
|
end
|
|
85
84
|
|
|
86
85
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def generate_routing_id
|
|
90
|
+
SecureRandom.bytes(4)
|
|
91
91
|
end
|
|
92
92
|
|
|
93
93
|
end
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OMQ
|
|
4
|
+
module Routing
|
|
5
|
+
# The subscription prefixes one peer holds, as a radix trie.
|
|
6
|
+
#
|
|
7
|
+
# {#match?} walks the topic's bytes once, so its cost depends on the
|
|
8
|
+
# topic, not on how many prefixes the peer holds (libzmq and omq.rs
|
|
9
|
+
# use tries too). Bytes are compared without regard to encoding, so a
|
|
10
|
+
# UTF-8 topic matches a binary prefix.
|
|
11
|
+
#
|
|
12
|
+
# Like libzmq's PUB side, subscribing a prefix twice stores it once.
|
|
13
|
+
#
|
|
14
|
+
class PrefixSet
|
|
15
|
+
include Enumerable
|
|
16
|
+
|
|
17
|
+
# A trie node. +label+ is the edge from the parent, +children+ maps
|
|
18
|
+
# the first byte of each child's label to the child (nil for a leaf).
|
|
19
|
+
Node = Struct.new(:label, :children, :terminal)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# @return [Integer] number of prefixes
|
|
23
|
+
#
|
|
24
|
+
attr_reader :size
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def initialize
|
|
28
|
+
@root = Node.new(::Protocol::ZMTP::Codec::EMPTY_BINARY, nil, false)
|
|
29
|
+
@size = 0
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# @param prefix [String]
|
|
34
|
+
# @return [Boolean] true if the prefix was new
|
|
35
|
+
#
|
|
36
|
+
def add(prefix)
|
|
37
|
+
node = @root
|
|
38
|
+
i = 0
|
|
39
|
+
n = prefix.bytesize
|
|
40
|
+
|
|
41
|
+
while i < n
|
|
42
|
+
children = (node.children ||= {})
|
|
43
|
+
byte = prefix.getbyte(i)
|
|
44
|
+
child = children[byte]
|
|
45
|
+
|
|
46
|
+
unless child
|
|
47
|
+
children[byte] = Node.new(prefix.byteslice(i, n - i).b.freeze, nil, true)
|
|
48
|
+
@size += 1
|
|
49
|
+
return true
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
shared = common_length(child.label, prefix, i)
|
|
53
|
+
split(child, shared) if shared < child.label.bytesize
|
|
54
|
+
|
|
55
|
+
node = child
|
|
56
|
+
i += shared
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
return false if node.terminal
|
|
60
|
+
|
|
61
|
+
node.terminal = true
|
|
62
|
+
@size += 1
|
|
63
|
+
true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
alias << add
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# @param prefix [String]
|
|
71
|
+
# @return [Boolean] true if the prefix was present
|
|
72
|
+
#
|
|
73
|
+
def delete(prefix)
|
|
74
|
+
parent = nil
|
|
75
|
+
node = find(prefix) { |p| parent = p }
|
|
76
|
+
return false unless node&.terminal
|
|
77
|
+
|
|
78
|
+
node.terminal = false
|
|
79
|
+
@size -= 1
|
|
80
|
+
prune(parent, node)
|
|
81
|
+
true
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# @param prefix [String]
|
|
86
|
+
# @return [Boolean]
|
|
87
|
+
#
|
|
88
|
+
def include?(prefix)
|
|
89
|
+
node = find(prefix)
|
|
90
|
+
node ? node.terminal : false
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# @return [Boolean]
|
|
95
|
+
#
|
|
96
|
+
def empty?
|
|
97
|
+
@size.zero?
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# @param topic [String]
|
|
102
|
+
# @return [Boolean] true if some prefix is a prefix of +topic+
|
|
103
|
+
#
|
|
104
|
+
def match?(topic)
|
|
105
|
+
node = @root
|
|
106
|
+
i = 0
|
|
107
|
+
n = topic.bytesize
|
|
108
|
+
|
|
109
|
+
while true
|
|
110
|
+
return true if node.terminal
|
|
111
|
+
return false if i == n
|
|
112
|
+
|
|
113
|
+
children = node.children
|
|
114
|
+
return false unless children
|
|
115
|
+
|
|
116
|
+
child = children[topic.getbyte(i)]
|
|
117
|
+
return false unless child
|
|
118
|
+
|
|
119
|
+
label = child.label
|
|
120
|
+
m = label.bytesize
|
|
121
|
+
return false if i + m > n
|
|
122
|
+
|
|
123
|
+
j = 1 # the first byte matched through the Hash
|
|
124
|
+
|
|
125
|
+
while j < m
|
|
126
|
+
return false unless label.getbyte(j) == topic.getbyte(i + j)
|
|
127
|
+
j += 1
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
node = child
|
|
131
|
+
i += m
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# Yields every prefix (new frozen binary strings).
|
|
137
|
+
#
|
|
138
|
+
# @yieldparam prefix [String]
|
|
139
|
+
#
|
|
140
|
+
def each(&block)
|
|
141
|
+
return enum_for(:each) { @size } unless block
|
|
142
|
+
|
|
143
|
+
walk(@root, ::Protocol::ZMTP::Codec::EMPTY_BINARY, &block)
|
|
144
|
+
self
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
private
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# Number of bytes +label+ shares with +prefix+ from +offset+ on.
|
|
152
|
+
#
|
|
153
|
+
def common_length(label, prefix, offset)
|
|
154
|
+
j = 0
|
|
155
|
+
m = label.bytesize
|
|
156
|
+
n = prefix.bytesize - offset
|
|
157
|
+
|
|
158
|
+
j += 1 while j < m && j < n && label.getbyte(j) == prefix.getbyte(offset + j)
|
|
159
|
+
j
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
# Cuts +node+'s label after +at+ bytes; the rest moves to a new child.
|
|
164
|
+
#
|
|
165
|
+
def split(node, at)
|
|
166
|
+
label = node.label
|
|
167
|
+
lower = Node.new(label.byteslice(at, label.bytesize - at).freeze, node.children, node.terminal)
|
|
168
|
+
node.label = label.byteslice(0, at).freeze
|
|
169
|
+
node.children = { lower.label.getbyte(0) => lower }
|
|
170
|
+
node.terminal = false
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
# Exact lookup. Yields each node passed on the way (the last one
|
|
175
|
+
# yielded is the parent of the returned node).
|
|
176
|
+
#
|
|
177
|
+
def find(prefix)
|
|
178
|
+
node = @root
|
|
179
|
+
i = 0
|
|
180
|
+
n = prefix.bytesize
|
|
181
|
+
|
|
182
|
+
while i < n
|
|
183
|
+
children = node.children
|
|
184
|
+
return unless children
|
|
185
|
+
|
|
186
|
+
child = children[prefix.getbyte(i)]
|
|
187
|
+
return unless child
|
|
188
|
+
|
|
189
|
+
m = child.label.bytesize
|
|
190
|
+
return if i + m > n || common_length(child.label, prefix, i) < m
|
|
191
|
+
|
|
192
|
+
yield node if block_given?
|
|
193
|
+
node = child
|
|
194
|
+
i += m
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
node
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
# Removes +node+ if it became useless, then merges a parent left
|
|
202
|
+
# with a single child, so the trie stays compact.
|
|
203
|
+
#
|
|
204
|
+
def prune(parent, node)
|
|
205
|
+
return if node.equal?(@root)
|
|
206
|
+
|
|
207
|
+
if node.children.nil?
|
|
208
|
+
parent.children.delete(node.label.getbyte(0))
|
|
209
|
+
parent.children = nil if parent.children.empty?
|
|
210
|
+
merge(parent) unless parent.equal?(@root) || parent.terminal
|
|
211
|
+
else
|
|
212
|
+
merge(node)
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def merge(node)
|
|
218
|
+
children = node.children
|
|
219
|
+
return unless children&.size == 1
|
|
220
|
+
|
|
221
|
+
child = children.each_value.first
|
|
222
|
+
node.label = (node.label + child.label).freeze
|
|
223
|
+
node.children = child.children
|
|
224
|
+
node.terminal = child.terminal
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def walk(node, path, &block)
|
|
229
|
+
path = (path + node.label).freeze unless node.equal?(@root)
|
|
230
|
+
yield path if node.terminal
|
|
231
|
+
|
|
232
|
+
node.children&.each_value do |child|
|
|
233
|
+
walk(child, path, &block)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
end
|
data/lib/omq/routing/pub.rb
CHANGED
|
@@ -10,6 +10,7 @@ module OMQ
|
|
|
10
10
|
#
|
|
11
11
|
class Pub
|
|
12
12
|
include FanOut
|
|
13
|
+
include ClosableQueues
|
|
13
14
|
|
|
14
15
|
# @param engine [Engine]
|
|
15
16
|
#
|
|
@@ -40,7 +41,7 @@ module OMQ
|
|
|
40
41
|
#
|
|
41
42
|
def connection_added(connection)
|
|
42
43
|
@connections << connection
|
|
43
|
-
@subscriptions[connection] =
|
|
44
|
+
@subscriptions[connection] = PrefixSet.new
|
|
44
45
|
start_subscription_listener(connection)
|
|
45
46
|
add_fan_out_send_connection(connection)
|
|
46
47
|
end
|
data/lib/omq/routing/pull.rb
CHANGED
data/lib/omq/routing/push.rb
CHANGED
data/lib/omq/routing/radio.rb
CHANGED
|
@@ -11,6 +11,8 @@ module OMQ
|
|
|
11
11
|
# group (MORE=1) + body (MORE=0)
|
|
12
12
|
#
|
|
13
13
|
class Radio
|
|
14
|
+
include ClosableQueues
|
|
15
|
+
|
|
14
16
|
# Sentinel used for UDP connections that have no group filter:
|
|
15
17
|
# any group is considered a match.
|
|
16
18
|
#
|
|
@@ -27,7 +29,7 @@ module OMQ
|
|
|
27
29
|
#
|
|
28
30
|
def initialize(engine)
|
|
29
31
|
@engine = engine
|
|
30
|
-
@connections = []
|
|
32
|
+
@connections = [].freeze # copy-on-write: the send pump iterates it while writes yield
|
|
31
33
|
@groups = {} # connection => Set of joined groups (or ANY_GROUPS for UDP)
|
|
32
34
|
@send_queue = Routing.build_queue(engine.options.send_hwm, :block)
|
|
33
35
|
@subscriber_joined = Async::Promise.new
|
|
@@ -55,7 +57,7 @@ module OMQ
|
|
|
55
57
|
# @param connection [Protocol::ZMTP::Connection]
|
|
56
58
|
#
|
|
57
59
|
def connection_added(connection)
|
|
58
|
-
@connections
|
|
60
|
+
@connections = [*@connections, connection].freeze
|
|
59
61
|
if connection.respond_to?(:read_frame)
|
|
60
62
|
@groups[connection] = Set.new
|
|
61
63
|
start_group_listener(connection)
|
|
@@ -69,7 +71,7 @@ module OMQ
|
|
|
69
71
|
# @param connection [Protocol::ZMTP::Connection]
|
|
70
72
|
#
|
|
71
73
|
def connection_removed(connection)
|
|
72
|
-
@connections
|
|
74
|
+
@connections = (@connections - [connection]).freeze
|
|
73
75
|
@groups.delete(connection)
|
|
74
76
|
end
|
|
75
77
|
|
|
@@ -107,64 +109,71 @@ module OMQ
|
|
|
107
109
|
|
|
108
110
|
loop do
|
|
109
111
|
@send_pump_idle = true
|
|
110
|
-
Routing.dequeue_batch(@send_queue, batch)
|
|
112
|
+
Routing.dequeue_batch(@send_queue, batch) or break
|
|
111
113
|
@send_pump_idle = false
|
|
112
114
|
|
|
113
|
-
@
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
115
|
+
@engine.sending_batch { write_batch(batch) }
|
|
116
|
+
batch.clear
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
# Writes +batch+ to every connection that joined a message's group,
|
|
123
|
+
# then flushes each connection written to.
|
|
124
|
+
#
|
|
125
|
+
def write_batch(batch)
|
|
126
|
+
@written.clear
|
|
127
|
+
|
|
128
|
+
if @conflate
|
|
129
|
+
# Keep only the last matching message per connection.
|
|
130
|
+
@latest.clear
|
|
131
|
+
batch.each do |parts|
|
|
132
|
+
group = parts[0]
|
|
133
|
+
body = parts[1] || EMPTY_BINARY
|
|
134
|
+
@connections.each do |conn|
|
|
135
|
+
next unless @groups[conn]&.include?(group)
|
|
136
|
+
@latest[conn] = [group, body]
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
@latest.each do |conn, msg|
|
|
140
|
+
next if muted?(conn)
|
|
141
|
+
begin
|
|
142
|
+
conn.write_message(msg)
|
|
143
|
+
@written << conn
|
|
144
|
+
rescue *CONNECTION_LOST
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
else
|
|
148
|
+
batch.each do |parts|
|
|
149
|
+
group = parts[0]
|
|
150
|
+
body = parts[1] || EMPTY_BINARY
|
|
151
|
+
msg = [group, body]
|
|
152
|
+
wire_bytes = nil
|
|
153
|
+
|
|
154
|
+
@connections.each do |conn|
|
|
155
|
+
next unless @groups[conn]&.include?(group)
|
|
156
|
+
next if muted?(conn)
|
|
157
|
+
begin
|
|
158
|
+
if conn.respond_to?(:curve?) && conn.curve?
|
|
159
|
+
conn.write_message(msg)
|
|
160
|
+
elsif conn.respond_to?(:write_wire)
|
|
161
|
+
wire_bytes ||= Protocol::ZMTP::Codec::Frame.encode_message(msg)
|
|
162
|
+
conn.write_wire(wire_bytes)
|
|
163
|
+
else
|
|
129
164
|
conn.write_message(msg)
|
|
130
|
-
@written << conn
|
|
131
|
-
rescue *CONNECTION_LOST
|
|
132
|
-
end
|
|
133
|
-
end
|
|
134
|
-
else
|
|
135
|
-
batch.each do |parts|
|
|
136
|
-
group = parts[0]
|
|
137
|
-
body = parts[1] || EMPTY_BINARY
|
|
138
|
-
msg = [group, body]
|
|
139
|
-
wire_bytes = nil
|
|
140
|
-
|
|
141
|
-
@connections.each do |conn|
|
|
142
|
-
next unless @groups[conn]&.include?(group)
|
|
143
|
-
next if muted?(conn)
|
|
144
|
-
begin
|
|
145
|
-
if conn.respond_to?(:curve?) && conn.curve?
|
|
146
|
-
conn.write_message(msg)
|
|
147
|
-
elsif conn.respond_to?(:write_wire)
|
|
148
|
-
wire_bytes ||= Protocol::ZMTP::Codec::Frame.encode_message(msg)
|
|
149
|
-
conn.write_wire(wire_bytes)
|
|
150
|
-
else
|
|
151
|
-
conn.write_message(msg)
|
|
152
|
-
end
|
|
153
|
-
@written << conn
|
|
154
|
-
rescue *CONNECTION_LOST
|
|
155
|
-
end
|
|
156
165
|
end
|
|
166
|
+
@written << conn
|
|
167
|
+
rescue *CONNECTION_LOST
|
|
157
168
|
end
|
|
158
169
|
end
|
|
159
|
-
|
|
160
|
-
@written.each do |conn|
|
|
161
|
-
conn.flush
|
|
162
|
-
rescue *CONNECTION_LOST
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
batch.clear
|
|
166
170
|
end
|
|
167
171
|
end
|
|
172
|
+
|
|
173
|
+
@written.each do |conn|
|
|
174
|
+
conn.flush
|
|
175
|
+
rescue *CONNECTION_LOST
|
|
176
|
+
end
|
|
168
177
|
end
|
|
169
178
|
|
|
170
179
|
|
|
@@ -176,7 +185,9 @@ module OMQ
|
|
|
176
185
|
cmd = Protocol::ZMTP::Codec::Command.from_body(frame.body)
|
|
177
186
|
case cmd.name
|
|
178
187
|
when "JOIN"
|
|
179
|
-
@groups[conn]
|
|
188
|
+
groups = @groups[conn] or next
|
|
189
|
+
Routing.check_peer_subscriptions!(groups, cmd.data, @engine.options.max_peer_subscriptions)
|
|
190
|
+
groups.add(cmd.data)
|
|
180
191
|
@subscriber_joined.resolve(conn) unless @subscriber_joined.resolved?
|
|
181
192
|
when "LEAVE"
|
|
182
193
|
@groups[conn]&.delete(cmd.data)
|
data/lib/omq/routing/rep.rb
CHANGED
|
@@ -9,6 +9,8 @@ module OMQ
|
|
|
9
9
|
# on send.
|
|
10
10
|
#
|
|
11
11
|
class Rep
|
|
12
|
+
include ClosableQueues
|
|
13
|
+
|
|
12
14
|
EMPTY_FRAME = "".b.freeze
|
|
13
15
|
|
|
14
16
|
|
|
@@ -85,7 +87,7 @@ module OMQ
|
|
|
85
87
|
# @param connection [Protocol::ZMTP::Connection]
|
|
86
88
|
#
|
|
87
89
|
def connection_removed(connection)
|
|
88
|
-
@conn_queues.delete(connection)
|
|
90
|
+
@conn_queues.delete(connection)&.close
|
|
89
91
|
end
|
|
90
92
|
|
|
91
93
|
|
|
@@ -99,7 +101,7 @@ module OMQ
|
|
|
99
101
|
raise SocketError, "REP socket expects recv/send/recv/send order" unless @current_reply
|
|
100
102
|
|
|
101
103
|
conn, envelope = @current_reply
|
|
102
|
-
@conn_queues[conn]
|
|
104
|
+
Routing.enqueue_to_peer(@conn_queues[conn], [*envelope, EMPTY_FRAME, *parts].freeze)
|
|
103
105
|
@current_reply = nil
|
|
104
106
|
end
|
|
105
107
|
|
data/lib/omq/routing/req.rb
CHANGED
|
@@ -8,6 +8,7 @@ module OMQ
|
|
|
8
8
|
#
|
|
9
9
|
class Req
|
|
10
10
|
include RoundRobin
|
|
11
|
+
include ClosableQueues
|
|
11
12
|
|
|
12
13
|
# Shared frozen empty binary string to avoid repeated allocations.
|
|
13
14
|
EMPTY_BINARY = ::Protocol::ZMTP::Codec::EMPTY_BINARY
|
|
@@ -80,6 +81,13 @@ module OMQ
|
|
|
80
81
|
def connection_removed(connection)
|
|
81
82
|
@connections.delete(connection)
|
|
82
83
|
remove_round_robin_send_connection(connection)
|
|
84
|
+
|
|
85
|
+
# The peer holding our request is gone and will never reply.
|
|
86
|
+
# Allow the next send (lazy pirate retry) instead of wedging.
|
|
87
|
+
if @state == :waiting_reply && connection.equal?(@reply_connection)
|
|
88
|
+
@state = :ready
|
|
89
|
+
@reply_connection = nil
|
|
90
|
+
end
|
|
83
91
|
end
|
|
84
92
|
|
|
85
93
|
|