nats-pure 2.3.0 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f963530316a2132bf59b1408a2d4e8393db94d429787c3d16c0317f8ff2fd555
4
- data.tar.gz: 69f369b23184f5cdf53fd886019708cbe9559d69a30d9aa7e0ca6e6e80d37f56
3
+ metadata.gz: 6a89824c45aafc2a28dbdfe35762fb3172c9210a54097456e8480630910cb741
4
+ data.tar.gz: b2f99e1fee310ffc0a689f3bb956133ad694230bed23a6c9ad987c4634db6008
5
5
  SHA512:
6
- metadata.gz: 550f13d0ed5e3bbd3203a681c86aa0fce2d6d459bbe61f06722908fcac72922369d650eec270d5222b28ed335fd5b0acdbbd3ba8134ffe1d9de49bc82774afdd
7
- data.tar.gz: ff2d22028e5af8d49ec88cd5018f485a054c3f638d2b0118bea4c7beb0f2fbb98277a0dac84c61673da3858a045b725a5ef1c02d68464c9a7fb4c4e44e24f4c3
6
+ metadata.gz: 4b2c9b1b010e7701fbafa7e161d1dd915ee24830d72df6ef5455ad87a8d07e2493442c3986121d5556f19e91a2f55ec837938831da61917c7974187b7e8fcea1
7
+ data.tar.gz: a914e41f74342ab9010f0728351fb1ac37992f71cd60b35448b1542b4818d12580a2792db9fd3ad63a00a08b0acaceff44c1c39f6f6acb7e0e764d40a26afd08
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A thread safe [Ruby](http://ruby-lang.org) client for the [NATS messaging system](https://nats.io) written in pure Ruby.
4
4
 
5
- [![License Apache 2.0](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)[![Build Status](https://travis-ci.org/nats-io/nats-pure.rb.svg)](http://travis-ci.org/nats-io/nats-pure.rb)[![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&type=5&v=2.3.0)](https://rubygems.org/gems/nats-pure/versions/2.3.0)
5
+ [![License Apache 2.0](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)[![Build Status](https://travis-ci.org/nats-io/nats-pure.rb.svg)](http://travis-ci.org/nats-io/nats-pure.rb)[![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&type=5&v=2.4.0)](https://rubygems.org/gems/nats-pure/versions/2.4.0)
6
6
 
7
7
  ## Getting Started
8
8
 
data/lib/nats/client.rb CHANGED
@@ -12,4 +12,5 @@
12
12
  # limitations under the License.
13
13
  #
14
14
  require 'nats/io/client'
15
+ require 'nats/io/rails' if defined?(Rails::Engine)
15
16
  require 'nats/nuid'
@@ -18,7 +18,6 @@ require_relative 'errors'
18
18
  require_relative 'msg'
19
19
  require_relative 'subscription'
20
20
  require_relative 'jetstream'
21
- require_relative "rails" if defined?(::Rails::Engine)
22
21
 
23
22
  require 'nats/nuid'
24
23
  require 'thread'
@@ -1825,7 +1824,7 @@ module NATS
1825
1824
 
1826
1825
  # Host and Port
1827
1826
  uri_object.hostname ||= "localhost"
1828
- uri_object.port ||= DEFAULT_PORT.fetch(uri.scheme.to_sym, DEFAULT_PORT[:nats])
1827
+ uri_object.port ||= DEFAULT_PORT.fetch(uri_object.scheme.to_sym, DEFAULT_PORT[:nats])
1829
1828
 
1830
1829
  uri_object
1831
1830
  end
@@ -119,6 +119,9 @@ module NATS
119
119
  :num_replicas,
120
120
  # Force memory storage
121
121
  :mem_storage,
122
+
123
+ # NATS v2.10 features
124
+ :metadata, :filter_subjects, :max_bytes,
122
125
  keyword_init: true) do
123
126
  def initialize(opts={})
124
127
  # Filter unrecognized fields just in case.
@@ -192,6 +195,7 @@ module NATS
192
195
  :republish,
193
196
  :allow_direct,
194
197
  :mirror_direct,
198
+ :metadata,
195
199
  keyword_init: true) do
196
200
  def initialize(opts={})
197
201
  # Filter unrecognized fields just in case.
@@ -106,18 +106,37 @@ module NATS
106
106
  else
107
107
  config
108
108
  end
109
-
109
+ config[:name] ||= config[:durable_name]
110
110
  req_subject = case
111
111
  when config[:name]
112
- # NOTE: Only supported after nats-server v2.9.0
112
+ ###############################################################################
113
+ # #
114
+ # Using names is the supported way of creating consumers (NATS +v2.9.0. #
115
+ # #
116
+ ###############################################################################
113
117
  if config[:filter_subject] && config[:filter_subject] != ">"
114
118
  "#{@prefix}.CONSUMER.CREATE.#{stream}.#{config[:name]}.#{config[:filter_subject]}"
115
119
  else
120
+ ##############################################################################
121
+ # #
122
+ # Endpoint to support creating ANY consumer with multi-filters (NATS +v2.10) #
123
+ # #
124
+ ##############################################################################
116
125
  "#{@prefix}.CONSUMER.CREATE.#{stream}.#{config[:name]}"
117
126
  end
118
127
  when config[:durable_name]
128
+ ###############################################################################
129
+ # #
130
+ # Endpoint to support creating DURABLES before NATS v2.9.0. #
131
+ # #
132
+ ###############################################################################
119
133
  "#{@prefix}.CONSUMER.DURABLE.CREATE.#{stream}.#{config[:durable_name]}"
120
134
  else
135
+ ###############################################################################
136
+ # #
137
+ # Endpoint to support creating EPHEMERALS before NATS v2.9.0. #
138
+ # #
139
+ ###############################################################################
121
140
  "#{@prefix}.CONSUMER.CREATE.#{stream}"
122
141
  end
123
142
 
@@ -105,16 +105,50 @@ module NATS
105
105
 
106
106
  # subscribe binds or creates a push subscription to a JetStream pull consumer.
107
107
  #
108
- # @param subject [String] Subject from which the messages will be fetched.
108
+ # @param subject [String, Array] Subject(s) from which the messages will be fetched.
109
109
  # @param params [Hash] Options to customize the PushSubscription.
110
110
  # @option params [String] :stream Name of the Stream to which the consumer belongs.
111
111
  # @option params [String] :consumer Name of the Consumer to which the PushSubscription will be bound.
112
+ # @option params [String] :name Name of the Consumer to which the PushSubscription will be bound.
112
113
  # @option params [String] :durable Consumer durable name from where the messages will be fetched.
113
114
  # @option params [Hash] :config Configuration for the consumer.
114
115
  # @return [NATS::JetStream::PushSubscription]
115
116
  def subscribe(subject, params={}, &cb)
116
117
  params[:consumer] ||= params[:durable]
117
- stream = params[:stream].nil? ? find_stream_name_by_subject(subject) : params[:stream]
118
+ params[:consumer] ||= params[:name]
119
+ multi_filter = case
120
+ when (subject.is_a?(Array) and subject.size == 1)
121
+ subject = subject.first
122
+ false
123
+ when (subject.is_a?(Array) and subject.size > 1)
124
+ true
125
+ end
126
+
127
+ #
128
+ stream = if params[:stream].nil?
129
+ if multi_filter
130
+ # Use the first subject to try to find the stream.
131
+ streams = subject.map do |s|
132
+ begin
133
+ find_stream_name_by_subject(s)
134
+ rescue NATS::JetStream::Error::NotFound
135
+ raise NATS::JetStream::Error.new("nats: could not find stream matching filter subject '#{s}'")
136
+ end
137
+ end
138
+
139
+ # Ensure that the filter subjects are not ambiguous.
140
+ streams.uniq!
141
+ if streams.count > 1
142
+ raise NATS::JetStream::Error.new("nats: multiple streams matched filter subjects: #{streams}")
143
+ end
144
+
145
+ streams.first
146
+ else
147
+ find_stream_name_by_subject(subject)
148
+ end
149
+ else
150
+ params[:stream]
151
+ end
118
152
 
119
153
  queue = params[:queue]
120
154
  durable = params[:durable]
@@ -183,7 +217,11 @@ module NATS
183
217
  config.deliver_subject = deliver
184
218
 
185
219
  # Auto created consumers use the filter subject.
186
- config.filter_subject = subject
220
+ if multi_filter
221
+ config[:filter_subjects] ||= subject
222
+ else
223
+ config[:filter_subject] ||= subject
224
+ end
187
225
 
188
226
  # Heartbeats / FlowControl
189
227
  config.flow_control = flow_control
@@ -219,25 +257,57 @@ module NATS
219
257
 
220
258
  # pull_subscribe binds or creates a subscription to a JetStream pull consumer.
221
259
  #
222
- # @param subject [String] Subject from which the messages will be fetched.
260
+ # @param subject [String, Array] Subject or subjects from which the messages will be fetched.
223
261
  # @param durable [String] Consumer durable name from where the messages will be fetched.
224
262
  # @param params [Hash] Options to customize the PullSubscription.
225
263
  # @option params [String] :stream Name of the Stream to which the consumer belongs.
226
264
  # @option params [String] :consumer Name of the Consumer to which the PullSubscription will be bound.
265
+ # @option params [String] :name Name of the Consumer to which the PullSubscription will be bound.
227
266
  # @option params [Hash] :config Configuration for the consumer.
228
267
  # @return [NATS::JetStream::PullSubscription]
229
268
  def pull_subscribe(subject, durable, params={})
230
- if durable.empty? && !params[:consumer]
269
+ if (!durable or durable.empty?) && !(params[:consumer] or params[:name])
231
270
  raise JetStream::Error::InvalidDurableName.new("nats: invalid durable name")
232
271
  end
272
+ multi_filter = case
273
+ when (subject.is_a?(Array) and subject.size == 1)
274
+ subject = subject.first
275
+ false
276
+ when (subject.is_a?(Array) and subject.size > 1)
277
+ true
278
+ end
279
+
233
280
  params[:consumer] ||= durable
234
- stream = params[:stream].nil? ? find_stream_name_by_subject(subject) : params[:stream]
281
+ params[:consumer] ||= params[:name]
282
+ stream = if params[:stream].nil?
283
+ if multi_filter
284
+ # Use the first subject to try to find the stream.
285
+ streams = subject.map do |s|
286
+ begin
287
+ find_stream_name_by_subject(s)
288
+ rescue NATS::JetStream::Error::NotFound
289
+ raise NATS::JetStream::Error.new("nats: could not find stream matching filter subject '#{s}'")
290
+ end
291
+ end
292
+
293
+ # Ensure that the filter subjects are not ambiguous.
294
+ streams.uniq!
295
+ if streams.count > 1
296
+ raise NATS::JetStream::Error.new("nats: multiple streams matched filter subjects: #{streams}")
297
+ end
235
298
 
299
+ streams.first
300
+ else
301
+ find_stream_name_by_subject(subject)
302
+ end
303
+ else
304
+ params[:stream]
305
+ end
236
306
  begin
237
307
  consumer_info(stream, params[:consumer])
238
308
  rescue NATS::JetStream::Error::NotFound => e
239
309
  # If attempting to bind, then this is a hard error.
240
- raise e if params[:stream]
310
+ raise e if params[:stream] and !multi_filter
241
311
 
242
312
  config = if not params[:config]
243
313
  JetStream::API::ConsumerConfig.new
@@ -248,6 +318,11 @@ module NATS
248
318
  end
249
319
  config[:durable_name] = durable
250
320
  config[:ack_policy] ||= JS::Config::AckExplicit
321
+ if multi_filter
322
+ config[:filter_subjects] ||= subject
323
+ else
324
+ config[:filter_subject] ||= subject
325
+ end
251
326
  add_consumer(stream, config)
252
327
  end
253
328
 
@@ -15,7 +15,7 @@
15
15
  module NATS
16
16
  module IO
17
17
  # VERSION is the version of the client announced on CONNECT to the server.
18
- VERSION = "2.3.0".freeze
18
+ VERSION = "2.4.0".freeze
19
19
 
20
20
  # LANG is the lang runtime of the client announced on CONNECT to the server.
21
21
  LANG = "#{RUBY_ENGINE}#{RUBY_VERSION}".freeze
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nats-pure
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Waldemar Quevedo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-09 00:00:00.000000000 Z
11
+ date: 2023-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby