protocol-zmtp 0.3.0 → 0.4.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/lib/protocol/zmtp/connection.rb +47 -2
- data/lib/protocol/zmtp/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f6f3ddb84f7c69bf776e615da15797cb815eaf663f357cf16cff60c744153bc8
|
|
4
|
+
data.tar.gz: fd410e2dc417e623254be530230e88fc3e428f62413d6465ee88465c82444b10
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0c84020c35c81c9b9609541019cb7433956bfed7a2bf8fd4ea615ad7560b9aecec5086efbab733576309ff7cb2febb4f5c98bef2388f55a056bcf38c78a0633d
|
|
7
|
+
data.tar.gz: 7251f30d057e56f97e6ffa3eb95af5727d7763a04829b3c93580e36998b1aa2591cdc0152ff4d53e7764ea80f75113585513d537147c3de238b498d50f25d15c
|
|
@@ -52,6 +52,11 @@ module Protocol
|
|
|
52
52
|
@mutex = Mutex.new
|
|
53
53
|
@max_message_size = max_message_size
|
|
54
54
|
@last_received_at = nil
|
|
55
|
+
|
|
56
|
+
# Reusable scratch buffer for frame headers. Array#pack(buffer:)
|
|
57
|
+
# writes in place so the per-message 2-or-9 byte String allocation
|
|
58
|
+
# in write_frames disappears on the hot send path.
|
|
59
|
+
@header_buf = String.new(capacity: 9, encoding: Encoding::BINARY)
|
|
55
60
|
end
|
|
56
61
|
|
|
57
62
|
|
|
@@ -109,6 +114,27 @@ module Protocol
|
|
|
109
114
|
end
|
|
110
115
|
|
|
111
116
|
|
|
117
|
+
# Writes a batch of multi-frame messages to the buffer under a
|
|
118
|
+
# single mutex acquisition. Used by work-stealing send pumps that
|
|
119
|
+
# dequeue up to N messages at once — avoids the N lock/unlock
|
|
120
|
+
# pairs per batch that a plain `batch.each { write_message }`
|
|
121
|
+
# would incur.
|
|
122
|
+
#
|
|
123
|
+
# @param messages [Array<Array<String>>] each element is one
|
|
124
|
+
# multi-frame message
|
|
125
|
+
# @return [void]
|
|
126
|
+
def write_messages(messages)
|
|
127
|
+
@mutex.synchronize do
|
|
128
|
+
i = 0
|
|
129
|
+
n = messages.size
|
|
130
|
+
while i < n
|
|
131
|
+
write_frames(messages[i])
|
|
132
|
+
i += 1
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
|
|
112
138
|
# Writes pre-encoded wire bytes to the buffer without flushing.
|
|
113
139
|
# Used for fan-out: encode once, write to many connections.
|
|
114
140
|
#
|
|
@@ -241,13 +267,32 @@ module Protocol
|
|
|
241
267
|
private
|
|
242
268
|
|
|
243
269
|
# Writes message parts as ZMTP frames, encrypting if needed.
|
|
270
|
+
#
|
|
271
|
+
# For the unencrypted path, writes the frame header and body
|
|
272
|
+
# separately to the IO instead of allocating a wire String. This
|
|
273
|
+
# avoids copying the body just to glue a 1- or 9-byte header onto
|
|
274
|
+
# it -- significant for large messages where the body copy was
|
|
275
|
+
# the dominant allocation per send.
|
|
244
276
|
def write_frames(parts)
|
|
277
|
+
encrypted = @mechanism.encrypted?
|
|
278
|
+
buf = @header_buf
|
|
279
|
+
|
|
245
280
|
parts.each_with_index do |part, i|
|
|
246
281
|
more = i < parts.size - 1
|
|
247
|
-
if
|
|
282
|
+
if encrypted
|
|
248
283
|
@io.write(@mechanism.encrypt(part.b, more: more))
|
|
249
284
|
else
|
|
250
|
-
|
|
285
|
+
body = part.b
|
|
286
|
+
size = body.bytesize
|
|
287
|
+
flags = more ? Codec::Frame::FLAGS_MORE : 0
|
|
288
|
+
buf.clear
|
|
289
|
+
if size > Codec::Frame::SHORT_MAX
|
|
290
|
+
[flags | Codec::Frame::FLAGS_LONG, size].pack("CQ>", buffer: buf)
|
|
291
|
+
else
|
|
292
|
+
[flags, size].pack("CC", buffer: buf)
|
|
293
|
+
end
|
|
294
|
+
@io.write(buf)
|
|
295
|
+
@io.write(body)
|
|
251
296
|
end
|
|
252
297
|
end
|
|
253
298
|
end
|