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
data/lib/omq/socket.rb
CHANGED
|
@@ -61,6 +61,8 @@ module OMQ
|
|
|
61
61
|
:heartbeat_ttl, :heartbeat_ttl=,
|
|
62
62
|
:heartbeat_timeout, :heartbeat_timeout=,
|
|
63
63
|
:max_message_size, :max_message_size=,
|
|
64
|
+
:max_message_parts, :max_message_parts=,
|
|
65
|
+
:max_peer_subscriptions, :max_peer_subscriptions=,
|
|
64
66
|
:sndbuf, :sndbuf=,
|
|
65
67
|
:rcvbuf, :rcvbuf=,
|
|
66
68
|
:on_mute, :on_mute=,
|
|
@@ -208,7 +210,7 @@ module OMQ
|
|
|
208
210
|
|
|
209
211
|
ensure_parent_task
|
|
210
212
|
|
|
211
|
-
queue =
|
|
213
|
+
queue = MonitorQueue.new
|
|
212
214
|
@engine.monitor_queue = queue
|
|
213
215
|
@engine.verbose_monitor = verbose
|
|
214
216
|
|
|
@@ -220,6 +222,7 @@ module OMQ
|
|
|
220
222
|
rescue Async::Stop
|
|
221
223
|
ensure
|
|
222
224
|
@engine.monitor_queue = nil
|
|
225
|
+
queue.close
|
|
223
226
|
block.call(MonitorEvent.new(type: :monitor_stopped))
|
|
224
227
|
end
|
|
225
228
|
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OMQ
|
|
4
|
+
module Transport
|
|
5
|
+
# Error handling shared by stream listener accept loops (TCP, IPC).
|
|
6
|
+
#
|
|
7
|
+
# A listener must survive errors that a later accept(2) recovers
|
|
8
|
+
# from. Without this, one EMFILE ended the accept task for good and
|
|
9
|
+
# left the endpoint bound but deaf.
|
|
10
|
+
#
|
|
11
|
+
module AcceptLoop
|
|
12
|
+
# accept(2) ran out of a resource. Retry after a pause so a full fd
|
|
13
|
+
# table doesn't spin the loop.
|
|
14
|
+
BACKOFF_ERRORS = [
|
|
15
|
+
Errno::EMFILE,
|
|
16
|
+
Errno::ENFILE,
|
|
17
|
+
Errno::ENOBUFS,
|
|
18
|
+
Errno::ENOMEM,
|
|
19
|
+
].freeze
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# One client failed during accept(2) or socket setup. Drop it and
|
|
23
|
+
# accept the next one right away.
|
|
24
|
+
CLIENT_ERRORS = %i[ECONNABORTED ECONNRESET EPROTO EPERM EINVAL ENOTCONN].filter_map do |name|
|
|
25
|
+
Errno.const_get(name) if Errno.const_defined?(name)
|
|
26
|
+
end.freeze
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# Every error an accept loop recovers from.
|
|
30
|
+
RECOVERABLE_ERRORS = (BACKOFF_ERRORS + CLIENT_ERRORS).freeze
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# Reports a recoverable accept error and backs off if the process
|
|
34
|
+
# is out of a resource.
|
|
35
|
+
#
|
|
36
|
+
# @param error [SystemCallError]
|
|
37
|
+
# @param engine [Engine]
|
|
38
|
+
# @param endpoint [String]
|
|
39
|
+
# @return [void]
|
|
40
|
+
#
|
|
41
|
+
def self.recover(error, engine, endpoint)
|
|
42
|
+
engine.emit_monitor_event(:accept_failed, endpoint: endpoint, detail: { error: error })
|
|
43
|
+
|
|
44
|
+
if BACKOFF_ERRORS.include?(error.class)
|
|
45
|
+
sleep backoff(engine.options)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# @param options [Options]
|
|
51
|
+
# @return [Numeric] the shortest reconnect interval
|
|
52
|
+
#
|
|
53
|
+
def self.backoff(options)
|
|
54
|
+
ri = options.reconnect_interval
|
|
55
|
+
ri.is_a?(Range) ? ri.begin : ri
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -6,11 +6,17 @@ require_relative "compression_arena"
|
|
|
6
6
|
module OMQ
|
|
7
7
|
module Transport
|
|
8
8
|
class CompressedConnection < SimpleDelegator
|
|
9
|
+
# The most a message may decode to when `max_message_size` is unset:
|
|
10
|
+
# just under 1 GiB, as in omq.rs. Sizes a peer declares are checked
|
|
11
|
+
# against it before anything is allocated for them.
|
|
12
|
+
DEFAULT_DECODE_BUDGET = (1 << 30) - 1
|
|
13
|
+
|
|
9
14
|
attr_reader :last_wire_size_in
|
|
10
15
|
|
|
11
16
|
def initialize(conn, max_message_size:, arena_sentinel:, protocol_error:)
|
|
12
17
|
super(conn)
|
|
13
18
|
@max_message_size = max_message_size
|
|
19
|
+
@decode_budget = max_message_size || DEFAULT_DECODE_BUDGET
|
|
14
20
|
@arena_sentinel = arena_sentinel
|
|
15
21
|
@protocol_error = protocol_error
|
|
16
22
|
@pending_messages = []
|
|
@@ -122,6 +122,9 @@ module OMQ
|
|
|
122
122
|
else
|
|
123
123
|
(@pending_direct ||= []) << parts
|
|
124
124
|
end
|
|
125
|
+
rescue Async::Queue::ClosedError, ClosedQueueError
|
|
126
|
+
# The peer socket closed and with it the queue we write into.
|
|
127
|
+
raise IOError, "closed"
|
|
125
128
|
end
|
|
126
129
|
|
|
127
130
|
|
|
@@ -186,6 +189,8 @@ module OMQ
|
|
|
186
189
|
def send_command(command)
|
|
187
190
|
raise IOError, "closed" if @closed
|
|
188
191
|
@send_queue.enqueue([:command, command])
|
|
192
|
+
rescue Async::Queue::ClosedError, ClosedQueueError
|
|
193
|
+
raise IOError, "closed"
|
|
189
194
|
end
|
|
190
195
|
|
|
191
196
|
|
|
@@ -215,6 +220,8 @@ module OMQ
|
|
|
215
220
|
@closed = true
|
|
216
221
|
@closed_signal.resolve(true)
|
|
217
222
|
@send_queue&.enqueue(nil) # close sentinel
|
|
223
|
+
rescue Async::Queue::ClosedError, ClosedQueueError
|
|
224
|
+
# peer already closed its queues
|
|
218
225
|
end
|
|
219
226
|
|
|
220
227
|
|
data/lib/omq/transport/inproc.rb
CHANGED
|
@@ -89,12 +89,21 @@ module OMQ
|
|
|
89
89
|
#
|
|
90
90
|
# @param endpoint [String] e.g. "ruby://my-endpoint"
|
|
91
91
|
# @param engine [Engine] the connecting engine
|
|
92
|
+
# @param wait [Boolean] wait up to one reconnect interval for a bind
|
|
92
93
|
# @return [void]
|
|
94
|
+
# @raise [Errno::ECONNREFUSED] if nothing is bound. It is in
|
|
95
|
+
# {CONNECTION_FAILED}, so {Engine::Reconnect} retries.
|
|
93
96
|
#
|
|
94
|
-
def connect(endpoint, engine, **)
|
|
97
|
+
def connect(endpoint, engine, wait: false, **)
|
|
95
98
|
endpoint = canonical_endpoint(endpoint)
|
|
96
99
|
bound_engine = @mutex.synchronize { @registry[endpoint] }
|
|
97
|
-
|
|
100
|
+
|
|
101
|
+
if bound_engine.nil? && wait
|
|
102
|
+
bound_engine = await_bind(endpoint, engine)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
raise Errno::ECONNREFUSED, endpoint unless bound_engine
|
|
106
|
+
|
|
98
107
|
establish_link(engine, bound_engine, endpoint)
|
|
99
108
|
end
|
|
100
109
|
|
|
@@ -145,7 +154,8 @@ module OMQ
|
|
|
145
154
|
client_pipe, server_pipe = make_pipe_pair client_engine, server_engine,
|
|
146
155
|
client_type, server_type, needs_cmds
|
|
147
156
|
|
|
148
|
-
|
|
157
|
+
# A refused client closes its pipe; don't hand the server a dead peer.
|
|
158
|
+
client_engine.connection_ready(client_pipe, endpoint: endpoint, dialed: true) or return
|
|
149
159
|
server_engine.connection_ready(server_pipe, endpoint: endpoint)
|
|
150
160
|
end
|
|
151
161
|
|
|
@@ -187,9 +197,13 @@ module OMQ
|
|
|
187
197
|
end
|
|
188
198
|
|
|
189
199
|
|
|
200
|
+
# Waits up to one reconnect interval for +endpoint+ to be bound.
|
|
201
|
+
# Matches ZMQ 4.x: connect to unbound inproc succeeds silently,
|
|
202
|
+
# and the caller retries in the background.
|
|
203
|
+
#
|
|
204
|
+
# @return [Engine, nil] the bound engine, or nil on timeout
|
|
205
|
+
#
|
|
190
206
|
def await_bind(endpoint, engine)
|
|
191
|
-
# Endpoint not bound yet — wait briefly then start background retry.
|
|
192
|
-
# Matches ZMQ 4.x: connect to unbound inproc succeeds silently.
|
|
193
207
|
ri = engine.options.reconnect_interval
|
|
194
208
|
timeout = ri.is_a?(Range) ? ri.begin : ri
|
|
195
209
|
promise = Async::Promise.new
|
|
@@ -207,39 +221,18 @@ module OMQ
|
|
|
207
221
|
@waiters[endpoint].delete(promise)
|
|
208
222
|
end
|
|
209
223
|
|
|
210
|
-
start_connect_retry(endpoint, engine)
|
|
211
224
|
nil
|
|
212
225
|
end
|
|
213
226
|
end
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
# Spawns a background task that periodically retries
|
|
217
|
-
# #establish_link until the endpoint appears in the registry.
|
|
218
|
-
#
|
|
219
|
-
# @param endpoint [String] the inproc endpoint name
|
|
220
|
-
# @param engine [Engine] the connecting engine
|
|
221
|
-
#
|
|
222
|
-
def start_connect_retry(endpoint, engine)
|
|
223
|
-
engine.spawn_inproc_retry(endpoint) do |ivl|
|
|
224
|
-
loop do
|
|
225
|
-
sleep ivl
|
|
226
|
-
bound_engine = @mutex.synchronize { @registry[endpoint] }
|
|
227
|
-
|
|
228
|
-
if bound_engine
|
|
229
|
-
establish_link(engine, bound_engine, endpoint)
|
|
230
|
-
break
|
|
231
|
-
end
|
|
232
|
-
end
|
|
233
|
-
end
|
|
234
|
-
end
|
|
235
227
|
end
|
|
236
228
|
|
|
237
229
|
|
|
238
230
|
# An inproc dialer: stateful factory for outgoing connections.
|
|
239
231
|
#
|
|
240
232
|
# Created once per {Engine#connect}, stored in +@dialers[endpoint]+.
|
|
241
|
-
# {Engine#connect} calls {#connect} synchronously
|
|
242
|
-
# calls it
|
|
233
|
+
# {Engine#connect} calls {#connect} synchronously with +wait: true+;
|
|
234
|
+
# {Engine::Reconnect} calls it while nothing is bound, and again
|
|
235
|
+
# after the bound peer goes away.
|
|
243
236
|
#
|
|
244
237
|
class Dialer
|
|
245
238
|
# @return [String] the endpoint this dialer connects to
|
|
@@ -256,13 +249,14 @@ module OMQ
|
|
|
256
249
|
end
|
|
257
250
|
|
|
258
251
|
|
|
259
|
-
# Links to the bound endpoint
|
|
260
|
-
# if nothing is bound yet.
|
|
252
|
+
# Links to the bound endpoint.
|
|
261
253
|
#
|
|
254
|
+
# @param wait [Boolean] wait up to one reconnect interval for a bind
|
|
262
255
|
# @return [void]
|
|
256
|
+
# @raise [Errno::ECONNREFUSED] if nothing is bound
|
|
263
257
|
#
|
|
264
|
-
def connect
|
|
265
|
-
Inproc.connect(@endpoint, @engine)
|
|
258
|
+
def connect(wait: false)
|
|
259
|
+
Inproc.connect(@endpoint, @engine, wait: wait)
|
|
266
260
|
end
|
|
267
261
|
|
|
268
262
|
end
|
data/lib/omq/transport/ipc.rb
CHANGED
|
@@ -14,6 +14,21 @@ module OMQ
|
|
|
14
14
|
Engine.transports["ipc"] = self
|
|
15
15
|
|
|
16
16
|
|
|
17
|
+
# The URI of an endpoint +URI.parse+ can't keep, with the path as
|
|
18
|
+
# given. +URI.parse+ reads the @ of "ipc://@name" as the end of an
|
|
19
|
+
# empty userinfo, so its #to_s is "ipc://name", a relative file path,
|
|
20
|
+
# and it rejects paths with spaces and similar characters.
|
|
21
|
+
#
|
|
22
|
+
class PathURI < URI::Generic
|
|
23
|
+
# @return [String] e.g. "ipc://@name"
|
|
24
|
+
#
|
|
25
|
+
def to_s
|
|
26
|
+
"ipc://#{path}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
|
|
17
32
|
class << self
|
|
18
33
|
# ZMTP connection class used for IPC-accepted/dialed peers.
|
|
19
34
|
#
|
|
@@ -30,12 +45,17 @@ module OMQ
|
|
|
30
45
|
# @param engine [Engine]
|
|
31
46
|
# @return [Listener]
|
|
32
47
|
#
|
|
48
|
+
# @raise [Errno::EADDRINUSE] if a live socket or a non-socket file
|
|
49
|
+
# already occupies the path
|
|
50
|
+
#
|
|
33
51
|
def listener(endpoint, engine, **)
|
|
34
52
|
path = parse_path(endpoint)
|
|
35
53
|
sock_path = to_socket_path(path)
|
|
36
54
|
|
|
37
|
-
#
|
|
38
|
-
|
|
55
|
+
# Reject a too-long path before touching the filesystem.
|
|
56
|
+
::Socket.sockaddr_un(sock_path)
|
|
57
|
+
|
|
58
|
+
remove_stale_socket(path) unless abstract?(path)
|
|
39
59
|
|
|
40
60
|
server = UNIXServer.new(sock_path)
|
|
41
61
|
|
|
@@ -43,6 +63,29 @@ module OMQ
|
|
|
43
63
|
end
|
|
44
64
|
|
|
45
65
|
|
|
66
|
+
# Removes a socket file left behind by a process that is gone.
|
|
67
|
+
# A socket that still accepts connections, or a file that isn't
|
|
68
|
+
# a socket, is never replaced.
|
|
69
|
+
#
|
|
70
|
+
# @param path [String] filesystem path
|
|
71
|
+
# @raise [Errno::EADDRINUSE]
|
|
72
|
+
#
|
|
73
|
+
def remove_stale_socket(path)
|
|
74
|
+
return unless File.exist?(path) || File.symlink?(path)
|
|
75
|
+
|
|
76
|
+
unless File.socket?(path)
|
|
77
|
+
raise Errno::EADDRINUSE, "#{path} exists and is not a socket"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
probe_socket(path)
|
|
81
|
+
raise Errno::EADDRINUSE, "#{path} is in use"
|
|
82
|
+
rescue Errno::ECONNREFUSED
|
|
83
|
+
File.delete(path)
|
|
84
|
+
rescue Errno::ENOENT
|
|
85
|
+
# vanished meanwhile
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
|
|
46
89
|
# Creates an IPC dialer for an endpoint.
|
|
47
90
|
#
|
|
48
91
|
# @param endpoint [String]
|
|
@@ -62,11 +105,11 @@ module OMQ
|
|
|
62
105
|
#
|
|
63
106
|
def apply_buffer_sizes(sock, options)
|
|
64
107
|
if options.sndbuf
|
|
65
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, options.sndbuf)
|
|
108
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options.sndbuf)
|
|
66
109
|
end
|
|
67
110
|
|
|
68
111
|
if options.rcvbuf
|
|
69
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVBUF, options.rcvbuf)
|
|
112
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options.rcvbuf)
|
|
70
113
|
end
|
|
71
114
|
end
|
|
72
115
|
|
|
@@ -94,6 +137,40 @@ module OMQ
|
|
|
94
137
|
def abstract?(path)
|
|
95
138
|
path.start_with?("@")
|
|
96
139
|
end
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# @param endpoint [String] e.g. "ipc:///tmp/my.sock" or "ipc://@abstract"
|
|
143
|
+
# @return [URI::Generic] the endpoint's URI; a {PathURI} for an
|
|
144
|
+
# abstract namespace name or a path +URI.parse+ rejects
|
|
145
|
+
#
|
|
146
|
+
def endpoint_uri(endpoint)
|
|
147
|
+
path = parse_path(endpoint)
|
|
148
|
+
return path_uri(path) if abstract?(path)
|
|
149
|
+
|
|
150
|
+
begin
|
|
151
|
+
URI.parse(endpoint)
|
|
152
|
+
rescue URI::InvalidURIError
|
|
153
|
+
path_uri(path)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def path_uri(path)
|
|
162
|
+
PathURI.new("ipc", nil, nil, nil, nil, path, nil, nil, nil)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# Connects to +path+ and hangs up. Raises ECONNREFUSED if nothing
|
|
167
|
+
# listens there.
|
|
168
|
+
#
|
|
169
|
+
def probe_socket(path)
|
|
170
|
+
UNIXSocket.new(path).close
|
|
171
|
+
rescue Errno::EAGAIN
|
|
172
|
+
# Backlog full: someone is listening.
|
|
173
|
+
end
|
|
97
174
|
end
|
|
98
175
|
|
|
99
176
|
|
|
@@ -152,6 +229,14 @@ module OMQ
|
|
|
152
229
|
@path = path
|
|
153
230
|
@engine = engine
|
|
154
231
|
@task = nil
|
|
232
|
+
|
|
233
|
+
# Remember which file we created, by absolute path and inode,
|
|
234
|
+
# so #stop never unlinks a file that another socket bound
|
|
235
|
+
# since, even after a chdir.
|
|
236
|
+
unless IPC.abstract?(path)
|
|
237
|
+
@file_path = File.expand_path(path)
|
|
238
|
+
@file_id = file_id(@file_path)
|
|
239
|
+
end
|
|
155
240
|
end
|
|
156
241
|
|
|
157
242
|
|
|
@@ -170,6 +255,9 @@ module OMQ
|
|
|
170
255
|
Async::Task.current.defer_stop do
|
|
171
256
|
yield IO::Stream::Buffered.wrap(client)
|
|
172
257
|
end
|
|
258
|
+
rescue *AcceptLoop::RECOVERABLE_ERRORS => error
|
|
259
|
+
client&.close rescue nil
|
|
260
|
+
AcceptLoop.recover(error, @engine, @endpoint)
|
|
173
261
|
end
|
|
174
262
|
rescue Async::Stop
|
|
175
263
|
rescue IOError
|
|
@@ -190,12 +278,22 @@ module OMQ
|
|
|
190
278
|
@task&.stop
|
|
191
279
|
@server.close rescue nil
|
|
192
280
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
File.delete(@path) rescue nil
|
|
281
|
+
if @file_id && file_id(@file_path) == @file_id
|
|
282
|
+
File.delete(@file_path) rescue nil
|
|
196
283
|
end
|
|
197
284
|
end
|
|
198
285
|
|
|
286
|
+
|
|
287
|
+
private
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def file_id(path)
|
|
291
|
+
stat = File.lstat(path)
|
|
292
|
+
[stat.dev, stat.ino]
|
|
293
|
+
rescue SystemCallError
|
|
294
|
+
nil
|
|
295
|
+
end
|
|
296
|
+
|
|
199
297
|
end
|
|
200
298
|
end
|
|
201
299
|
end
|
data/lib/omq/transport/tcp.rb
CHANGED
|
@@ -29,6 +29,8 @@ module OMQ
|
|
|
29
29
|
# @return [Listener]
|
|
30
30
|
#
|
|
31
31
|
def listener(endpoint, engine, **)
|
|
32
|
+
# libzmq's wildcard port: any free port, like :0.
|
|
33
|
+
endpoint = endpoint.sub(/:\*\z/, ":0")
|
|
32
34
|
host, port = self.parse_endpoint(endpoint)
|
|
33
35
|
lookup_host = normalize_bind_host(host)
|
|
34
36
|
|
|
@@ -115,9 +117,7 @@ module OMQ
|
|
|
115
117
|
# doesn't block the retry loop. Floor at 0.5s for real-network latency.
|
|
116
118
|
#
|
|
117
119
|
def connect_timeout(options)
|
|
118
|
-
|
|
119
|
-
ri = ri.end if ri.is_a?(Range)
|
|
120
|
-
[ri, 0.5].max
|
|
120
|
+
options.attempt_timeout
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
|
|
@@ -140,11 +140,11 @@ module OMQ
|
|
|
140
140
|
#
|
|
141
141
|
def apply_buffer_sizes(sock, options)
|
|
142
142
|
if options.sndbuf
|
|
143
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, options.sndbuf)
|
|
143
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options.sndbuf)
|
|
144
144
|
end
|
|
145
145
|
|
|
146
146
|
if options.rcvbuf
|
|
147
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVBUF, options.rcvbuf)
|
|
147
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options.rcvbuf)
|
|
148
148
|
end
|
|
149
149
|
end
|
|
150
150
|
end
|
|
@@ -234,6 +234,9 @@ module OMQ
|
|
|
234
234
|
Async::Task.current.defer_stop do
|
|
235
235
|
yield IO::Stream::Buffered.wrap(client)
|
|
236
236
|
end
|
|
237
|
+
rescue *AcceptLoop::RECOVERABLE_ERRORS => error
|
|
238
|
+
client&.close rescue nil
|
|
239
|
+
AcceptLoop.recover(error, @engine, @endpoint)
|
|
237
240
|
end
|
|
238
241
|
rescue Async::Stop
|
|
239
242
|
rescue IOError
|
data/lib/omq/transport/udp.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "socket"
|
|
4
4
|
require "uri"
|
|
5
5
|
require "set"
|
|
6
|
+
require "ipaddr"
|
|
6
7
|
|
|
7
8
|
module OMQ
|
|
8
9
|
module Transport
|
|
@@ -27,15 +28,50 @@ module OMQ
|
|
|
27
28
|
# @param engine [Engine]
|
|
28
29
|
# @return [Listener]
|
|
29
30
|
#
|
|
31
|
+
# A multicast address joins its group, and several DISH sockets
|
|
32
|
+
# may bind the same group and port.
|
|
33
|
+
#
|
|
30
34
|
def listener(endpoint, engine, **)
|
|
31
35
|
host, port = parse_endpoint(endpoint)
|
|
32
|
-
host
|
|
33
|
-
socket
|
|
34
|
-
|
|
36
|
+
addr = Addrinfo.udp(host == "*" ? "0.0.0.0" : host, port)
|
|
37
|
+
socket = UDPSocket.new(addr.afamily)
|
|
38
|
+
|
|
39
|
+
if multicast?(addr)
|
|
40
|
+
socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true)
|
|
41
|
+
socket.bind(addr.ip_address, port)
|
|
42
|
+
join_group(socket, addr)
|
|
43
|
+
else
|
|
44
|
+
socket.bind(addr.ip_address, port)
|
|
45
|
+
end
|
|
46
|
+
|
|
35
47
|
Listener.new(endpoint, socket, engine)
|
|
36
48
|
end
|
|
37
49
|
|
|
38
50
|
|
|
51
|
+
# @param addr [Addrinfo]
|
|
52
|
+
# @return [Boolean]
|
|
53
|
+
#
|
|
54
|
+
def multicast?(addr)
|
|
55
|
+
addr.ipv4_multicast? || addr.ipv6_multicast?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# Joins the multicast group +addr+ on the default interface.
|
|
60
|
+
#
|
|
61
|
+
# @param socket [UDPSocket]
|
|
62
|
+
# @param addr [Addrinfo]
|
|
63
|
+
#
|
|
64
|
+
def join_group(socket, addr)
|
|
65
|
+
group = IPAddr.new(addr.ip_address)
|
|
66
|
+
|
|
67
|
+
if addr.ipv6?
|
|
68
|
+
socket.setsockopt(::Socket::IPPROTO_IPV6, ::Socket::IPV6_JOIN_GROUP, group.hton + [0].pack("I"))
|
|
69
|
+
else
|
|
70
|
+
socket.setsockopt(::Socket::IPPROTO_IP, ::Socket::IP_ADD_MEMBERSHIP, group.hton + IPAddr.new("0.0.0.0").hton)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
|
|
39
75
|
# Creates a UDP dialer for a RADIO socket.
|
|
40
76
|
#
|
|
41
77
|
# @param endpoint [String] e.g. "udp://127.0.0.1:5555"
|
|
@@ -69,6 +105,22 @@ module OMQ
|
|
|
69
105
|
end
|
|
70
106
|
|
|
71
107
|
|
|
108
|
+
# Sizes the send buffer of a RADIO socket. Uses +sndbuf+ if set;
|
|
109
|
+
# otherwise makes sure a maximum-size datagram fits (macOS
|
|
110
|
+
# defaults to 9216 bytes and refuses larger sends).
|
|
111
|
+
#
|
|
112
|
+
# @param socket [UDPSocket]
|
|
113
|
+
# @param options [Options]
|
|
114
|
+
#
|
|
115
|
+
def apply_send_buffer(socket, options)
|
|
116
|
+
size = options.sndbuf
|
|
117
|
+
size ||= MAX_DATAGRAM_SIZE unless socket.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF).int >= MAX_DATAGRAM_SIZE
|
|
118
|
+
socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, size) if size
|
|
119
|
+
rescue SystemCallError
|
|
120
|
+
# keep the OS default
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
|
|
72
124
|
# Decodes a UDP datagram into [group, body].
|
|
73
125
|
#
|
|
74
126
|
# @param data [String] raw datagram bytes
|
|
@@ -110,10 +162,15 @@ module OMQ
|
|
|
110
162
|
#
|
|
111
163
|
# @return [void]
|
|
112
164
|
#
|
|
165
|
+
# Resolves the host once, so sends don't hit DNS, and picks the
|
|
166
|
+
# socket family to match (IPv4 or IPv6).
|
|
167
|
+
#
|
|
113
168
|
def connect
|
|
114
169
|
host, port = UDP.parse_endpoint(@endpoint)
|
|
115
|
-
|
|
116
|
-
|
|
170
|
+
addr = Addrinfo.udp(host, port)
|
|
171
|
+
socket = UDPSocket.new(addr.afamily)
|
|
172
|
+
UDP.apply_send_buffer(socket, @engine.options)
|
|
173
|
+
conn = RadioConnection.new(socket, addr)
|
|
117
174
|
@engine.connection_ready(conn, endpoint: @endpoint)
|
|
118
175
|
end
|
|
119
176
|
|
|
@@ -127,13 +184,11 @@ module OMQ
|
|
|
127
184
|
#
|
|
128
185
|
class RadioConnection
|
|
129
186
|
# @param socket [UDPSocket]
|
|
130
|
-
# @param
|
|
131
|
-
# @param port [Integer]
|
|
187
|
+
# @param addr [Addrinfo] resolved destination
|
|
132
188
|
#
|
|
133
|
-
def initialize(socket,
|
|
134
|
-
@socket
|
|
135
|
-
@
|
|
136
|
-
@port = port
|
|
189
|
+
def initialize(socket, addr)
|
|
190
|
+
@socket = socket
|
|
191
|
+
@sockaddr = addr.to_sockaddr
|
|
137
192
|
end
|
|
138
193
|
|
|
139
194
|
|
|
@@ -141,11 +196,16 @@ module OMQ
|
|
|
141
196
|
#
|
|
142
197
|
# @param parts [Array<String>] [group, body]
|
|
143
198
|
#
|
|
199
|
+
# Drops what UDP can't carry: a datagram over
|
|
200
|
+
# {MAX_DATAGRAM_SIZE}, or one the OS refuses as too large.
|
|
201
|
+
#
|
|
144
202
|
def write_message(parts)
|
|
145
203
|
group, body = parts
|
|
146
204
|
datagram = UDP.encode_datagram(group.to_s, body.to_s)
|
|
147
|
-
|
|
148
|
-
|
|
205
|
+
return if datagram.bytesize > MAX_DATAGRAM_SIZE
|
|
206
|
+
|
|
207
|
+
@socket.send(datagram, 0, @sockaddr)
|
|
208
|
+
rescue Errno::ECONNREFUSED, Errno::ENETUNREACH, Errno::EMSGSIZE
|
|
149
209
|
# UDP fire-and-forget — drop silently
|
|
150
210
|
end
|
|
151
211
|
|
|
@@ -178,6 +238,10 @@ module OMQ
|
|
|
178
238
|
# never transmitted on the wire.
|
|
179
239
|
#
|
|
180
240
|
class DishConnection
|
|
241
|
+
# Datagrams skipped in a row before yielding to other fibers.
|
|
242
|
+
SKIP_BUDGET = 64
|
|
243
|
+
|
|
244
|
+
|
|
181
245
|
# @param socket [UDPSocket] bound socket
|
|
182
246
|
#
|
|
183
247
|
def initialize(socket)
|
|
@@ -194,13 +258,24 @@ module OMQ
|
|
|
194
258
|
# @return [Array<String>] [group, body], both binary-frozen
|
|
195
259
|
#
|
|
196
260
|
def receive_message
|
|
261
|
+
skipped = 0
|
|
262
|
+
|
|
197
263
|
loop do
|
|
198
264
|
data, = @socket.recvfrom_nonblock(MAX_DATAGRAM_SIZE)
|
|
199
265
|
parts = UDP.decode_datagram(data)
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
266
|
+
|
|
267
|
+
if parts && @groups.include?(parts[0].b)
|
|
268
|
+
return parts
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# Malformed or for a group we didn't join. A flood of these
|
|
272
|
+
# must not keep the reactor from running other fibers.
|
|
273
|
+
skipped += 1
|
|
274
|
+
|
|
275
|
+
if skipped >= SKIP_BUDGET
|
|
276
|
+
skipped = 0
|
|
277
|
+
Async::Task.current.yield
|
|
278
|
+
end
|
|
204
279
|
rescue IO::WaitReadable
|
|
205
280
|
@socket.wait_readable
|
|
206
281
|
retry
|
data/lib/omq/version.rb
CHANGED
data/lib/omq.rb
CHANGED
|
@@ -7,6 +7,7 @@ require "io/stream"
|
|
|
7
7
|
# Core
|
|
8
8
|
require_relative "omq/version"
|
|
9
9
|
require_relative "omq/constants"
|
|
10
|
+
require_relative "omq/monitor_queue"
|
|
10
11
|
require_relative "omq/backend"
|
|
11
12
|
require_relative "omq/reactor"
|
|
12
13
|
require_relative "omq/options"
|
|
@@ -28,6 +29,7 @@ require_relative "omq/engine"
|
|
|
28
29
|
OMQ::Backend.register(:ruby, OMQ::Engine)
|
|
29
30
|
|
|
30
31
|
# Transport
|
|
32
|
+
require_relative "omq/transport/accept_loop"
|
|
31
33
|
require_relative "omq/transport/inproc"
|
|
32
34
|
require_relative "omq/transport/tcp"
|
|
33
35
|
require_relative "omq/transport/ipc"
|