jetstream_bridge 4.5.2 → 4.5.3
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/jetstream_bridge/consumer/subscription_manager.rb +73 -26
- data/lib/jetstream_bridge/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: eda1d1166867a08139af3cb85b6d9aca0765d5444d4a44a37bbb42e8bb519a4f
|
|
4
|
+
data.tar.gz: 267dba8c88cc5fbff41323ef05b74d3cb6cb9491de38d68b95d2d0fd52a4be03
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1614bfc73e26c5e21820a0dab70ff61f61e3d33cbfbe2fec5e60bea44b9b83d377e694ade633e0e495d105bef18ab9d5782911d5f05a8bd3356f8e9cbecf7779
|
|
7
|
+
data.tar.gz: ab40c30a9f1566cd2bdc7c41a4721b950041052d7365540d517b154bcd34d63cc2daf5dd1042982161a279c8d2940bb7d3df3d536c4a9a796d0abcb907f4a46d
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'json'
|
|
3
4
|
require_relative '../core/logging'
|
|
4
5
|
require_relative '../core/duration'
|
|
5
6
|
require_relative '../errors'
|
|
@@ -47,32 +48,7 @@ module JetstreamBridge
|
|
|
47
48
|
# This bypasses the permission check in nats-pure's pull_subscribe
|
|
48
49
|
nc = resolve_nc
|
|
49
50
|
|
|
50
|
-
if nc.respond_to?(:new_inbox) && nc.respond_to?(:subscribe)
|
|
51
|
-
prefix = @jts.instance_variable_get(:@prefix) || '$JS.API'
|
|
52
|
-
deliver = nc.new_inbox
|
|
53
|
-
sub = nc.subscribe(deliver)
|
|
54
|
-
|
|
55
|
-
# Extend with PullSubscription module to add fetch methods
|
|
56
|
-
sub.extend(NATS::JetStream::PullSubscription)
|
|
57
|
-
|
|
58
|
-
# Set up the JSI (JetStream Info) struct that PullSubscription expects
|
|
59
|
-
# This matches what nats-pure does in pull_subscribe
|
|
60
|
-
subject = "#{prefix}.CONSUMER.MSG.NEXT.#{stream_name}.#{@durable}"
|
|
61
|
-
sub.jsi = NATS::JetStream::JS::Sub.new(
|
|
62
|
-
js: @jts,
|
|
63
|
-
stream: stream_name,
|
|
64
|
-
consumer: @durable,
|
|
65
|
-
nms: subject
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
Logging.info(
|
|
69
|
-
"Created pull subscription without verification for consumer #{@durable} " \
|
|
70
|
-
"(stream=#{stream_name}, filter=#{filter_subject})",
|
|
71
|
-
tag: 'JetstreamBridge::Consumer'
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
return sub
|
|
75
|
-
end
|
|
51
|
+
return build_pull_subscription(nc) if nc.respond_to?(:new_inbox) && nc.respond_to?(:subscribe)
|
|
76
52
|
|
|
77
53
|
# Fallback for environments (mocks/tests) where low-level NATS client is unavailable.
|
|
78
54
|
if @jts.respond_to?(:pull_subscribe)
|
|
@@ -193,5 +169,76 @@ module JetstreamBridge
|
|
|
193
169
|
|
|
194
170
|
nil
|
|
195
171
|
end
|
|
172
|
+
|
|
173
|
+
def build_pull_subscription(nats_client)
|
|
174
|
+
prefix = @jts.instance_variable_get(:@prefix) || '$JS.API'
|
|
175
|
+
deliver = nats_client.new_inbox
|
|
176
|
+
sub = nats_client.subscribe(deliver)
|
|
177
|
+
sub.instance_variable_set(:@_jsb_nc, nats_client)
|
|
178
|
+
sub.instance_variable_set(:@_jsb_deliver, deliver)
|
|
179
|
+
sub.instance_variable_set(:@_jsb_next_subject, "#{prefix}.CONSUMER.MSG.NEXT.#{stream_name}.#{@durable}")
|
|
180
|
+
|
|
181
|
+
extend_pull_subscription(sub)
|
|
182
|
+
attach_jsi(sub)
|
|
183
|
+
|
|
184
|
+
Logging.info(
|
|
185
|
+
"Created pull subscription without verification for consumer #{@durable} " \
|
|
186
|
+
"(stream=#{stream_name}, filter=#{filter_subject})",
|
|
187
|
+
tag: 'JetstreamBridge::Consumer'
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
sub
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def extend_pull_subscription(sub)
|
|
194
|
+
pull_mod = begin
|
|
195
|
+
NATS::JetStream.const_get(:PullSubscription)
|
|
196
|
+
rescue NameError
|
|
197
|
+
nil
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
sub.extend(pull_mod) if pull_mod
|
|
201
|
+
shim_fetch(sub) unless pull_mod
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def shim_fetch(sub)
|
|
205
|
+
Logging.warn(
|
|
206
|
+
'PullSubscription mixin unavailable; using shim fetch implementation',
|
|
207
|
+
tag: 'JetstreamBridge::Consumer'
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
sub.define_singleton_method(:fetch) do |batch_size, timeout: nil|
|
|
211
|
+
nc_handle = instance_variable_get(:@_jsb_nc)
|
|
212
|
+
deliver_subject = instance_variable_get(:@_jsb_deliver)
|
|
213
|
+
next_subject = instance_variable_get(:@_jsb_next_subject)
|
|
214
|
+
unless nc_handle && deliver_subject && next_subject
|
|
215
|
+
raise JetstreamBridge::ConnectionError, 'Missing NATS handles for fetch'
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
expires_ns = ((timeout || 5).to_f * 1_000_000_000).to_i
|
|
219
|
+
payload = { batch: batch_size, expires: expires_ns }.to_json
|
|
220
|
+
|
|
221
|
+
nc_handle.publish(next_subject, payload, deliver_subject)
|
|
222
|
+
nc_handle.flush
|
|
223
|
+
|
|
224
|
+
messages = []
|
|
225
|
+
batch_size.times do
|
|
226
|
+
msg = next_msg(timeout || 5)
|
|
227
|
+
messages << msg if msg
|
|
228
|
+
rescue NATS::IO::Timeout, NATS::Timeout
|
|
229
|
+
break
|
|
230
|
+
end
|
|
231
|
+
messages
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def attach_jsi(sub)
|
|
236
|
+
sub.jsi = NATS::JetStream::JS::Sub.new(
|
|
237
|
+
js: @jts,
|
|
238
|
+
stream: stream_name,
|
|
239
|
+
consumer: @durable,
|
|
240
|
+
nms: sub.instance_variable_get(:@_jsb_next_subject)
|
|
241
|
+
)
|
|
242
|
+
end
|
|
196
243
|
end
|
|
197
244
|
end
|