bunny_rpc 0.1.0 → 0.1.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 +4 -4
- data/.gitignore +1 -0
- data/README.md +12 -8
- data/Rakefile +2 -2
- data/bin/console +4 -4
- data/bunny_rpc.gemspec +12 -18
- data/lib/bunny_rpc/client.rb +75 -0
- data/lib/bunny_rpc/server.rb +47 -0
- data/lib/bunny_rpc/version.rb +1 -1
- data/lib/bunny_rpc.rb +3 -5
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85b3814ee9e2fd9a8071c13f473144edb7b3c884
|
4
|
+
data.tar.gz: 575cab001d663544971e53a18d7bd85b93ebe987
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dd3daad75a0a18d4aa7ed11d42a0cf62d30b9e0d69adf4116256d1243758cc936e3df015b83a766770c4de5cadb932d6cf0ebeb18e7b1b306ef475f9d0255c1
|
7
|
+
data.tar.gz: d18a2333bfdc23ab0bb846b3b862c83bf88cb332ed8170d58986774df5c63594db4a3e6c42443629bbdcaa295e29727d4f1c92345f37f5c9d76088a4151fac5b
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -22,20 +22,24 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
### Client
|
26
26
|
|
27
|
-
|
27
|
+
require 'bunny_rpc'
|
28
28
|
|
29
|
-
|
29
|
+
client = BunnyRpc::Client.new
|
30
30
|
|
31
|
-
|
31
|
+
result = client.publish('playload')
|
32
32
|
|
33
|
-
|
33
|
+
### Server
|
34
34
|
|
35
|
-
|
35
|
+
require 'bunny_rpc'
|
36
36
|
|
37
|
+
client = BunnyRpc::Server.new('queue1')
|
37
38
|
|
38
|
-
|
39
|
+
client.subscribe do |data|
|
40
|
+
puts data
|
39
41
|
|
40
|
-
|
42
|
+
payload = 'get it !'
|
41
43
|
|
44
|
+
client.publish(payload)
|
45
|
+
end
|
data/Rakefile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require
|
2
|
-
task :
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
task default: :spec
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bunny_rpc'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require
|
10
|
+
# require 'pry'
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/bunny_rpc.gemspec
CHANGED
@@ -4,29 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'bunny_rpc/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'bunny_rpc'
|
8
8
|
spec.version = BunnyRpc::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['menghuanwd']
|
10
|
+
spec.email = ['651019063@qq.com']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
12
|
+
spec.summary = 'bunny_rpc'
|
13
|
+
spec.description = 'bunny_rpc'
|
14
|
+
spec.homepage = 'https://github.com/menghuanwd/bunny_rpc'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
|
-
|
18
|
-
# delete this section to allow pushing this gem to any host.
|
19
|
-
# if spec.respond_to?(:metadata)
|
20
|
-
# spec.metadata['allowed_push_host'] = "https://github.com/menghuanwd/bunny_rpc"
|
21
|
-
# else
|
22
|
-
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
-
# end
|
17
|
+
spec.add_dependency 'bunny', '~> 2.3.0'
|
24
18
|
|
25
19
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
-
spec.bindir =
|
20
|
+
spec.bindir = 'exe'
|
27
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
-
spec.require_paths = [
|
22
|
+
spec.require_paths = ['lib']
|
29
23
|
|
30
|
-
spec.add_development_dependency
|
31
|
-
spec.add_development_dependency
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
26
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
|
3
|
+
module BunnyRpc
|
4
|
+
class Client
|
5
|
+
attr_reader :payload, :correlation_id
|
6
|
+
|
7
|
+
def initialize(options={})
|
8
|
+
@options = options
|
9
|
+
@mutex = Mutex.new
|
10
|
+
@cv = ConditionVariable.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def publish(body, queue_name)
|
14
|
+
subscribe
|
15
|
+
|
16
|
+
exchange.publish(body, routing_key: queue_name, reply_to: reply_queue.name, correlation_id: @correlation_id)
|
17
|
+
|
18
|
+
add_lock
|
19
|
+
|
20
|
+
@payload
|
21
|
+
end
|
22
|
+
|
23
|
+
def close
|
24
|
+
@connect.stop
|
25
|
+
end
|
26
|
+
|
27
|
+
def subscribe
|
28
|
+
reply_queue.subscribe do |_, properties, payload|
|
29
|
+
if properties.correlation_id == @correlation_id
|
30
|
+
@payload = payload
|
31
|
+
|
32
|
+
del_lock
|
33
|
+
else
|
34
|
+
del_lock
|
35
|
+
raise 'correlation_id error !'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_lock
|
41
|
+
@mutex.synchronize { @cv.wait(@mutex) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def del_lock
|
45
|
+
@mutex.synchronize { @cv.signal }
|
46
|
+
end
|
47
|
+
|
48
|
+
def connect
|
49
|
+
return @connect if defined?(@connect)
|
50
|
+
@connect = Bunny.new(@options)
|
51
|
+
@connect.start
|
52
|
+
@connect
|
53
|
+
end
|
54
|
+
|
55
|
+
def channel
|
56
|
+
@channel ||= connect.create_channel
|
57
|
+
end
|
58
|
+
|
59
|
+
def exchange
|
60
|
+
@exchange ||= channel.default_exchange
|
61
|
+
end
|
62
|
+
|
63
|
+
def reply_queue
|
64
|
+
@reply_queue ||= channel.queue(reply_queue_name, exclusive: true, auto_delete: true)
|
65
|
+
end
|
66
|
+
|
67
|
+
def reply_queue_name
|
68
|
+
"reply_to-#{rand(10)}"
|
69
|
+
end
|
70
|
+
|
71
|
+
def correlation_id
|
72
|
+
"#{rand(10)}#{rand(10)}#{rand(10)}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module BunnyRpc
|
2
|
+
class Server
|
3
|
+
attr_accessor :reply_correlation_id, :reply_to
|
4
|
+
|
5
|
+
def initialize(queue_name, options={})
|
6
|
+
@queue_name = queue_name
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def publish(payload)
|
11
|
+
exchange.publish(
|
12
|
+
payload,
|
13
|
+
routing_key: @reply_to,
|
14
|
+
correlation_id: @reply_correlation_id
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def subscribe
|
19
|
+
queue.subscribe(block: true) do |_, properties, payload|
|
20
|
+
@reply_to = properties.reply_to
|
21
|
+
@reply_correlation_id = properties.correlation_id
|
22
|
+
|
23
|
+
yield payload
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def connect
|
28
|
+
return @connect if defined?(@connect)
|
29
|
+
@connect = Bunny.new(@options)
|
30
|
+
@connect.start
|
31
|
+
@connect
|
32
|
+
end
|
33
|
+
|
34
|
+
def channel
|
35
|
+
@channel ||= connect.create_channel
|
36
|
+
end
|
37
|
+
|
38
|
+
def exchange
|
39
|
+
@exchange ||= channel.default_exchange
|
40
|
+
end
|
41
|
+
|
42
|
+
def queue
|
43
|
+
@queue ||= channel.queue(@queue_name, exclusive: true)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/lib/bunny_rpc/version.rb
CHANGED
data/lib/bunny_rpc.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bunny_rpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- menghuanwd
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bunny
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.3.0
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -55,6 +69,8 @@ files:
|
|
55
69
|
- bin/setup
|
56
70
|
- bunny_rpc.gemspec
|
57
71
|
- lib/bunny_rpc.rb
|
72
|
+
- lib/bunny_rpc/client.rb
|
73
|
+
- lib/bunny_rpc/server.rb
|
58
74
|
- lib/bunny_rpc/version.rb
|
59
75
|
homepage: https://github.com/menghuanwd/bunny_rpc
|
60
76
|
licenses:
|