nats-pure 0.6.0 → 2.0.0.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nats/client.rb +15 -0
- data/lib/nats/io/client.rb +1380 -1141
- data/lib/nats/io/errors.rb +62 -0
- data/lib/nats/io/js.rb +1090 -0
- data/lib/nats/io/msg.rb +56 -0
- data/lib/nats/io/parser.rb +22 -8
- data/lib/nats/io/subscription.rb +92 -0
- data/lib/nats/io/version.rb +8 -4
- data/lib/nats.rb +39 -0
- metadata +14 -8
data/lib/nats/io/msg.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Copyright 2016-2021 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_relative 'js'
|
15
|
+
|
16
|
+
module NATS
|
17
|
+
class Msg
|
18
|
+
attr_accessor :subject, :reply, :data, :header
|
19
|
+
|
20
|
+
# Enhance it with ack related methods from JetStream to ack msgs.
|
21
|
+
include JetStream::Msg::AckMethods
|
22
|
+
|
23
|
+
def initialize(opts={})
|
24
|
+
@subject = opts[:subject]
|
25
|
+
@reply = opts[:reply]
|
26
|
+
@data = opts[:data]
|
27
|
+
@header = opts[:header]
|
28
|
+
@nc = opts[:nc]
|
29
|
+
@sub = opts[:sub]
|
30
|
+
@ackd = false
|
31
|
+
@meta = nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def respond(data='')
|
35
|
+
return unless @nc
|
36
|
+
if self.header
|
37
|
+
dmsg = self.dup
|
38
|
+
dmsg.subject = self.reply
|
39
|
+
dmsg.data = data
|
40
|
+
@nc.publish_msg(dmsg)
|
41
|
+
else
|
42
|
+
@nc.publish(self.reply, data)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def respond_msg(msg)
|
47
|
+
return unless @nc
|
48
|
+
@nc.publish_msg(msg)
|
49
|
+
end
|
50
|
+
|
51
|
+
def inspect
|
52
|
+
hdr = ", header=#{@header}" if @header
|
53
|
+
"#<NATS::Msg(subject: \"#{@subject}\", reply: \"#{@reply}\", data: #{@data.slice(0, 10).inspect}#{hdr})>"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/nats/io/parser.rb
CHANGED
@@ -16,6 +16,7 @@ module NATS
|
|
16
16
|
module Protocol
|
17
17
|
|
18
18
|
MSG = /\AMSG\s+([^\s]+)\s+([^\s]+)\s+(([^\s]+)[^\S\r\n]+)?(\d+)\r\n/i
|
19
|
+
HMSG = /\AHMSG\s+([^\s]+)\s+([^\s]+)\s+(([^\s]+)[^\S\r\n]+)?([\d]+)\s+(\d+)\r\n/i
|
19
20
|
OK = /\A\+OK\s*\r\n/i
|
20
21
|
ERR = /\A-ERR\s+('.+')?\r\n/i
|
21
22
|
PING = /\APING\s*\r\n/i
|
@@ -49,6 +50,7 @@ module NATS
|
|
49
50
|
@sid = nil
|
50
51
|
@reply = nil
|
51
52
|
@needed = nil
|
53
|
+
@header_needed = nil
|
52
54
|
end
|
53
55
|
|
54
56
|
def parse(data)
|
@@ -61,26 +63,30 @@ module NATS
|
|
61
63
|
@buf = $'
|
62
64
|
@sub, @sid, @reply, @needed = $1, $2.to_i, $4, $5.to_i
|
63
65
|
@parse_state = AWAITING_MSG_PAYLOAD
|
66
|
+
when HMSG
|
67
|
+
@buf = $'
|
68
|
+
@sub, @sid, @reply, @header_needed, @needed = $1, $2.to_i, $4, $5.to_i, $6.to_i
|
69
|
+
@parse_state = AWAITING_MSG_PAYLOAD
|
64
70
|
when OK # No-op right now
|
65
71
|
@buf = $'
|
66
72
|
when ERR
|
67
73
|
@buf = $'
|
68
|
-
@nc.process_err
|
74
|
+
@nc.send(:process_err, $1)
|
69
75
|
when PING
|
70
76
|
@buf = $'
|
71
|
-
@nc.process_ping
|
77
|
+
@nc.send(:process_ping)
|
72
78
|
when PONG
|
73
79
|
@buf = $'
|
74
|
-
@nc.process_pong
|
80
|
+
@nc.send(:process_pong)
|
75
81
|
when INFO
|
76
82
|
@buf = $'
|
77
83
|
# First INFO message is processed synchronously on connect,
|
78
84
|
# and onwards we would be receiving asynchronously INFO commands
|
79
85
|
# signaling possible changes in the topology of the NATS cluster.
|
80
|
-
@nc.process_info
|
86
|
+
@nc.send(:process_info, $1)
|
81
87
|
when UNKNOWN
|
82
88
|
@buf = $'
|
83
|
-
@nc.process_err
|
89
|
+
@nc.send(:process_err, "Unknown protocol: #{$1}")
|
84
90
|
else
|
85
91
|
# If we are here we do not have a complete line yet that we understand.
|
86
92
|
return
|
@@ -89,9 +95,17 @@ module NATS
|
|
89
95
|
|
90
96
|
when AWAITING_MSG_PAYLOAD
|
91
97
|
return unless (@needed && @buf.bytesize >= (@needed + CR_LF_SIZE))
|
92
|
-
|
93
|
-
|
94
|
-
|
98
|
+
if @header_needed
|
99
|
+
hbuf = @buf.slice(0, @header_needed)
|
100
|
+
payload = @buf.slice(@header_needed, (@needed-@header_needed))
|
101
|
+
@nc.send(:process_msg, @sub, @sid, @reply, payload, hbuf)
|
102
|
+
@buf = @buf.slice((@needed + CR_LF_SIZE), @buf.bytesize)
|
103
|
+
else
|
104
|
+
@nc.send(:process_msg, @sub, @sid, @reply, @buf.slice(0, @needed), nil)
|
105
|
+
@buf = @buf.slice((@needed + CR_LF_SIZE), @buf.bytesize)
|
106
|
+
end
|
107
|
+
|
108
|
+
@sub = @sid = @reply = @needed = @header_needed = nil
|
95
109
|
@parse_state = AWAITING_CONTROL_LINE
|
96
110
|
@buf = nil if (@buf && @buf.empty?)
|
97
111
|
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# Copyright 2016-2021 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
|
+
|
15
|
+
module NATS
|
16
|
+
|
17
|
+
# A Subscription represents interest in a given subject.
|
18
|
+
#
|
19
|
+
# @example Create NATS subscription with callback.
|
20
|
+
# require 'nats/client'
|
21
|
+
#
|
22
|
+
# nc = NATS.connect("demo.nats.io")
|
23
|
+
# sub = nc.subscribe("foo") do |msg|
|
24
|
+
# puts "Received [#{msg.subject}]: #{}"
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
class Subscription
|
28
|
+
include MonitorMixin
|
29
|
+
|
30
|
+
attr_accessor :subject, :queue, :future, :callback, :response, :received, :max, :pending, :sid
|
31
|
+
attr_accessor :pending_queue, :pending_size, :wait_for_msgs_t, :wait_for_msgs_cond, :is_slow_consumer
|
32
|
+
attr_accessor :pending_msgs_limit, :pending_bytes_limit
|
33
|
+
attr_accessor :nc
|
34
|
+
attr_accessor :jsi
|
35
|
+
attr_accessor :closed
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
super # required to initialize monitor
|
39
|
+
@subject = ''
|
40
|
+
@queue = nil
|
41
|
+
@future = nil
|
42
|
+
@callback = nil
|
43
|
+
@response = nil
|
44
|
+
@received = 0
|
45
|
+
@max = nil
|
46
|
+
@pending = nil
|
47
|
+
@sid = nil
|
48
|
+
@nc = nil
|
49
|
+
@closed = nil
|
50
|
+
|
51
|
+
# State from async subscriber messages delivery
|
52
|
+
@pending_queue = nil
|
53
|
+
@pending_size = 0
|
54
|
+
@pending_msgs_limit = nil
|
55
|
+
@pending_bytes_limit = nil
|
56
|
+
@wait_for_msgs_t = nil
|
57
|
+
@is_slow_consumer = false
|
58
|
+
|
59
|
+
# Sync subscriber
|
60
|
+
@wait_for_msgs_cond = nil
|
61
|
+
end
|
62
|
+
|
63
|
+
# Auto unsubscribes the server by sending UNSUB command and throws away
|
64
|
+
# subscription in case already present and has received enough messages.
|
65
|
+
def unsubscribe(opt_max=nil)
|
66
|
+
@nc.send(:unsubscribe, self, opt_max)
|
67
|
+
end
|
68
|
+
|
69
|
+
# next_msg blocks and waiting for the next message to be received.
|
70
|
+
def next_msg(opts={})
|
71
|
+
timeout = opts[:timeout] ||= 0.5
|
72
|
+
synchronize do
|
73
|
+
return @pending_queue.pop if not @pending_queue.empty?
|
74
|
+
|
75
|
+
# Wait for a bit until getting a signal.
|
76
|
+
MonotonicTime::with_nats_timeout(timeout) do
|
77
|
+
wait_for_msgs_cond.wait(timeout)
|
78
|
+
end
|
79
|
+
|
80
|
+
if not @pending_queue.empty?
|
81
|
+
return @pending_queue.pop
|
82
|
+
else
|
83
|
+
raise NATS::Timeout
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def inspect
|
89
|
+
"#<NATS::Subscription(subject: \"#{@subject}\", queue: \"#{@queue}\", sid: #{@sid})>"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/nats/io/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright 2016-
|
1
|
+
# Copyright 2016-2021 The NATS Authors
|
2
2
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
3
|
# you may not use this file except in compliance with the License.
|
4
4
|
# You may obtain a copy of the License at
|
@@ -14,9 +14,13 @@
|
|
14
14
|
|
15
15
|
module NATS
|
16
16
|
module IO
|
17
|
-
#
|
18
|
-
VERSION
|
19
|
-
|
17
|
+
# VERSION is the version of the client announced on CONNECT to the server.
|
18
|
+
VERSION = "2.0.0-alpha".freeze
|
19
|
+
|
20
|
+
# LANG is the lang runtime of the client announced on CONNECT to the server.
|
21
|
+
LANG = "#{RUBY_ENGINE}#{RUBY_VERSION}".freeze
|
22
|
+
|
23
|
+
# PROTOCOL is the supported version of the protocol in the client.
|
20
24
|
PROTOCOL = 1
|
21
25
|
end
|
22
26
|
end
|
data/lib/nats.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Copyright 2021 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
|
+
|
15
|
+
require 'nats/io/client'
|
16
|
+
require 'nats/nuid'
|
17
|
+
|
18
|
+
# A thread safe Ruby client for the NATS messaging system (https://nats.io).
|
19
|
+
#
|
20
|
+
# @example Service example
|
21
|
+
# nc = NATS.connect("demo.nats.io")
|
22
|
+
# nc.subscribe("foo") do |msg|
|
23
|
+
# msg.respond("Hello World")
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# resp = nc.request("foo")
|
27
|
+
# puts "Received: #{msg.data}"
|
28
|
+
#
|
29
|
+
#
|
30
|
+
# @example Stream example
|
31
|
+
# nc = NATS.connect("demo.nats.io")
|
32
|
+
# sub = nc.subscribe("foo")
|
33
|
+
#
|
34
|
+
# nc.publish("foo")
|
35
|
+
# msg = sub.next_msg
|
36
|
+
# puts "Received: #{msg.data}"
|
37
|
+
#
|
38
|
+
module NATS
|
39
|
+
end
|
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: 0.
|
4
|
+
version: 2.0.0.pre.alpha
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Waldemar Quevedo
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: NATS is an open-source, high-performance, lightweight cloud messaging
|
14
14
|
system.
|
@@ -18,15 +18,21 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- lib/nats.rb
|
22
|
+
- lib/nats/client.rb
|
21
23
|
- lib/nats/io/client.rb
|
24
|
+
- lib/nats/io/errors.rb
|
25
|
+
- lib/nats/io/js.rb
|
26
|
+
- lib/nats/io/msg.rb
|
22
27
|
- lib/nats/io/parser.rb
|
28
|
+
- lib/nats/io/subscription.rb
|
23
29
|
- lib/nats/io/version.rb
|
24
30
|
- lib/nats/nuid.rb
|
25
31
|
homepage: https://nats.io
|
26
32
|
licenses:
|
27
33
|
- Apache-2.0
|
28
34
|
metadata: {}
|
29
|
-
post_install_message:
|
35
|
+
post_install_message:
|
30
36
|
rdoc_options: []
|
31
37
|
require_paths:
|
32
38
|
- lib
|
@@ -37,12 +43,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
43
|
version: '0'
|
38
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
45
|
requirements:
|
40
|
-
- - "
|
46
|
+
- - ">"
|
41
47
|
- !ruby/object:Gem::Version
|
42
|
-
version:
|
48
|
+
version: 1.3.1
|
43
49
|
requirements: []
|
44
|
-
rubygems_version: 3.
|
45
|
-
signing_key:
|
50
|
+
rubygems_version: 3.2.22
|
51
|
+
signing_key:
|
46
52
|
specification_version: 4
|
47
53
|
summary: NATS is an open-source, high-performance, lightweight cloud messaging system.
|
48
54
|
test_files: []
|