nats 0.8.4 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +4 -0
- data/README.md +72 -21
- data/Rakefile +14 -0
- data/bin/nats-pub +13 -0
- data/bin/nats-queue +13 -0
- data/bin/nats-request +13 -0
- data/bin/nats-server +14 -0
- data/bin/nats-sub +13 -0
- data/bin/nats-top +13 -0
- data/lib/nats/client.rb +120 -6
- data/lib/nats/ext/bytesize.rb +14 -0
- data/lib/nats/ext/em.rb +14 -0
- data/lib/nats/ext/json.rb +14 -0
- data/lib/nats/nuid.rb +72 -0
- data/lib/nats/server.rb +13 -0
- data/lib/nats/version.rb +15 -1
- data/nats.gemspec +14 -3
- metadata +3 -3
- data/COPYING +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69469e7ad38954ab676e099443dc45f56bda0986a539cdcae552d7f760595eaa
|
4
|
+
data.tar.gz: ed8f954f8ae1da8bbca64a09dc57ae8ee532628ab9e8d871836ccc490f6d2266
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c924a60aa9d2380da3fb916cf7336f6c59d1c10c1685dc198577a5593aa84849c7c1839ce2d0ed818bed012aa2c7ca0fa27f16f665267ba49512dca44a2ff41
|
7
|
+
data.tar.gz: 10d496a25c6783ce15f92323dcbc76b927f2a7c6ea2fc196c46218f4d1cc6854a3ab303f560454977ee07d4fd5671431d94e164bfabb82b37d6dba5a89320413
|
data/HISTORY.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# HISTORY
|
2
2
|
|
3
|
+
## v0.8.4 (Feb 23, 2018)
|
4
|
+
- Support to include connection `name` as part of CONNECT options (#145)
|
5
|
+
- Fixed support for Ruby 2.5 due to missing OpenSSL `require` (#144)
|
6
|
+
|
3
7
|
## v0.8.2 (March 14, 2017)
|
4
8
|
- Allow setting name from client on connect (#129)
|
5
9
|
- Add discovered servers helper for servers announced via async INFO (#136)
|
data/README.md
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
A [Ruby](http://ruby-lang.org) client for the [NATS messaging system](https://nats.io).
|
4
4
|
|
5
|
-
[![License
|
6
|
-
[![Build Status](https://travis-ci.org/nats-io/ruby-nats.svg)](http://travis-ci.org/nats-io/ruby-nats) [![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&type=5&v=0.
|
5
|
+
[![License Apache 2.0](https://img.shields.io/badge/License-Apache2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
|
6
|
+
[![Build Status](https://travis-ci.org/nats-io/ruby-nats.svg)](http://travis-ci.org/nats-io/ruby-nats) [![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&type=5&v=0.9.0)](https://rubygems.org/gems/nats/versions/0.9.0) [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/nats-io/ruby-nats)
|
7
7
|
|
8
8
|
## Supported Platforms
|
9
9
|
|
@@ -156,6 +156,8 @@ end
|
|
156
156
|
NATS.connect { NATS.publish('test', 'Hello World!') }
|
157
157
|
```
|
158
158
|
|
159
|
+
See examples and benchmarks for more information..
|
160
|
+
|
159
161
|
### TLS
|
160
162
|
|
161
163
|
Advanced customizations options for setting up a secure connection can
|
@@ -210,29 +212,78 @@ NATS.start(options) do |nats|
|
|
210
212
|
end
|
211
213
|
```
|
212
214
|
|
213
|
-
|
215
|
+
### Fibers
|
214
216
|
|
215
|
-
|
217
|
+
Requests without a callback can be made to work synchronously and return
|
218
|
+
the result when running in a Fiber. For these type of requests, it is
|
219
|
+
possible to set a timeout of how long to wait for a single or multiple
|
220
|
+
responses.
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
NATS.start {
|
216
224
|
|
217
|
-
(
|
225
|
+
NATS.subscribe('help') do |msg, reply|
|
226
|
+
puts "[Received]: <<- #{msg}"
|
227
|
+
NATS.publish(reply, "I'll help! - #{msg}")
|
228
|
+
end
|
218
229
|
|
219
|
-
|
230
|
+
NATS.subscribe('slow') do |msg, reply|
|
231
|
+
puts "[Received]: <<- #{msg}"
|
232
|
+
EM.add_timer(1) { NATS.publish(reply, "I'll help! - #{msg}") }
|
233
|
+
end
|
220
234
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
furnished to do so, subject to the following conditions:
|
235
|
+
10.times do |n|
|
236
|
+
NATS.subscribe('hi') do |msg, reply|
|
237
|
+
NATS.publish(reply, "Hello World! - id:#{n}")
|
238
|
+
end
|
239
|
+
end
|
227
240
|
|
228
|
-
|
229
|
-
|
241
|
+
Fiber.new do
|
242
|
+
# Requests work synchronously within the same Fiber
|
243
|
+
# returning the message when done.
|
244
|
+
response = NATS.request('help', 'foo')
|
245
|
+
puts "[Response]: ->> '#{response}'"
|
246
|
+
|
247
|
+
# Specifying a custom timeout to give up waiting for
|
248
|
+
# a response.
|
249
|
+
response = NATS.request('slow', 'bar', timeout: 2)
|
250
|
+
if response.nil?
|
251
|
+
puts "No response after 2 seconds..."
|
252
|
+
else
|
253
|
+
puts "[Response]: ->> '#{response}'"
|
254
|
+
end
|
255
|
+
|
256
|
+
# Can gather multiple responses with the same request
|
257
|
+
# which will then return a collection with the responses
|
258
|
+
# that were received before the timeout.
|
259
|
+
responses = NATS.request('hi', 'quux', max: 10, timeout: 1)
|
260
|
+
responses.each_with_index do |response, i|
|
261
|
+
puts "[Response# #{i}]: ->> '#{response}'"
|
262
|
+
end
|
263
|
+
|
264
|
+
# If no replies then an empty collection is returned.
|
265
|
+
responses = NATS.request('nowhere', '', max: 10, timeout: 2)
|
266
|
+
if responses.any?
|
267
|
+
puts "Got #{responses.count} responses"
|
268
|
+
else
|
269
|
+
puts "No response after 2 seconds..."
|
270
|
+
end
|
271
|
+
|
272
|
+
NATS.stop
|
273
|
+
end.resume
|
274
|
+
|
275
|
+
# Multiple fibers can make requests concurrently
|
276
|
+
# under the same Eventmachine loop.
|
277
|
+
Fiber.new do
|
278
|
+
10.times do |n|
|
279
|
+
response = NATS.request('help', "help.#{n}")
|
280
|
+
puts "[Response]: ->> '#{response}'"
|
281
|
+
end
|
282
|
+
end.resume
|
283
|
+
}
|
284
|
+
```
|
230
285
|
|
231
|
-
|
232
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
233
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
234
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
235
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
236
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
237
|
-
IN THE SOFTWARE.
|
286
|
+
## License
|
238
287
|
|
288
|
+
Unless otherwise noted, the NATS source files are distributed under
|
289
|
+
the Apache Version 2.0 license found in the LICENSE file.
|
data/Rakefile
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
+
# Copyright 2010-2018 The NATS Authors
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#
|
15
|
+
|
2
16
|
require 'rspec/core'
|
3
17
|
require 'rspec/core/rake_task'
|
4
18
|
|
data/bin/nats-pub
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# Copyright 2010-2018 The NATS Authors
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#
|
2
15
|
|
3
16
|
require 'optparse'
|
4
17
|
require 'rubygems'
|
data/bin/nats-queue
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# Copyright 2010-2018 The NATS Authors
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#
|
2
15
|
|
3
16
|
require 'optparse'
|
4
17
|
require 'rubygems'
|
data/bin/nats-request
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# Copyright 2010-2018 The NATS Authors
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#
|
2
15
|
|
3
16
|
require 'optparse'
|
4
17
|
require 'rubygems'
|
data/bin/nats-server
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# Copyright 2010-2018 The NATS Authors
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#
|
15
|
+
|
2
16
|
# NATS command line interface script.
|
3
17
|
# Run <tt>nats-server -h</tt> to get more usage.
|
4
18
|
|
data/bin/nats-sub
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# Copyright 2010-2018 The NATS Authors
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#
|
2
15
|
|
3
16
|
require 'optparse'
|
4
17
|
require 'rubygems'
|
data/bin/nats-top
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# Copyright 2010-2018 The NATS Authors
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
#
|
2
15
|
|
3
16
|
require 'optparse'
|
4
17
|
require 'net/http'
|
data/lib/nats/client.rb
CHANGED
@@ -1,5 +1,20 @@
|
|
1
|
+
# Copyright 2010-2018 The NATS Authors
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
14
|
+
|
1
15
|
require 'uri'
|
2
16
|
require 'securerandom'
|
17
|
+
require 'fiber'
|
3
18
|
require 'openssl' unless defined?(OpenSSL)
|
4
19
|
|
5
20
|
ep = File.expand_path(File.dirname(__FILE__))
|
@@ -8,6 +23,7 @@ require "#{ep}/ext/em"
|
|
8
23
|
require "#{ep}/ext/bytesize"
|
9
24
|
require "#{ep}/ext/json"
|
10
25
|
require "#{ep}/version"
|
26
|
+
require "#{ep}/nuid"
|
11
27
|
|
12
28
|
module NATS
|
13
29
|
|
@@ -361,6 +377,12 @@ module NATS
|
|
361
377
|
@tls = options[:tls] if options[:tls]
|
362
378
|
@ssl = options[:ssl] if options[:ssl] or @tls
|
363
379
|
|
380
|
+
# New style request/response implementation.
|
381
|
+
@resp_sub = nil
|
382
|
+
@resp_map = nil
|
383
|
+
@resp_sub_prefix = nil
|
384
|
+
@nuid = NATS::NUID.new
|
385
|
+
|
364
386
|
send_connect_command
|
365
387
|
end
|
366
388
|
|
@@ -446,16 +468,108 @@ module NATS
|
|
446
468
|
# @return [Object] sid
|
447
469
|
def request(subject, data=nil, opts={}, &cb)
|
448
470
|
return unless subject
|
449
|
-
|
450
|
-
|
451
|
-
|
471
|
+
|
472
|
+
# In case of using async request then fallback to auto unsubscribe
|
473
|
+
# based request/response and not break compatibility too much since
|
474
|
+
# new request/response style can only be used with fibers.
|
475
|
+
if cb
|
476
|
+
inbox = "_INBOX.#{@nuid.next}"
|
477
|
+
s = subscribe(inbox, opts) { |msg, reply|
|
478
|
+
case cb.arity
|
452
479
|
when 0 then cb.call
|
453
480
|
when 1 then cb.call(msg)
|
454
481
|
else cb.call(msg, reply)
|
455
|
-
|
456
|
-
|
482
|
+
end
|
483
|
+
}
|
484
|
+
publish(subject, data, inbox)
|
485
|
+
return s
|
486
|
+
end
|
487
|
+
|
488
|
+
# If this is the first request being made, then need to start
|
489
|
+
# the responses mux handler that handles the responses.
|
490
|
+
start_resp_mux_sub! unless @resp_sub_prefix
|
491
|
+
|
492
|
+
# Generate unique token for the reply subject.
|
493
|
+
token = @nuid.next
|
494
|
+
inbox = "#{@resp_sub_prefix}.#{token}"
|
495
|
+
|
496
|
+
# Synchronous request/response requires using a Fiber
|
497
|
+
# to be able to await the response.
|
498
|
+
f = Fiber.current
|
499
|
+
@resp_map[token][:fiber] = f
|
500
|
+
|
501
|
+
# If awaiting more than a single response then use array
|
502
|
+
# to include all that could be gathered before the deadline.
|
503
|
+
expected = opts[:max] ||= 1
|
504
|
+
@resp_map[token][:expected] = expected
|
505
|
+
@resp_map[token][:msgs] = [] if expected > 1
|
506
|
+
|
507
|
+
# Announce the request with the inbox using the token.
|
457
508
|
publish(subject, data, inbox)
|
458
|
-
|
509
|
+
|
510
|
+
# If deadline expires, then discard the token and resume fiber
|
511
|
+
opts[:timeout] ||= 0.5
|
512
|
+
t = EM.add_timer(opts[:timeout]) do
|
513
|
+
if expected > 1
|
514
|
+
f.resume @resp_map[token][:msgs]
|
515
|
+
else
|
516
|
+
f.resume
|
517
|
+
end
|
518
|
+
|
519
|
+
@resp_map.delete(token)
|
520
|
+
end
|
521
|
+
|
522
|
+
# Wait for the response and cancel timeout callback if received.
|
523
|
+
if expected > 1
|
524
|
+
# Wait to receive all replies that can get before deadline.
|
525
|
+
msgs = Fiber.yield
|
526
|
+
EM.cancel_timer(t)
|
527
|
+
|
528
|
+
# Slice and throwaway responses that are not needed.
|
529
|
+
return msgs.slice(0, expected)
|
530
|
+
else
|
531
|
+
msg = Fiber.yield
|
532
|
+
EM.cancel_timer(t)
|
533
|
+
return msg
|
534
|
+
end
|
535
|
+
end
|
536
|
+
|
537
|
+
def start_resp_mux_sub!
|
538
|
+
@resp_sub_prefix = "_INBOX.#{@nuid.next}"
|
539
|
+
@resp_map = Hash.new { |h,k| h[k] = { }}
|
540
|
+
|
541
|
+
# Single subscription that will be handling all the requests
|
542
|
+
# using fibers to yield the responses.
|
543
|
+
subscribe("#{@resp_sub_prefix}.*") do |msg, reply, subject|
|
544
|
+
token = subject.split('.').last
|
545
|
+
|
546
|
+
# Discard the response if requestor not interested already.
|
547
|
+
next unless @resp_map.key? token
|
548
|
+
|
549
|
+
# Take fiber that will be passed the response
|
550
|
+
f = @resp_map[token][:fiber]
|
551
|
+
expected = @resp_map[token][:expected]
|
552
|
+
|
553
|
+
if expected == 1
|
554
|
+
f.resume msg
|
555
|
+
@resp_map.delete(token)
|
556
|
+
next
|
557
|
+
end
|
558
|
+
|
559
|
+
if @resp_map[token][:msgs].size < expected
|
560
|
+
@resp_map[token][:msgs] << msg
|
561
|
+
|
562
|
+
msgs = @resp_map[token][:msgs]
|
563
|
+
if msgs.size >= expected
|
564
|
+
f.resume(msgs)
|
565
|
+
else
|
566
|
+
# Wait to gather more messages or timeout.
|
567
|
+
next
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
571
|
+
@resp_map.delete(token)
|
572
|
+
end
|
459
573
|
end
|
460
574
|
|
461
575
|
# Flushes all messages and subscriptions for the connection.
|
data/lib/nats/ext/bytesize.rb
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
# Copyright 2010-2018 The NATS Authors
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
14
|
+
|
1
15
|
if RUBY_VERSION <= "1.8.6"
|
2
16
|
class String #:nodoc:
|
3
17
|
def bytesize; self.size; end
|
data/lib/nats/ext/em.rb
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
# Copyright 2010-2018 The NATS Authors
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
14
|
+
|
1
15
|
begin
|
2
16
|
require 'eventmachine'
|
3
17
|
rescue LoadError
|
data/lib/nats/ext/json.rb
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
# Copyright 2010-2018 The NATS Authors
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
14
|
+
|
1
15
|
begin
|
2
16
|
require 'yajl'
|
3
17
|
require 'yajl/json_gem'
|
data/lib/nats/nuid.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Copyright 2016-2018 The NATS Authors
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
14
|
+
require 'securerandom'
|
15
|
+
|
16
|
+
module NATS
|
17
|
+
class NUID
|
18
|
+
DIGITS = [*'0'..'9', *'A'..'Z', *'a'..'z']
|
19
|
+
BASE = 62
|
20
|
+
PREFIX_LENGTH = 12
|
21
|
+
SEQ_LENGTH = 10
|
22
|
+
TOTAL_LENGTH = PREFIX_LENGTH + SEQ_LENGTH
|
23
|
+
MAX_SEQ = BASE**10
|
24
|
+
MIN_INC = 33
|
25
|
+
MAX_INC = 333
|
26
|
+
INC = MAX_INC - MIN_INC
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@prand = Random.new
|
30
|
+
@seq = @prand.rand(MAX_SEQ)
|
31
|
+
@inc = MIN_INC + @prand.rand(INC)
|
32
|
+
@prefix = ''
|
33
|
+
randomize_prefix!
|
34
|
+
end
|
35
|
+
|
36
|
+
def next
|
37
|
+
@seq += @inc
|
38
|
+
if @seq >= MAX_SEQ
|
39
|
+
randomize_prefix!
|
40
|
+
reset_sequential!
|
41
|
+
end
|
42
|
+
l = @seq
|
43
|
+
|
44
|
+
# Do this inline 10 times to avoid even more extra allocs,
|
45
|
+
# then use string interpolation of everything which works
|
46
|
+
# faster for doing concat.
|
47
|
+
s_10 = DIGITS[l % BASE];
|
48
|
+
|
49
|
+
# Ugly, but parallel assignment is slightly faster here...
|
50
|
+
s_09, s_08, s_07, s_06, s_05, s_04, s_03, s_02, s_01 = \
|
51
|
+
(l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]),\
|
52
|
+
(l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]),\
|
53
|
+
(l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE])
|
54
|
+
"#{@prefix}#{s_01}#{s_02}#{s_03}#{s_04}#{s_05}#{s_06}#{s_07}#{s_08}#{s_09}#{s_10}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def randomize_prefix!
|
58
|
+
@prefix = \
|
59
|
+
SecureRandom.random_bytes(PREFIX_LENGTH).each_byte
|
60
|
+
.reduce('') do |prefix, n|
|
61
|
+
prefix << DIGITS[n % BASE]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def reset_sequential!
|
68
|
+
@seq = @prand.rand(MAX_SEQ)
|
69
|
+
@inc = MIN_INC + @prand.rand(INC)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/nats/server.rb
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
# Copyright 2010-2018 The NATS Authors
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
1
14
|
|
2
15
|
require 'socket'
|
3
16
|
require 'fileutils'
|
data/lib/nats/version.rb
CHANGED
@@ -1,6 +1,20 @@
|
|
1
|
+
# Copyright 2010-2018 The NATS Authors
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
14
|
+
|
1
15
|
module NATS
|
2
16
|
# NOTE: These are all announced to the server on CONNECT
|
3
|
-
VERSION = "0.
|
17
|
+
VERSION = "0.9.0".freeze
|
4
18
|
LANG = RUBY_ENGINE
|
5
19
|
PROTOCOL_VERSION = 1
|
6
20
|
end
|
data/nats.gemspec
CHANGED
@@ -1,4 +1,16 @@
|
|
1
|
-
|
1
|
+
# Copyright 2010-2018 The NATS Authors
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
2
14
|
|
3
15
|
lib = File.expand_path('../lib/', __FILE__)
|
4
16
|
$:.unshift lib unless $:.include?(lib)
|
@@ -12,7 +24,6 @@ spec = Gem::Specification.new do |s|
|
|
12
24
|
s.homepage = 'https://nats.io'
|
13
25
|
s.description = 'NATS is an open-source, high-performance, lightweight cloud messaging system.'
|
14
26
|
s.licenses = ['MIT']
|
15
|
-
s.has_rdoc = true
|
16
27
|
|
17
28
|
s.authors = ['Derek Collison']
|
18
29
|
s.email = ['derek.collison@gmail.com']
|
@@ -23,7 +34,6 @@ spec = Gem::Specification.new do |s|
|
|
23
34
|
s.executables = ['nats-pub', 'nats-sub', 'nats-queue', 'nats-request']
|
24
35
|
|
25
36
|
s.files = %w[
|
26
|
-
COPYING
|
27
37
|
README.md
|
28
38
|
HISTORY.md
|
29
39
|
nats.gemspec
|
@@ -35,6 +45,7 @@ spec = Gem::Specification.new do |s|
|
|
35
45
|
bin/nats-top
|
36
46
|
bin/nats-request
|
37
47
|
lib/nats/client.rb
|
48
|
+
lib/nats/nuid.rb
|
38
49
|
lib/nats/version.rb
|
39
50
|
lib/nats/ext/bytesize.rb
|
40
51
|
lib/nats/ext/em.rb
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nats
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derek Collison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|
@@ -42,7 +42,6 @@ executables:
|
|
42
42
|
extensions: []
|
43
43
|
extra_rdoc_files: []
|
44
44
|
files:
|
45
|
-
- COPYING
|
46
45
|
- HISTORY.md
|
47
46
|
- README.md
|
48
47
|
- Rakefile
|
@@ -56,6 +55,7 @@ files:
|
|
56
55
|
- lib/nats/ext/bytesize.rb
|
57
56
|
- lib/nats/ext/em.rb
|
58
57
|
- lib/nats/ext/json.rb
|
58
|
+
- lib/nats/nuid.rb
|
59
59
|
- lib/nats/server.rb
|
60
60
|
- lib/nats/server/cluster.rb
|
61
61
|
- lib/nats/server/connection.rb
|
data/COPYING
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
Copyright (c) 2010, 2011 Derek Collison <derek.collison@gmail.com>. All rights reserved.
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
of this software and associated documentation files (the "Software"), to
|
5
|
-
deal in the Software without restriction, including without limitation the
|
6
|
-
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
-
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
-
furnished to do so, subject to the following conditions:
|
9
|
-
|
10
|
-
The above copyright notice and this permission notice shall be included in
|
11
|
-
all copies or substantial portions of the Software.
|
12
|
-
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
19
|
-
IN THE SOFTWARE.
|