thrifty-bunny 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +94 -0
- data/Rakefile +6 -0
- data/examples/bin/client.start +14 -0
- data/examples/bin/server.start +7 -0
- data/examples/hello_thrift/client.rb +97 -0
- data/examples/hello_thrift/hello_service/hello_service.rb +78 -0
- data/examples/hello_thrift/hello_service/hello_service_constants.rb +9 -0
- data/examples/hello_thrift/hello_service/hello_service_types.rb +8 -0
- data/examples/hello_thrift/server.rb +27 -0
- data/examples/hello_thrift/version.rb +3 -0
- data/lib/thrifty_bunny/client_transport.rb +129 -0
- data/lib/thrifty_bunny/config.rb +28 -0
- data/lib/thrifty_bunny/rpc_server.rb +125 -0
- data/lib/thrifty_bunny/version.rb +3 -0
- data/lib/thrifty_bunny.rb +4 -0
- data/spec/integration/client_spec.rb +3 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/unit/client_transport_spec.rb +8 -0
- data/spec/unit/rpc_server_spec.rb +8 -0
- data/thrifty-bunny.gemspec +30 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5992b16b439065274e52b44230cc0c58d52c65b7
|
4
|
+
data.tar.gz: 69050ac8af6876f408b1d985f3a0cb8148c379c0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 83ca6bff1660815814706532e698afd88fff307ef8410657714a6284de14bc4d984fd94675cca58cda5e215498469c93fa5b18e4c70faab2aaf98c96caf12c56
|
7
|
+
data.tar.gz: 5b428709190ca727626111340c0fcaa7aa5232504664c77f62738d477b42c090f4c06e8dd34320a7dbdb59d09d8757118dcce4be66a1d0531905eacc28c25c99
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Bill Siggelkow
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Thrifty::Bunny
|
2
|
+
|
3
|
+
A RabbitMQ-ApacheThrift adapter for RPC calls
|
4
|
+
|
5
|
+
## Acknowledgements
|
6
|
+
|
7
|
+
This gem is largely-based on the [Stephen Henrie's Apache Thrift AMQP](https://github.com/shenrie/apache-thrift-amqp). Thanks to Stephen for his work and excellent tutorial.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'thrifty-bunny'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install thrifty-bunny
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
The gem provides a Thrift server that integrates with RabbitMQ. You would use this server in standard Thrift fashion:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
handler = MyHandler.new
|
31
|
+
processor = MyProcessor.new(handler)
|
32
|
+
service = ThriftyBunny::RpcServer.new(processor)
|
33
|
+
service.serve
|
34
|
+
```
|
35
|
+
|
36
|
+
Then you would utilize the included ```ClientTransport``` to connect your Thrift client to RabbitMQ:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
transport = ThriftyBunny::ClientTransport.new
|
40
|
+
protocol = Thrift::BinaryProtocol.new(transport)
|
41
|
+
client = MyService::Client.new(protocol)
|
42
|
+
client.my_remote_method() # Make the remote procedure call
|
43
|
+
```
|
44
|
+
|
45
|
+
### Configuration
|
46
|
+
|
47
|
+
Both the ```RpcServer``` and the ```ClientTransport``` accept a ```ThriftyBunny::Config``` object that encapsulates the RabbitMQ configuration settings. The default values for the configuration options are:
|
48
|
+
|
49
|
+
option | value | notes
|
50
|
+
------------- | ------------ | -------------------------------------
|
51
|
+
host | 127.0.0.1 | Host name or IP for the RabbitMQ host
|
52
|
+
port | 5672 | Port that RabbitMQ is listening on
|
53
|
+
vhost | / | Virtual host path
|
54
|
+
user | guest | RabbitMQ user
|
55
|
+
password | guest | RabbitMQ password
|
56
|
+
queue | rpc_queue | Name of RabbitMQ queue for the RPC messages
|
57
|
+
exchange | rpc_exchange | Name of the RabbitMQ exchange for the RPC messages
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
## Examples
|
62
|
+
|
63
|
+
An easy way to experiment with the gem is to take a look at the provided examples. There is an example simple client and server. To run the example:
|
64
|
+
|
65
|
+
1. Start up RabbitMQ -- it should be running on the default port (5672) with the default credentials.
|
66
|
+
|
67
|
+
2. Start up two separate terminals
|
68
|
+
|
69
|
+
3. Change to the ```examples``` directory in each terminal:
|
70
|
+
```
|
71
|
+
$ cd examples
|
72
|
+
```
|
73
|
+
|
74
|
+
4. In one terminal, start the server:
|
75
|
+
```
|
76
|
+
$ bundle exec bin/server.start
|
77
|
+
```
|
78
|
+
|
79
|
+
5. In the other terminal, start the client:
|
80
|
+
```
|
81
|
+
$ bundle exec bin/client.start
|
82
|
+
```
|
83
|
+
|
84
|
+
6. In the [RabbitMQ admin console](http://localhost:15672), you can monitor the message queues.
|
85
|
+
|
86
|
+
The client prompts you to enter a name, then the servers respond by saying hello to you. Feel free to start up additional servers to demonstrate how the system scales.
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
1. Fork it ( https://github.com/[my-github-username]/thrifty-bunny/fork )
|
91
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
92
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
93
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
94
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require_relative '../hello_thrift/client'
|
3
|
+
|
4
|
+
# transport = ThriftyBunny::ClientTransport.new
|
5
|
+
# protocol = Thrift::BinaryProtocol.new(transport)
|
6
|
+
# client = HelloThrift::Client.new(protocol)
|
7
|
+
# client = HelloThrift::Client.new
|
8
|
+
|
9
|
+
loop do
|
10
|
+
print "What is your name? "
|
11
|
+
input = gets.strip
|
12
|
+
break if input == 'quit'
|
13
|
+
puts HelloThrift::Client.new.say_hello(input)
|
14
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'thrifty_bunny'
|
2
|
+
require_relative 'hello_service/hello_service'
|
3
|
+
|
4
|
+
module HelloThrift
|
5
|
+
|
6
|
+
class Client # < HelloService::Client
|
7
|
+
|
8
|
+
def initialize(options={})
|
9
|
+
@transport = ThriftyBunny::ClientTransport.new
|
10
|
+
protocol = Thrift::BinaryProtocol.new(@transport)
|
11
|
+
@client = HelloService::Client.new(protocol)
|
12
|
+
end
|
13
|
+
|
14
|
+
def say_hello(name)
|
15
|
+
safe_transport do
|
16
|
+
@client.say_hello(name)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def safe_transport
|
23
|
+
begin
|
24
|
+
@transport.open
|
25
|
+
return yield
|
26
|
+
rescue ThriftyBunny::ResponseTimeout => e
|
27
|
+
puts e
|
28
|
+
ensure
|
29
|
+
@transport.close
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# def send_message(name, args_class, args = {})
|
34
|
+
# @oprot.write_message_begin(name, Thrift::MessageTypes::CALL, @seqid)
|
35
|
+
# data = args_class.new
|
36
|
+
# fld = 1
|
37
|
+
|
38
|
+
# args.each do |k, v|
|
39
|
+
|
40
|
+
# if data.struct_fields[fld][:type] == Thrift::Types::STRUCT and data.struct_fields[fld][:class] == JsonData
|
41
|
+
# res = Json_data.new()
|
42
|
+
# res.data = JSON.generate(v)
|
43
|
+
# v = res
|
44
|
+
# end
|
45
|
+
|
46
|
+
# data.send("#{k.to_s}=", v)
|
47
|
+
# fld += 1
|
48
|
+
# end
|
49
|
+
# begin
|
50
|
+
# data.write(@oprot)
|
51
|
+
# rescue StandardError => e
|
52
|
+
# @oprot.trans.close
|
53
|
+
# raise e
|
54
|
+
# end
|
55
|
+
# @oprot.write_message_end
|
56
|
+
|
57
|
+
# #If the oneway modifier is used in the thrift definition, then the recv_ version of the methods will
|
58
|
+
# #not be generated, so if it does not exist then no need to wait for a response.
|
59
|
+
# blocking_call = self.respond_to?("recv_" + name)
|
60
|
+
|
61
|
+
# @oprot.trans.flush(:operation => name,
|
62
|
+
# :blocking => blocking_call,
|
63
|
+
# :log_messages => true)
|
64
|
+
|
65
|
+
# end
|
66
|
+
|
67
|
+
# def receive_message(result_klass)
|
68
|
+
# fname, mtype, rseqid = @iprot.read_message_begin
|
69
|
+
# handle_exception(mtype)
|
70
|
+
# result = result_klass.new
|
71
|
+
# result.read(@iprot)
|
72
|
+
# @iprot.read_message_end
|
73
|
+
|
74
|
+
# if result.respond_to?(:success) and result.success.class == JsonData and
|
75
|
+
# result.struct_fields[0][:type] == Thrift::Types::STRUCT and result.struct_fields[0][:class] == JsonData
|
76
|
+
# json_res = JSON.parse(result.success.data)
|
77
|
+
|
78
|
+
# result.success = json_res
|
79
|
+
# end
|
80
|
+
|
81
|
+
# result
|
82
|
+
|
83
|
+
# end
|
84
|
+
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
# client = HelloThrift::Client.new
|
91
|
+
|
92
|
+
# loop do
|
93
|
+
# print "What is your name? "
|
94
|
+
# input = gets.strip
|
95
|
+
# break if input == 'quit'
|
96
|
+
# puts client.say_hello(input)
|
97
|
+
# end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
#
|
2
|
+
# Autogenerated by Thrift Compiler (0.9.1)
|
3
|
+
#
|
4
|
+
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'thrift'
|
8
|
+
require_relative 'hello_service_types'
|
9
|
+
|
10
|
+
module HelloService
|
11
|
+
class Client
|
12
|
+
include ::Thrift::Client
|
13
|
+
|
14
|
+
def say_hello(name)
|
15
|
+
send_say_hello(name)
|
16
|
+
return recv_say_hello()
|
17
|
+
end
|
18
|
+
|
19
|
+
def send_say_hello(name)
|
20
|
+
send_message('say_hello', Say_hello_args, :name => name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def recv_say_hello()
|
24
|
+
result = receive_message(Say_hello_result)
|
25
|
+
return result.success unless result.success.nil?
|
26
|
+
raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'say_hello failed: unknown result')
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
class Processor
|
32
|
+
include ::Thrift::Processor
|
33
|
+
|
34
|
+
def process_say_hello(seqid, iprot, oprot)
|
35
|
+
args = read_args(iprot, Say_hello_args)
|
36
|
+
result = Say_hello_result.new()
|
37
|
+
result.success = @handler.say_hello(args.name)
|
38
|
+
write_result(result, oprot, 'say_hello', seqid)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
# HELPER FUNCTIONS AND STRUCTURES
|
44
|
+
|
45
|
+
class Say_hello_args
|
46
|
+
include ::Thrift::Struct, ::Thrift::Struct_Union
|
47
|
+
NAME = 1
|
48
|
+
|
49
|
+
FIELDS = {
|
50
|
+
NAME => {:type => ::Thrift::Types::STRING, :name => 'name'}
|
51
|
+
}
|
52
|
+
|
53
|
+
def struct_fields; FIELDS; end
|
54
|
+
|
55
|
+
def validate
|
56
|
+
end
|
57
|
+
|
58
|
+
::Thrift::Struct.generate_accessors self
|
59
|
+
end
|
60
|
+
|
61
|
+
class Say_hello_result
|
62
|
+
include ::Thrift::Struct, ::Thrift::Struct_Union
|
63
|
+
SUCCESS = 0
|
64
|
+
|
65
|
+
FIELDS = {
|
66
|
+
SUCCESS => {:type => ::Thrift::Types::STRING, :name => 'success'}
|
67
|
+
}
|
68
|
+
|
69
|
+
def struct_fields; FIELDS; end
|
70
|
+
|
71
|
+
def validate
|
72
|
+
end
|
73
|
+
|
74
|
+
::Thrift::Struct.generate_accessors self
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'thrifty_bunny'
|
2
|
+
require_relative 'hello_service/hello_service'
|
3
|
+
|
4
|
+
module HelloThrift
|
5
|
+
|
6
|
+
class Handler
|
7
|
+
def say_hello(name)
|
8
|
+
puts "Saying hello to #{name}"
|
9
|
+
"Hello, #{name} from Thrift!"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Server
|
14
|
+
attr_reader :service
|
15
|
+
|
16
|
+
def initialize(options={})
|
17
|
+
handler = Handler.new
|
18
|
+
processor = HelloService::Processor.new(handler)
|
19
|
+
@service = ThriftyBunny::RpcServer.new(processor)
|
20
|
+
end
|
21
|
+
|
22
|
+
def serve
|
23
|
+
service.serve
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'thrift'
|
2
|
+
require 'bunny'
|
3
|
+
require 'uuidtools'
|
4
|
+
|
5
|
+
module ThriftyBunny
|
6
|
+
|
7
|
+
class ResponseTimeout < Timeout::Error; end
|
8
|
+
|
9
|
+
class ClientTransport < ::Thrift::BaseTransport
|
10
|
+
|
11
|
+
def initialize(config=Config.new, options={})
|
12
|
+
@service_queue_name = config.queue
|
13
|
+
@outbuf = Thrift::Bytes.empty_byte_buffer
|
14
|
+
|
15
|
+
if options[:connection].nil?
|
16
|
+
@conn = Bunny.new(config.bunny_config)
|
17
|
+
@conn.start
|
18
|
+
@connection_started = true
|
19
|
+
else
|
20
|
+
@conn = options[:connection]
|
21
|
+
@connection_started = false
|
22
|
+
end
|
23
|
+
|
24
|
+
@from_name = options[:from_name] || 'Unknown Client'
|
25
|
+
@exchange = options[:exchange]
|
26
|
+
|
27
|
+
@ch = @conn.create_channel
|
28
|
+
@service_exchange = @exchange.nil? ? @ch.default_exchange : @ch.direct(@exchange, durable: true)
|
29
|
+
@service_response_exchange = @ch.default_exchange
|
30
|
+
@reply_queue = @ch.queue('', exclusive: true)
|
31
|
+
@is_opened = true
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
def close
|
36
|
+
if @is_opened
|
37
|
+
@reply_queue.delete
|
38
|
+
@ch.close
|
39
|
+
|
40
|
+
if @connection_started
|
41
|
+
@conn.close
|
42
|
+
@connection_started = false
|
43
|
+
end
|
44
|
+
|
45
|
+
@is_opened = false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def open?; @is_opened end
|
50
|
+
def read(sz); @inbuf.read sz end
|
51
|
+
def write(buf); @outbuf << Thrift::Bytes.force_binary_encoding(buf) end
|
52
|
+
|
53
|
+
#If blocking is set to true then wait for a response message in the reply_to queue, otherwise
|
54
|
+
#just send and go!
|
55
|
+
def flush(options={})
|
56
|
+
|
57
|
+
operation = options.has_key?(:operation) ? options[:operation] : ""
|
58
|
+
blocking = options.has_key?(:blocking) ? options[:blocking] : true
|
59
|
+
msg_timeout = options.has_key?(:msg_timeout) ? options[:msg_timeout] : 10
|
60
|
+
log_messages = options.has_key?(:log_messages) ? options[:log_messages] : true
|
61
|
+
|
62
|
+
correlation_id = self.generate_uuid
|
63
|
+
|
64
|
+
headers = {:service_name => @service_queue_name,
|
65
|
+
:operation => operation,
|
66
|
+
:response_required => blocking, #Tell the receiver if a response is required
|
67
|
+
:from_name => @from_name
|
68
|
+
}
|
69
|
+
|
70
|
+
#Publish the message
|
71
|
+
print_log "Publishing message reply-to: #{@reply_queue.name} - headers: #{headers}", correlation_id if log_messages
|
72
|
+
start_time = Time.now
|
73
|
+
@service_exchange.publish(@outbuf,
|
74
|
+
:routing_key => @service_queue_name,
|
75
|
+
:correlation_id => correlation_id,
|
76
|
+
:expiration => msg_timeout,
|
77
|
+
:reply_to => @reply_queue.name,
|
78
|
+
:headers => headers)
|
79
|
+
|
80
|
+
#If this is a standard RPC blocking call, then wait for there to be a response from the
|
81
|
+
#service provider or timeout and log the timeout
|
82
|
+
if blocking
|
83
|
+
@response = ""
|
84
|
+
begin
|
85
|
+
#Adding 1sec to timeout to account for clock differences
|
86
|
+
Timeout.timeout(msg_timeout + 1, ResponseTimeout) do
|
87
|
+
@reply_queue.subscribe(:block => true) do |delivery_info, properties, payload|
|
88
|
+
|
89
|
+
if log_messages
|
90
|
+
response_time = Time.now - start_time
|
91
|
+
print_log "---- Response Message received in #{response_time}sec for #{@reply_queue.name}", correlation_id
|
92
|
+
print_log "HEADERS: #{properties}", correlation_id
|
93
|
+
end
|
94
|
+
|
95
|
+
if properties[:correlation_id] == correlation_id
|
96
|
+
@response = payload
|
97
|
+
|
98
|
+
#once the return message has been received, no need to continue a subscription
|
99
|
+
delivery_info.consumer.cancel
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
rescue ResponseTimeout => ex
|
104
|
+
#Trying to work around weirdness being seen in a multi threaded workflow environment
|
105
|
+
if @response == ""
|
106
|
+
msg = "A timeout has occurred (#{msg_timeout}sec) trying to call #{@service_queue_name}.#{operation}"
|
107
|
+
print_log msg, correlation_id
|
108
|
+
raise ex, msg
|
109
|
+
else
|
110
|
+
print_log "Ignoring timeout - #{@response}", correlation_id
|
111
|
+
end
|
112
|
+
end
|
113
|
+
@inbuf = StringIO.new Thrift::Bytes.force_binary_encoding(@response)
|
114
|
+
end
|
115
|
+
@outbuf = Thrift::Bytes.empty_byte_buffer
|
116
|
+
end
|
117
|
+
|
118
|
+
protected
|
119
|
+
|
120
|
+
def generate_uuid
|
121
|
+
UUIDTools::UUID.timestamp_create.to_s
|
122
|
+
end
|
123
|
+
|
124
|
+
def print_log(message="", correlation_id="")
|
125
|
+
puts "#{Time.now.utc} C Thread: #{Thread.current.object_id} CID:#{correlation_id} - #{message}"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
|
3
|
+
module ThriftyBunny
|
4
|
+
class Config
|
5
|
+
attr_reader :host, :port, :vhost, :user, :password, :ssl, :queue, :exchange
|
6
|
+
|
7
|
+
def initialize(options={})
|
8
|
+
@host = options[:host] || '127.0.0.1'
|
9
|
+
@port = options[:port] || 5672
|
10
|
+
|
11
|
+
@vhost = options[:vhost] || "/"
|
12
|
+
@user = options[:user] || "guest"
|
13
|
+
@password = options[:password] || "guest"
|
14
|
+
@ssl = options[:ssl] || false
|
15
|
+
|
16
|
+
@queue = options[:queue] || 'rpc_queue'
|
17
|
+
@exchange = options[:exchange] || 'rpc_exchange'
|
18
|
+
end
|
19
|
+
|
20
|
+
def bunny_config
|
21
|
+
{
|
22
|
+
host: host, port: port, vhost: vhost,
|
23
|
+
user: user, password: password, ssl: ssl
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'thrift'
|
2
|
+
|
3
|
+
module ThriftyBunny
|
4
|
+
|
5
|
+
class RpcServer < ::Thrift::BaseServer
|
6
|
+
|
7
|
+
class ProcessingTimeout < Timeout::Error; end
|
8
|
+
|
9
|
+
def initialize(processor, config=Config.new, options={})
|
10
|
+
|
11
|
+
@processor = processor
|
12
|
+
|
13
|
+
if options[:connection].nil?
|
14
|
+
@conn = Bunny.new(config.bunny_config)
|
15
|
+
@conn.start
|
16
|
+
else
|
17
|
+
@conn = options[:connection]
|
18
|
+
end
|
19
|
+
|
20
|
+
@queue_name = config.queue
|
21
|
+
@protocol_factory = options[:protocol_factory] || Thrift::BinaryProtocolFactory
|
22
|
+
@exchange = config.exchange
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def close
|
27
|
+
|
28
|
+
if not @request_channel.nil? and @request_channel.respond_to?('close')
|
29
|
+
@request_channel.close
|
30
|
+
end
|
31
|
+
|
32
|
+
#Always close the broker connection when closing the server
|
33
|
+
@conn.close
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def serve(options={})
|
38
|
+
log_messages = options[:log_messages] || false
|
39
|
+
max_messages = options[:max_messages].nil? ? 10 : options[:max_messages]
|
40
|
+
response_timeout = options[:response_timeout] || 10
|
41
|
+
|
42
|
+
#Create a channel to the service queue
|
43
|
+
@request_channel = @conn.create_channel(nil, max_messages )
|
44
|
+
|
45
|
+
if @exchange.nil?
|
46
|
+
@service_exchange = @request_channel.default_exchange
|
47
|
+
@request_queue = @request_channel.queue(@queue_name, :auto_delete => true)
|
48
|
+
else
|
49
|
+
@service_exchange = @request_channel.direct(@exchange,:durable => true)
|
50
|
+
@request_queue = @request_channel.queue(@queue_name, :auto_delete => true).bind(@service_exchange, :routing_key => @queue_name)
|
51
|
+
end
|
52
|
+
|
53
|
+
@request_queue.subscribe(:block => true) do |delivery_info, properties, payload|
|
54
|
+
|
55
|
+
if log_messages
|
56
|
+
Thread.current["correlation_id"] = properties.correlation_id
|
57
|
+
print_log "---- Message received ----"
|
58
|
+
print_log "HEADERS: #{properties}"
|
59
|
+
end
|
60
|
+
|
61
|
+
Thread.current["correlation_id"] = properties.correlation_id
|
62
|
+
|
63
|
+
response_channel = @conn.create_channel
|
64
|
+
response_exchange = response_channel.default_exchange
|
65
|
+
|
66
|
+
response_required = properties.headers.has_key?('response_required') ? properties.headers['response_required'] : true
|
67
|
+
process_timeout = response_timeout.to_i > properties.expiration.to_i ? response_timeout.to_i : properties.expiration.to_i
|
68
|
+
|
69
|
+
#Binary content will imply thrift based message payload
|
70
|
+
if properties.content_type == 'application/octet-stream'
|
71
|
+
|
72
|
+
print_log "Request to process #{@queue_name}.#{properties.headers['operation']} in #{process_timeout}sec" if log_messages
|
73
|
+
|
74
|
+
input = StringIO.new payload
|
75
|
+
out = StringIO.new
|
76
|
+
transport = Thrift::IOStreamTransport.new input, out
|
77
|
+
protocol = @protocol_factory.new.get_protocol transport
|
78
|
+
|
79
|
+
begin
|
80
|
+
start_time = Time.now
|
81
|
+
Timeout.timeout(process_timeout, ProcessingTimeout) do
|
82
|
+
@processor.process protocol, protocol
|
83
|
+
end
|
84
|
+
processing_time = Time.now - start_time
|
85
|
+
|
86
|
+
#rewind the buffer for reading
|
87
|
+
if out.length > 0
|
88
|
+
out.rewind
|
89
|
+
|
90
|
+
print_log "Time to process request: #{processing_time}sec Response length: #{out.length}" if log_messages
|
91
|
+
|
92
|
+
if response_required
|
93
|
+
response_exchange.publish(out.read(out.length),
|
94
|
+
:routing_key => properties.reply_to,
|
95
|
+
:correlation_id => properties.correlation_id,
|
96
|
+
:content_type => 'application/octet-stream' )
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
rescue ProcessingTimeout => ex
|
101
|
+
print_log "A timeout has occurred (#{process_timeout}sec) trying to call #{@queue_name}.#{properties.headers['operation']}"
|
102
|
+
end
|
103
|
+
|
104
|
+
else
|
105
|
+
|
106
|
+
print_log "Unable to process message content of type #{properties.content_type}. The message will be rejected"
|
107
|
+
@request_channel.reject(delivery_info.delivery_tag, false)
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
response_channel.close
|
112
|
+
|
113
|
+
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def print_log(message="")
|
120
|
+
puts "#{Time.now.utc} S Thread: #{Thread.current.object_id} CID:#{Thread.current["correlation_id"]} - #{message}"
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'thrifty_bunny/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "thrifty-bunny"
|
8
|
+
spec.version = ThriftyBunny::VERSION
|
9
|
+
spec.authors = ["Bill Siggelkow"]
|
10
|
+
spec.email = ["bsiggelkow@me.com"]
|
11
|
+
spec.summary = "RabbitMQ adapter for Apache Thrift"
|
12
|
+
spec.description = "RabbitMQ adapter for Apache Thrift"
|
13
|
+
spec.homepage = "http://github.com/bsiggelkow/thrifty-bunny"
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'pry'
|
25
|
+
|
26
|
+
spec.add_dependency 'thrift'
|
27
|
+
spec.add_dependency 'thin'
|
28
|
+
spec.add_dependency 'bunny'
|
29
|
+
spec.add_dependency 'uuidtools'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thrifty-bunny
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bill Siggelkow
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-30 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: thrift
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: thin
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: bunny
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: uuidtools
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: RabbitMQ adapter for Apache Thrift
|
126
|
+
email:
|
127
|
+
- bsiggelkow@me.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- Gemfile
|
134
|
+
- LICENSE.txt
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- examples/bin/client.start
|
138
|
+
- examples/bin/server.start
|
139
|
+
- examples/hello_thrift/client.rb
|
140
|
+
- examples/hello_thrift/hello_service/hello_service.rb
|
141
|
+
- examples/hello_thrift/hello_service/hello_service_constants.rb
|
142
|
+
- examples/hello_thrift/hello_service/hello_service_types.rb
|
143
|
+
- examples/hello_thrift/server.rb
|
144
|
+
- examples/hello_thrift/version.rb
|
145
|
+
- lib/thrifty_bunny.rb
|
146
|
+
- lib/thrifty_bunny/client_transport.rb
|
147
|
+
- lib/thrifty_bunny/config.rb
|
148
|
+
- lib/thrifty_bunny/rpc_server.rb
|
149
|
+
- lib/thrifty_bunny/version.rb
|
150
|
+
- spec/integration/client_spec.rb
|
151
|
+
- spec/spec_helper.rb
|
152
|
+
- spec/unit/client_transport_spec.rb
|
153
|
+
- spec/unit/rpc_server_spec.rb
|
154
|
+
- thrifty-bunny.gemspec
|
155
|
+
homepage: http://github.com/bsiggelkow/thrifty-bunny
|
156
|
+
licenses:
|
157
|
+
- MIT
|
158
|
+
metadata: {}
|
159
|
+
post_install_message:
|
160
|
+
rdoc_options: []
|
161
|
+
require_paths:
|
162
|
+
- lib
|
163
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
requirements: []
|
174
|
+
rubyforge_project:
|
175
|
+
rubygems_version: 2.2.2
|
176
|
+
signing_key:
|
177
|
+
specification_version: 4
|
178
|
+
summary: RabbitMQ adapter for Apache Thrift
|
179
|
+
test_files:
|
180
|
+
- spec/integration/client_spec.rb
|
181
|
+
- spec/spec_helper.rb
|
182
|
+
- spec/unit/client_transport_spec.rb
|
183
|
+
- spec/unit/rpc_server_spec.rb
|