fl-thrift_client 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +33 -0
- data/LICENSE +202 -0
- data/Manifest +23 -0
- data/README.rdoc +50 -0
- data/Rakefile +16 -0
- data/lib/thrift_client/abstract_thrift_client.rb +217 -0
- data/lib/thrift_client/connection/base.rb +19 -0
- data/lib/thrift_client/connection/factory.rb +11 -0
- data/lib/thrift_client/connection/http.rb +18 -0
- data/lib/thrift_client/connection/socket.rb +22 -0
- data/lib/thrift_client/connection.rb +5 -0
- data/lib/thrift_client/event_machine.rb +145 -0
- data/lib/thrift_client/simple.rb +263 -0
- data/lib/thrift_client/thrift.rb +21 -0
- data/lib/thrift_client.rb +45 -0
- data/test/greeter/greeter.rb +77 -0
- data/test/greeter/greeter.thrift +3 -0
- data/test/greeter/server.rb +40 -0
- data/test/multiple_working_servers_test.rb +112 -0
- data/test/simple_test.rb +137 -0
- data/test/test_helper.rb +12 -0
- data/test/thrift_client_http_test.rb +54 -0
- data/test/thrift_client_test.rb +166 -0
- data/thrift_client.gemspec +26 -0
- metadata +112 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
v0.4.2 Allow per-method overrides of retries. Fix several bugs with EventMachine.
|
2
|
+
|
3
|
+
v0.4.1 Making ThriftClient decoratable. Able to add new functionality a class definition.
|
4
|
+
|
5
|
+
v0.4.0. Add new EventMachine transport. This requires two layers of transport
|
6
|
+
configurability:
|
7
|
+
options[:transport] for EventMachine or Socket transports
|
8
|
+
options[:transport_wrapper] for optional Buffered or Framed Transport.
|
9
|
+
Clients will need to update their options to ensure they don't conflict with this change. (mperham)
|
10
|
+
Revert global timeouts. (ryanking)
|
11
|
+
Add support for HTTPClientTransport (Chris Sepic)
|
12
|
+
|
13
|
+
v0.3.3. Allow for a timeout over all requests in a call.
|
14
|
+
|
15
|
+
v0.3.2. Fix connection close bug (nkallen, mattknox).
|
16
|
+
|
17
|
+
v0.3.1. Add ability to reset connection after N requests.
|
18
|
+
|
19
|
+
v0.3. Change default timeout semantics; hash default was too sneaky. Fix bug.
|
20
|
+
|
21
|
+
v0.2.2. Fix connect bug.
|
22
|
+
|
23
|
+
v0.2.1. Don't turn off strict_read by default; allow override.
|
24
|
+
|
25
|
+
v0.2. Add ThriftClient::Simple (Robey).
|
26
|
+
|
27
|
+
v0.1.3. Define methods explicitly.
|
28
|
+
|
29
|
+
v0.1.2. Support default responses.
|
30
|
+
|
31
|
+
v0.1.1. Support server_retry_period.
|
32
|
+
|
33
|
+
v0.1. First release.
|
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/Manifest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
LICENSE
|
3
|
+
Manifest
|
4
|
+
README.rdoc
|
5
|
+
Rakefile
|
6
|
+
lib/thrift_client.rb
|
7
|
+
lib/thrift_client/abstract_thrift_client.rb
|
8
|
+
lib/thrift_client/connection.rb
|
9
|
+
lib/thrift_client/connection/base.rb
|
10
|
+
lib/thrift_client/connection/factory.rb
|
11
|
+
lib/thrift_client/connection/http.rb
|
12
|
+
lib/thrift_client/connection/socket.rb
|
13
|
+
lib/thrift_client/event_machine.rb
|
14
|
+
lib/thrift_client/simple.rb
|
15
|
+
lib/thrift_client/thrift.rb
|
16
|
+
test/greeter/greeter.rb
|
17
|
+
test/greeter/greeter.thrift
|
18
|
+
test/greeter/server.rb
|
19
|
+
test/multiple_working_servers_test.rb
|
20
|
+
test/simple_test.rb
|
21
|
+
test/test_helper.rb
|
22
|
+
test/thrift_client_http_test.rb
|
23
|
+
test/thrift_client_test.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
thrift_client
|
3
|
+
|
4
|
+
A Thrift client wrapper that encapsulates some common failover behavior.
|
5
|
+
|
6
|
+
== License
|
7
|
+
|
8
|
+
Copyright 2009 Twitter, Inc. See included LICENSE file.
|
9
|
+
|
10
|
+
The public certificate for this gem is here[http://blog.evanweaver.com/files/evan_weaver-original-public_cert.pem].
|
11
|
+
|
12
|
+
== Features
|
13
|
+
|
14
|
+
* Transparent connection management
|
15
|
+
* Configurable failover and retry backoff
|
16
|
+
* Ruby 1.9 compatibility
|
17
|
+
* ThriftClient::Simple class, for working without generated bindings.
|
18
|
+
|
19
|
+
The Github source repository is {here}[http://github.com/fauna/thrift_client/]. Patches and contributions are very welcome.
|
20
|
+
|
21
|
+
== Usage
|
22
|
+
|
23
|
+
Instantiate a client:
|
24
|
+
|
25
|
+
client = ThriftClient.new(CassandraRb::Client, '127.0.0.1:9160', :retries => 2)
|
26
|
+
|
27
|
+
You can then make calls to the server via the <tt>client</tt> instance as if was your internal Thrift client. The connection will be opened lazily and methods will be proxied through.
|
28
|
+
|
29
|
+
client.get_string_list_property("keyspaces")
|
30
|
+
|
31
|
+
On failures, the client will try the remaining servers in the list before giving up. See ThriftClient for more.
|
32
|
+
|
33
|
+
== Installation
|
34
|
+
|
35
|
+
You need Ruby 1.8 or 1.9. If you have those, just run:
|
36
|
+
|
37
|
+
sudo gem install thrift_client
|
38
|
+
|
39
|
+
== Contributing
|
40
|
+
|
41
|
+
To contribute changes:
|
42
|
+
|
43
|
+
1. Fork the project
|
44
|
+
2. make your change, adding tests
|
45
|
+
3. send a pull request to fauna and ryanking
|
46
|
+
|
47
|
+
== Reporting problems
|
48
|
+
|
49
|
+
The Github issue tracker is {here}[http://github.com/fauna/thrift_client/issues].
|
50
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new("thrift_client") do |p|
|
6
|
+
p.author = "Evan Weaver"
|
7
|
+
p.project = "fauna"
|
8
|
+
p.summary = "A Thrift client wrapper that encapsulates some common failover behavior."
|
9
|
+
p.rubygems_version = ">= 0.8"
|
10
|
+
p.dependencies = ['thrift']
|
11
|
+
p.ignore_pattern = /^(vendor\/thrift)/
|
12
|
+
p.rdoc_pattern = /^(lib|bin|tasks|ext)|^README|^CHANGELOG|^TODO|^LICENSE|^COPYING$/
|
13
|
+
p.url = "http://blog.evanweaver.com/files/doc/fauna/thrift_client/"
|
14
|
+
p.docs_host = "blog.evanweaver.com:~/www/bax/public/files/doc/"
|
15
|
+
p.spec_pattern = "spec/*_spec.rb"
|
16
|
+
end
|
@@ -0,0 +1,217 @@
|
|
1
|
+
class AbstractThriftClient
|
2
|
+
|
3
|
+
DEFAULTS = {
|
4
|
+
:protocol => Thrift::BinaryProtocol,
|
5
|
+
:protocol_extra_params => [],
|
6
|
+
:transport => Thrift::Socket,
|
7
|
+
:transport_wrapper => Thrift::FramedTransport,
|
8
|
+
:raise => true,
|
9
|
+
:defaults => {}
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
attr_reader :client, :client_class, :current_server, :server_list, :options, :client_methods
|
13
|
+
|
14
|
+
def initialize(client_class, servers, options = {})
|
15
|
+
@options = DEFAULTS.merge(options)
|
16
|
+
@client_class = client_class
|
17
|
+
@server_list = Array(servers)
|
18
|
+
@current_server = @server_list.first
|
19
|
+
|
20
|
+
@client_methods = []
|
21
|
+
@client_class.instance_methods.each do |method_name|
|
22
|
+
if method_name =~ /^recv_(.*)$/
|
23
|
+
instance_eval("def #{$1}(*args); handled_proxy(:'#{$1}', *args); end", __FILE__, __LINE__)
|
24
|
+
@client_methods << $1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def inspect
|
30
|
+
"<#{self.class}(#{client_class}) @current_server=#{@current_server}>"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Force the client to connect to the server. Not necessary to be
|
34
|
+
# called as the connection will be made on the first RPC method
|
35
|
+
# call.
|
36
|
+
def connect!
|
37
|
+
@connection = Connection::Factory.create(@options[:transport], @options[:transport_wrapper], @current_server, @options[:timeout])
|
38
|
+
@connection.connect!
|
39
|
+
@client = @client_class.new(@options[:protocol].new(@connection.transport, *@options[:protocol_extra_params]))
|
40
|
+
rescue Thrift::TransportException, Errno::ECONNREFUSED => e
|
41
|
+
@transport.close rescue nil
|
42
|
+
raise e
|
43
|
+
end
|
44
|
+
|
45
|
+
def disconnect!
|
46
|
+
@connection.close rescue nil
|
47
|
+
@client = nil
|
48
|
+
@current_server = nil
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
def handled_proxy(method_name, *args)
|
53
|
+
proxy(method_name, *args)
|
54
|
+
rescue Exception => e
|
55
|
+
handle_exception(e, method_name, args)
|
56
|
+
end
|
57
|
+
|
58
|
+
def proxy(method_name, *args)
|
59
|
+
connect! unless @client
|
60
|
+
send_rpc(method_name, *args)
|
61
|
+
end
|
62
|
+
|
63
|
+
def send_rpc(method_name, *args)
|
64
|
+
@client.send(method_name, *args)
|
65
|
+
end
|
66
|
+
|
67
|
+
def disconnect_on_error!
|
68
|
+
@connection.close rescue nil
|
69
|
+
@client = nil
|
70
|
+
@current_server = nil
|
71
|
+
end
|
72
|
+
|
73
|
+
def handle_exception(e, method_name, args=nil)
|
74
|
+
raise e if @options[:raise]
|
75
|
+
@options[:defaults][method_name.to_sym]
|
76
|
+
end
|
77
|
+
|
78
|
+
module RetryingThriftClient
|
79
|
+
DISCONNECT_ERRORS = [
|
80
|
+
IOError,
|
81
|
+
Thrift::Exception,
|
82
|
+
Thrift::ProtocolException,
|
83
|
+
Thrift::ApplicationException,
|
84
|
+
Thrift::TransportException
|
85
|
+
].freeze
|
86
|
+
|
87
|
+
RETRYING_DEFAULTS = {
|
88
|
+
:exception_classes => DISCONNECT_ERRORS,
|
89
|
+
:randomize_server_list => true,
|
90
|
+
:retries => nil,
|
91
|
+
:server_retry_period => 1,
|
92
|
+
:server_max_requests => nil,
|
93
|
+
:retry_overrides => {}
|
94
|
+
}.freeze
|
95
|
+
|
96
|
+
def initialize(client_class, servers, options = {})
|
97
|
+
super
|
98
|
+
@options = RETRYING_DEFAULTS.merge(@options) # @options is set by super
|
99
|
+
@retries = options[:retries] || @server_list.size
|
100
|
+
@request_count = 0
|
101
|
+
@max_requests = @options[:server_max_requests]
|
102
|
+
@retry_period = @options[:server_retry_period]
|
103
|
+
rebuild_live_server_list!
|
104
|
+
end
|
105
|
+
|
106
|
+
def connect!
|
107
|
+
@current_server = next_server
|
108
|
+
super
|
109
|
+
rescue Thrift::TransportException, Errno::ECONNREFUSED
|
110
|
+
retry
|
111
|
+
end
|
112
|
+
|
113
|
+
def disconnect!
|
114
|
+
# Keep live servers in the list if we have a retry period. Otherwise,
|
115
|
+
# always eject, because we will always re-add them.
|
116
|
+
if @retry_period && @current_server
|
117
|
+
@live_server_list.unshift(@current_server)
|
118
|
+
end
|
119
|
+
|
120
|
+
super()
|
121
|
+
@request_count = 0
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def next_server
|
127
|
+
if @retry_period
|
128
|
+
rebuild_live_server_list! if Time.now > @last_rebuild + @retry_period
|
129
|
+
raise ThriftClient::NoServersAvailable, "No live servers in #{@server_list.inspect} since #{@last_rebuild.inspect}." if @live_server_list.empty?
|
130
|
+
elsif @live_server_list.empty?
|
131
|
+
rebuild_live_server_list!
|
132
|
+
end
|
133
|
+
@live_server_list.pop
|
134
|
+
end
|
135
|
+
|
136
|
+
def rebuild_live_server_list!
|
137
|
+
@last_rebuild = Time.now
|
138
|
+
if @options[:randomize_server_list]
|
139
|
+
@live_server_list = @server_list.sort_by { rand }
|
140
|
+
else
|
141
|
+
@live_server_list = @server_list.dup
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def handled_proxy(method_name, *args)
|
146
|
+
disconnect_on_max! if @max_requests and @request_count >= @max_requests
|
147
|
+
super
|
148
|
+
end
|
149
|
+
|
150
|
+
def proxy(method_name, *args)
|
151
|
+
super
|
152
|
+
rescue *@options[:exception_classes] => e
|
153
|
+
disconnect_on_error!
|
154
|
+
tries ||= (@options[:retry_overrides][method_name.to_sym] || @retries)
|
155
|
+
tries -= 1
|
156
|
+
tries == 0 ? handle_exception(e, method_name, args) : retry
|
157
|
+
end
|
158
|
+
|
159
|
+
def send_rpc(method_name, *args)
|
160
|
+
@request_count += 1
|
161
|
+
super
|
162
|
+
end
|
163
|
+
|
164
|
+
def disconnect_on_max!
|
165
|
+
@live_server_list.push(@current_server)
|
166
|
+
disconnect_on_error!
|
167
|
+
end
|
168
|
+
|
169
|
+
def disconnect_on_error!
|
170
|
+
super
|
171
|
+
@request_count = 0
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
module TimingOutThriftClient
|
177
|
+
TIMINGOUT_DEFAULTS = {
|
178
|
+
:timeout => 1,
|
179
|
+
:timeout_overrides => {}
|
180
|
+
}.freeze
|
181
|
+
|
182
|
+
def initialize(client_class, servers, options = {})
|
183
|
+
super
|
184
|
+
@options = TIMINGOUT_DEFAULTS.merge(@options)
|
185
|
+
end
|
186
|
+
|
187
|
+
def connect!
|
188
|
+
super
|
189
|
+
set_method_timeouts!
|
190
|
+
end
|
191
|
+
|
192
|
+
private
|
193
|
+
def set_method_timeouts!
|
194
|
+
return unless has_timeouts?
|
195
|
+
@client_methods.each do |method_name|
|
196
|
+
@client.timeout = @options[:timeout_overrides][method_name.to_sym] || @options[:timeout]
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def has_timeouts?
|
201
|
+
@has_timeouts ||= has_timeouts!
|
202
|
+
end
|
203
|
+
|
204
|
+
def has_timeouts!
|
205
|
+
@options[:timeout_overrides].any? && transport_can_timeout?
|
206
|
+
end
|
207
|
+
|
208
|
+
def transport_can_timeout?
|
209
|
+
if (@options[:transport_wrapper] || @options[:transport]).method_defined?(:timeout=)
|
210
|
+
true
|
211
|
+
else
|
212
|
+
warn "ThriftClient: Timeout overrides have no effect with with transport type #{(@options[:transport_wrapper] || @options[:transport])}"
|
213
|
+
false
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Connection
|
2
|
+
class Base
|
3
|
+
attr_accessor :transport, :server
|
4
|
+
|
5
|
+
def initialize(transport, transport_wrapper, server, timeout)
|
6
|
+
@transport = transport
|
7
|
+
@transport_wrapper = transport_wrapper
|
8
|
+
@server = server
|
9
|
+
@timeout = timeout
|
10
|
+
end
|
11
|
+
|
12
|
+
def connect!
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
def close
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Connection
|
2
|
+
class Factory
|
3
|
+
def self.create(transport, transport_wrapper, server, timeout)
|
4
|
+
if transport == Thrift::HTTPClientTransport
|
5
|
+
Connection::HTTP.new(transport, transport_wrapper, server, timeout)
|
6
|
+
else
|
7
|
+
Connection::Socket.new(transport, transport_wrapper, server, timeout)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Connection
|
2
|
+
class HTTP < Base
|
3
|
+
def connect!
|
4
|
+
uri = parse_server(@server)
|
5
|
+
@transport = Thrift::HTTPClientTransport.new(@server)
|
6
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
7
|
+
http.use_ssl = uri.scheme == "https"
|
8
|
+
http.get(uri.path)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def parse_server(server)
|
13
|
+
uri = URI.parse(server)
|
14
|
+
raise ArgumentError, 'Servers must start with http' unless uri.scheme =~ /^http/
|
15
|
+
uri
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Connection
|
2
|
+
class Socket < Base
|
3
|
+
def close
|
4
|
+
@transport.close
|
5
|
+
end
|
6
|
+
|
7
|
+
def connect!
|
8
|
+
host, port = parse_server(@server)
|
9
|
+
@transport = @transport.new(*[host, port.to_i, @timeout])
|
10
|
+
@transport = @transport_wrapper.new(@transport) if @transport_wrapper
|
11
|
+
@transport.open
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def parse_server(server)
|
17
|
+
host, port = server.to_s.split(":")
|
18
|
+
raise ArgumentError, 'Servers must be in the form "host:port"' unless host and port
|
19
|
+
[host, port]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|