nats-rpc 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ea664ac47363302e169e1bcec559adb687c8f7d973a5de6b0400678d673dd0d3
4
+ data.tar.gz: f138923d3d4ca30d039777d6defb45e0873a930d555ba56deb96331a32471c73
5
+ SHA512:
6
+ metadata.gz: 52eb7a7357a3359ebde3c94dbbf909e2838bbe272ed913a8dadb5f5e25f5e05d0859dea28f9a98ddf07e4334fd689160dd3593fad4ff0c0d1e0353060b8eadc2
7
+ data.tar.gz: 5e38dbbbc061ad9e8188f654348fd1dc58a97d267f7e90582baf6beba3bcd1bc568a4c933dd9e37fdd426b2d2b1caf7d9f5b3d36628f8f4060b4034303a452b4
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ nats-rpc
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in nats-rpc.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nats-rpc (0.0.1)
5
+ nats-pure
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ nats-pure (0.5.0)
12
+ rake (10.5.0)
13
+ rspec (3.7.0)
14
+ rspec-core (~> 3.7.0)
15
+ rspec-expectations (~> 3.7.0)
16
+ rspec-mocks (~> 3.7.0)
17
+ rspec-core (3.7.1)
18
+ rspec-support (~> 3.7.0)
19
+ rspec-expectations (3.7.0)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.7.0)
22
+ rspec-mocks (3.7.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.7.0)
25
+ rspec-support (3.7.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ nats-rpc!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Matti Paksula
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # nats-rpc
2
+
3
+ ```
4
+ gem install 'nats-rpc'
5
+ ```
6
+
7
+ ## usage
8
+
9
+ servant.rb
10
+ ```ruby
11
+ require "nats-rpc"
12
+
13
+ servant = NATS::RPC::Servant.new
14
+ servant.serve! 'testing', queue: 'test' do |params, subject|
15
+ puts "got params: #{params.inspect} in subject: #{subject}"
16
+
17
+ params["reverse"].reverse
18
+ end
19
+ ```
20
+
21
+ client.rb
22
+ ```ruby
23
+ client = NATS::RPC::Client.new
24
+ msg = {reverse: "hello"}
25
+ data, payload = client.request 'testing', msg, timeout: 1, queue: 'test'
26
+
27
+ puts data
28
+ # => {"status"=>"ok", "payload"=>"\"olleh\"", "took"=>0.0, "servant"=>"112a1f87-2d01-4339-a30d-ddc542ccd383"}
29
+ puts payload
30
+ # => olleh
31
+ ```
32
+
33
+ ## advanced usage
34
+
35
+ servant.rb
36
+ ```ruby
37
+ require "nats-rpc"
38
+
39
+ cluster_opts = {
40
+ servers: ["nats://127.0.0.1:4222", "nats://127.0.0.1:4223"],
41
+ dont_randomize_servers: true,
42
+ reconnect_time_wait: 0.5,
43
+ max_reconnect_attempts: 2
44
+ }
45
+
46
+ servant = NATS::RPC::Servant.new id: "a", cluster_opts
47
+ servant.serve! 'testing' do |params, subject|
48
+ puts "got params: #{params.inspect} in subject: #{subject}"
49
+
50
+ params["reverse"].reverse
51
+ end
52
+
53
+ # client requests show that they were served from servant "a"
54
+ # => {"status"=>"ok", "payload"=>"\"olleh\"", "took"=>0.0, "servant"=>"a"}
55
+ ```
56
+
57
+ ## testing
58
+
59
+ see `e2e/` and `docker-compose.yml`
60
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nats/rpc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ version: '3.6'
2
+
3
+ services:
4
+ nats:
5
+ image: nats:1.2.0-linux
6
+ ports:
7
+ - 4222:4222
8
+ - 6222:6222
data/e2e/client.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative "../lib/nats/rpc"
2
+
3
+ client = NATS::RPC::Client.new
4
+ data, payload = client.request 'testing', {reverse: "hello"}, timeout: 1, queue: 'e2e'
5
+
6
+ puts data
7
+ puts payload
data/e2e/main.rb ADDED
@@ -0,0 +1,26 @@
1
+ require_relative "../lib/nats/rpc"
2
+
3
+ servant_a = NATS::RPC::Servant.new id: "a"
4
+ servant_b = NATS::RPC::Servant.new id: "b"
5
+ servant_uuid = NATS::RPC::Servant.new
6
+
7
+ block = -> (params, subject) do
8
+ puts "got params: #{params.inspect} in subject: #{subject}"
9
+ params["reverse"].reverse
10
+ end
11
+
12
+ servant_a.serve 'testing', queue: "e2e", &block
13
+ servant_b.serve 'testing', queue: "e2e", &block
14
+ servant_uuid.serve 'testing', queue: "e2e", &block
15
+
16
+ sleep 0.5
17
+
18
+ 10.times do
19
+ load "e2e/client.rb"
20
+ end
21
+
22
+ servant_a.kill
23
+ servant_b.kill
24
+ servant_uuid.kill
25
+
26
+
data/e2e/servant.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative "../lib/nats/rpc"
2
+
3
+ servant = NATS::RPC::Servant.new
4
+ servant.serve! 'testing', queue: "e2e" do |params, subject|
5
+ puts "got params: #{params.inspect} in subject: #{subject}"
6
+ params["reverse"].reverse
7
+ end
@@ -0,0 +1,24 @@
1
+ module NATS
2
+ module RPC
3
+ class Client
4
+ def initialize(servers: ["nats://127.0.0.1:4222"])
5
+ @nats = NATS::IO::Client.new
6
+ @nats.connect servers: servers
7
+ end
8
+
9
+ def request(subscription, obj, opts)
10
+ obj_json = if obj.is_a? String
11
+ obj
12
+ else
13
+ obj.to_json
14
+ end
15
+
16
+ msg = @nats.request 'testing', obj_json, opts
17
+ data = JSON.parse(msg["data"])
18
+ payload = JSON.parse(data["payload"])
19
+
20
+ [data, payload]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,107 @@
1
+ module NATS
2
+ module RPC
3
+ class Servant
4
+ def initialize(id: SecureRandom.uuid, **cluster_opts)
5
+ @id = id
6
+
7
+ cluster_opts = {
8
+ servers: ["nats://127.0.0.1:4222"]
9
+ } unless cluster_opts
10
+
11
+ @nats = NATS::IO::Client.new
12
+ @nats.connect cluster_opts
13
+
14
+ @count_json_parse_errors = 0
15
+ @count_block_call_errors = 0
16
+ @count_to_json_errors = 0
17
+ @count_messages = 0
18
+
19
+ @serve_thread = nil
20
+ end
21
+
22
+ def serve(subscribe_to, opts={}, &block)
23
+ @serve_thread = Thread.new do
24
+ self.serve! subscribe_to, opts, &block
25
+ end
26
+ end
27
+
28
+ def kill
29
+ return unless @serve_thread
30
+ @serve_thread.kill
31
+ end
32
+
33
+ def serve!(subscribe_to, opts={}, &block)
34
+ sid = @nats.subscribe subscribe_to, opts do |msg_json, reply, subject|
35
+ params = nil
36
+ json_parse_exception = nil
37
+ begin
38
+ params = JSON.parse(msg_json)
39
+ rescue => ex
40
+ @count_json_parse_errors = @count_json_parse_errors + 1
41
+ json_parse_exception = ex
42
+ end
43
+
44
+ return @nats.publish reply, error_message(1.0, json_parse_exception.message) if json_parse_exception
45
+
46
+ value = nil
47
+ block_call_started_at = Time.now
48
+ block_call_exception = nil
49
+ begin
50
+ value = block.call params, subject
51
+ rescue => ex
52
+ @count_block_call_errors = @count_block_call_errors + 1
53
+ block_call_exception = ex
54
+ end
55
+ block_call_stopped_at = Time.now
56
+
57
+ return @nats.publish reply, error_message(2.0, block_call_exception.message) if block_call_exception
58
+
59
+ value_as_json = nil
60
+ begin
61
+ value_as_json = value.to_json
62
+ rescue => ex
63
+ @count_to_json_errors = @count_to_json_errors + 1
64
+ value_to_json_exception = ex
65
+ end
66
+
67
+ return @nats.publish reply, error_message(3.0, value_to_json_exception.message) if value_to_json_exception
68
+
69
+ response = {
70
+ status: "ok",
71
+ payload: value_as_json,
72
+ took: (block_call_stopped_at - block_call_started_at).floor(2),
73
+ servant: @id
74
+ }
75
+
76
+ @nats.publish(reply, response.to_json)
77
+
78
+ @count_messages = @count_messages + 1
79
+ end
80
+
81
+ last_count_messages = 0
82
+ loop do
83
+ throughput = (last_count_messages - @count_messages).abs
84
+ debug "messages: #{@count_messages} throughput: #{throughput}/s errors json_parse: #{@count_json_parse_errors} block_call: #{@count_block_call_errors} to_json: #{@count_to_json_errors}"
85
+ last_count_messages = @count_messages
86
+ sleep 1
87
+ end
88
+ end
89
+
90
+ private
91
+
92
+ def error_message(code, data)
93
+ {
94
+ status: "error",
95
+ code: code,
96
+ payload: data,
97
+ servant: @id
98
+ }.to_json
99
+ end
100
+
101
+ def debug(str)
102
+ return unless ENV["NATS_RPC_DEBUG"] == "true"
103
+ puts str
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,5 @@
1
+ module NATS
2
+ module RPC
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/nats/rpc.rb ADDED
@@ -0,0 +1,12 @@
1
+ $stdout.sync = true
2
+ require 'nats/io/client'
3
+ require 'json'
4
+
5
+ require_relative "rpc/version"
6
+ require_relative "rpc/servant"
7
+ require_relative "rpc/client"
8
+
9
+ module NATS
10
+ module RPC
11
+ end
12
+ end
data/nats-rpc.gemspec ADDED
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "nats/rpc/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nats-rpc"
8
+ spec.version = NATS::RPC::VERSION
9
+ spec.authors = ["Matti Paksula"]
10
+ spec.email = ["matti.paksula@iki.fi"]
11
+
12
+ spec.summary = "RPC implemented on top of NATS"
13
+ spec.description = "RPC implemented on top of NATS"
14
+ spec.homepage = "https://github.com/matti/nats-rpc"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "nats-pure"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nats-rpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matti Paksula
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nats-pure
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: RPC implemented on top of NATS
70
+ email:
71
+ - matti.paksula@iki.fi
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-gemset"
79
+ - ".ruby-version"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - docker-compose.yml
89
+ - e2e/client.rb
90
+ - e2e/main.rb
91
+ - e2e/servant.rb
92
+ - lib/nats/rpc.rb
93
+ - lib/nats/rpc/client.rb
94
+ - lib/nats/rpc/servant.rb
95
+ - lib/nats/rpc/version.rb
96
+ - nats-rpc.gemspec
97
+ homepage: https://github.com/matti/nats-rpc
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.7.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: RPC implemented on top of NATS
121
+ test_files: []