zeromem-ws 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +53 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/zeromem/ws/version.rb +5 -0
- data/lib/zeromem/ws.rb +85 -0
- data/ws_test.rb +15 -0
- data/zeromem-ws.gemspec +30 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 579bf8ce72bba762da6743212abec307be29a5a5
|
4
|
+
data.tar.gz: 96aefe2c08de15ff3bd6da05f3dda0084a551299
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a60bcb1a4aa5ad28f7bdea84831aa9289458f9a5ed775257af50c6ef1d5b1198b447cfe2890b1a1fb6805b0873836c590507a86002395dc6ea25fc3b376558a7
|
7
|
+
data.tar.gz: 20e7ca7b02553693d2f7144609f85077a69438d8a442c37e1f7b766024352d970187f1a66e5fa87cb2c046ce89f53d651e4c4005542a7b6b9a5602122d333619
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
zeromem-ws (0.1.1)
|
5
|
+
em-websocket (~> 0.5.1)
|
6
|
+
em-websocket-client (~> 0.1.2)
|
7
|
+
rjr (>= 0.19.3)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
diff-lcs (1.3)
|
13
|
+
em-websocket (0.5.1)
|
14
|
+
eventmachine (>= 0.12.9)
|
15
|
+
http_parser.rb (~> 0.6.0)
|
16
|
+
em-websocket-client (0.1.2)
|
17
|
+
eventmachine
|
18
|
+
websocket
|
19
|
+
eventmachine (1.2.5)
|
20
|
+
http_parser.rb (0.6.0)
|
21
|
+
json (2.1.0)
|
22
|
+
rake (10.5.0)
|
23
|
+
rjr (0.19.3)
|
24
|
+
eventmachine
|
25
|
+
json (>= 1.7.6)
|
26
|
+
rspec (3.7.0)
|
27
|
+
rspec-core (~> 3.7.0)
|
28
|
+
rspec-expectations (~> 3.7.0)
|
29
|
+
rspec-mocks (~> 3.7.0)
|
30
|
+
rspec-core (3.7.0)
|
31
|
+
rspec-support (~> 3.7.0)
|
32
|
+
rspec-expectations (3.7.0)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.7.0)
|
35
|
+
rspec-mocks (3.7.0)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.7.0)
|
38
|
+
rspec-support (3.7.0)
|
39
|
+
websocket (1.2.5)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
x86-mingw32
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
bundler (~> 1.16)
|
47
|
+
eventmachine (>= 1.2.5)
|
48
|
+
rake (~> 10.0)
|
49
|
+
rspec (~> 3.0)
|
50
|
+
zeromem-ws!
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 zeromem
|
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,39 @@
|
|
1
|
+
# Zeromem::Ws
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zeromem/ws`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'zeromem-ws'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install zeromem-ws
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zeromem-ws.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zeromem/ws"
|
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
data/lib/zeromem/ws.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "zeromem/ws/version"
|
2
|
+
require 'rjr/core_ext'
|
3
|
+
require 'rjr/nodes/ws'
|
4
|
+
|
5
|
+
module Zeromem
|
6
|
+
module Ws
|
7
|
+
class WsHeavy < RJR::Nodes::WS
|
8
|
+
# WS initializer
|
9
|
+
# @param [Hash] args the options to create the web socket node with
|
10
|
+
# @option args [String] :host the hostname/ip which to listen on
|
11
|
+
# @option args [Integer] :port the port which to listen on
|
12
|
+
def initialize(args = {})
|
13
|
+
super(args)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Instructs node to send rpc request, and wait for / return response
|
17
|
+
#
|
18
|
+
# Implementation of Zeromem::Ws::WsHeavy#invoke
|
19
|
+
# This is custom implementation of RJR::Node#invoke
|
20
|
+
# to make things work perfectly under heavy load (thousands of ws queries per min)
|
21
|
+
#
|
22
|
+
# Do not invoke directly from em event loop or callback as will block the message
|
23
|
+
# subscription used to receive responses
|
24
|
+
#
|
25
|
+
# @param [String] uri location of node to send request to, should be
|
26
|
+
# in format of ws://hostname:port
|
27
|
+
# @param [String] rpc_method json-rpc method to invoke on destination
|
28
|
+
# @param [Array] args array of arguments to convert to json and invoke remote method wtih
|
29
|
+
def invoke(uri, rpc_method, *args)
|
30
|
+
message = RJR::Messages::Request.new :method => rpc_method,
|
31
|
+
:args => args,
|
32
|
+
:headers => @message_headers
|
33
|
+
|
34
|
+
@@em.schedule {
|
35
|
+
init_client(uri) do |c|
|
36
|
+
c.stream { |msg| handle_message(msg.data, c) }
|
37
|
+
|
38
|
+
c.send_msg message.to_s
|
39
|
+
end
|
40
|
+
}
|
41
|
+
|
42
|
+
# TODO optional timeout for response ?
|
43
|
+
# this cause resource leak
|
44
|
+
#result = wait_for_result(message)
|
45
|
+
|
46
|
+
result = wait_for_result_custom(message)
|
47
|
+
|
48
|
+
if result.size > 2
|
49
|
+
fail result[2]
|
50
|
+
end
|
51
|
+
return result[1]
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
def wait_for_result_custom(message)
|
56
|
+
res = nil
|
57
|
+
message_id = message.msg_id
|
58
|
+
@pending[message_id] = Time.now
|
59
|
+
while res.nil?
|
60
|
+
@response_lock.synchronize{
|
61
|
+
# Prune messages that timed out
|
62
|
+
if @timeout
|
63
|
+
now = Time.now
|
64
|
+
@pending.delete_if { |_, start_time| (now - start_time) > @timeout }
|
65
|
+
end
|
66
|
+
pending_ids = @pending.keys
|
67
|
+
fail 'Timed out' unless pending_ids.include? message_id
|
68
|
+
|
69
|
+
# Prune invalid responses
|
70
|
+
@responses.keep_if { |response| @pending.has_key? response.first }
|
71
|
+
res = @responses.find { |response| message.msg_id == response.first }
|
72
|
+
if !res.nil?
|
73
|
+
@responses.delete(res)
|
74
|
+
@pending.delete(message_id)
|
75
|
+
else
|
76
|
+
@response_cv.wait @response_lock, @wait_interval
|
77
|
+
end
|
78
|
+
}
|
79
|
+
end
|
80
|
+
return res
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/ws_test.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'zeromem/ws'
|
2
|
+
|
3
|
+
server = Zeromem::Ws::WsHeavy.new :host => 'localhost', :port => 9789, :node_id => "server"
|
4
|
+
server.dispatcher.handle('method') { |i|
|
5
|
+
puts "server: #{i}"
|
6
|
+
"#{i}".upcase
|
7
|
+
}
|
8
|
+
server.listen
|
9
|
+
|
10
|
+
client = Zeromem::Ws::WsHeavy.new :node_id => "client", :timeout => 8000
|
11
|
+
|
12
|
+
0.upto(50000) { |i|
|
13
|
+
puts "#{i}"
|
14
|
+
puts client.invoke "ws://localhost:9789", "method", "Hello World"
|
15
|
+
}
|
data/zeromem-ws.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "zeromem/ws/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zeromem-ws"
|
8
|
+
spec.version = Zeromem::Ws::VERSION
|
9
|
+
spec.authors = ["zeromem88"]
|
10
|
+
spec.email = ["zeromem2@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Extended websocket client based on Movitto/rjr package}
|
13
|
+
spec.description = %q{Eliminates performance degradation of Movitto/rjr websocket json-rpc client under heavy load}
|
14
|
+
spec.homepage = "https://github.com/zeromem88/ruby-zeromem-ws"
|
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_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_dependency "em-websocket", "~> 0.5.1"
|
28
|
+
spec.add_dependency "em-websocket-client", "~> 0.1.2"
|
29
|
+
spec.add_dependency "rjr", ">= 0.19.3"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zeromem-ws
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- zeromem88
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: em-websocket
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.5.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.5.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: em-websocket-client
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.1.2
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.1.2
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rjr
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.19.3
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.19.3
|
97
|
+
description: Eliminates performance degradation of Movitto/rjr websocket json-rpc
|
98
|
+
client under heavy load
|
99
|
+
email:
|
100
|
+
- zeromem2@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".travis.yml"
|
108
|
+
- Gemfile
|
109
|
+
- Gemfile.lock
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- bin/console
|
114
|
+
- bin/setup
|
115
|
+
- lib/zeromem/ws.rb
|
116
|
+
- lib/zeromem/ws/version.rb
|
117
|
+
- ws_test.rb
|
118
|
+
- zeromem-ws.gemspec
|
119
|
+
homepage: https://github.com/zeromem88/ruby-zeromem-ws
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 2.6.11
|
140
|
+
signing_key:
|
141
|
+
specification_version: 4
|
142
|
+
summary: Extended websocket client based on Movitto/rjr package
|
143
|
+
test_files: []
|