ione-rpc 1.0.1 → 1.1.0.pre0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ione/rpc/server.rb +28 -11
- data/lib/ione/rpc/version.rb +1 -1
- data/spec/ione/rpc/server_spec.rb +57 -12
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b76720f1019ef558e761ca6150d3e6417525f7e4
|
4
|
+
data.tar.gz: e0e60b176ac1d29de164124a9db8090be4224e2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 843c6352df3a73996b9df2c110e4c9c259f85fa6dec03cda53619f69af220ba519aa17a11ef583aa111523a6dbe3e8fa09bfd987144dc7e3ee4b3809777b5afa
|
7
|
+
data.tar.gz: 1ecb1ef66d1f4f17ca14dccc216cc21a9ecaec02eee64fe1bed4bf32229f44dd56616df562fc0af64fdd09ab79314b6a651cc5a8d1d21f431e5ac12b406c6dd6
|
data/lib/ione/rpc/server.rb
CHANGED
@@ -52,7 +52,9 @@ module Ione
|
|
52
52
|
|
53
53
|
# Override this method to handle errors raised or returned by
|
54
54
|
# {#handle_request} or any other part of the request handling (for example
|
55
|
-
# the response encoding).
|
55
|
+
# the response encoding). In the event that {#handle_request} completed
|
56
|
+
# successfully, the response will contain that result, otherwise it will
|
57
|
+
# be nil.
|
56
58
|
#
|
57
59
|
# When this method raises an error or returns a failed future there will be
|
58
60
|
# no response for the request. Unless you use a custom client this means
|
@@ -65,9 +67,12 @@ module Ione
|
|
65
67
|
# with the error message and class. The full backtrace will also be logged
|
66
68
|
# at the `debug` level.
|
67
69
|
#
|
70
|
+
# To remain backwards compatible, the method can be overridden to take only
|
71
|
+
# three arguments, in which case the response is omitted.
|
72
|
+
#
|
68
73
|
# @return [Ione::Future<Object>] a future that will resolve to an alternate
|
69
74
|
# response for this request.
|
70
|
-
def handle_error(error, request, connection)
|
75
|
+
def handle_error(error, request, response=nil, connection)
|
71
76
|
Ione::Future.failed(error)
|
72
77
|
end
|
73
78
|
|
@@ -90,8 +95,12 @@ module Ione
|
|
90
95
|
end
|
91
96
|
|
92
97
|
# @private
|
93
|
-
def guarded_handle_error(error, request, connection)
|
94
|
-
handle_error
|
98
|
+
def guarded_handle_error(error, request, response, connection)
|
99
|
+
if method(:handle_error).arity == 3
|
100
|
+
handle_error(error, request, connection)
|
101
|
+
else
|
102
|
+
handle_error(error, request, response, connection)
|
103
|
+
end
|
95
104
|
rescue => e
|
96
105
|
Ione::Future.failed(e)
|
97
106
|
end
|
@@ -112,8 +121,14 @@ module Ione
|
|
112
121
|
@acceptor.on_accept do |connection|
|
113
122
|
@logger.info('Connection from %s:%d accepted' % [connection.host, connection.port]) if @logger
|
114
123
|
peer = ServerPeer.new(connection, @codec, self, @logger)
|
115
|
-
|
116
|
-
|
124
|
+
if @logger
|
125
|
+
peer.on_closed do |error|
|
126
|
+
if error
|
127
|
+
@logger.warn(sprintf('Connection from %s:%d closed unexpectedly: %s (%s)', connection.host, connection.port, error.message, error.class.name))
|
128
|
+
else
|
129
|
+
@logger.info(sprintf('Connection from %s:%d closed', connection.host, connection.port))
|
130
|
+
end
|
131
|
+
end
|
117
132
|
end
|
118
133
|
handle_connection(peer)
|
119
134
|
end
|
@@ -134,17 +149,19 @@ module Ione
|
|
134
149
|
end
|
135
150
|
|
136
151
|
def send_response(f, message, channel, try_again)
|
137
|
-
|
152
|
+
response = nil
|
153
|
+
f = f.map do |r|
|
154
|
+
response = r
|
138
155
|
encoded_response = @codec.encode(response, channel)
|
139
156
|
@connection.write(encoded_response)
|
140
157
|
nil
|
141
158
|
end
|
142
|
-
f.
|
159
|
+
f.on_failure do |error|
|
143
160
|
if try_again
|
144
|
-
ff = @server.guarded_handle_error(error, message, self)
|
161
|
+
ff = @server.guarded_handle_error(error, message, response, self)
|
145
162
|
send_response(ff, message, channel, false)
|
146
|
-
|
147
|
-
@logger.error('Unhandled error
|
163
|
+
elsif @logger
|
164
|
+
@logger.error(sprintf('Unhandled error for %s:%d; %s (%s)', @connection.host, @connection.port, error.message, error.class.name))
|
148
165
|
error.backtrace && @logger.debug(error.backtrace.join("#{$/}\t"))
|
149
166
|
end
|
150
167
|
end
|
data/lib/ione/rpc/version.rb
CHANGED
@@ -141,6 +141,11 @@ module Ione
|
|
141
141
|
raw_connection.closed_listener.call
|
142
142
|
logger.should have_received(:info).with('Connection from client.example.com:34534 closed')
|
143
143
|
end
|
144
|
+
|
145
|
+
it 'logs when the client is disconnected unexpectedly' do
|
146
|
+
raw_connection.closed_listener.call(IOError.new('Boork'))
|
147
|
+
logger.should have_received(:warn).with('Connection from client.example.com:34534 closed unexpectedly: Boork (IOError)')
|
148
|
+
end
|
144
149
|
end
|
145
150
|
|
146
151
|
context 'when a client sends a request' do
|
@@ -197,16 +202,17 @@ module Ione
|
|
197
202
|
server.errors.should == [StandardError.new('Borkzor')]
|
198
203
|
end
|
199
204
|
|
200
|
-
it 'calls #handle_error with the error, the request and the connection' do
|
201
|
-
error, request, connection = nil, nil, nil
|
205
|
+
it 'calls #handle_error with the error, the request, no response, and the connection' do
|
206
|
+
error, request, response, connection = nil, nil, nil, nil
|
202
207
|
server.override_handle_request { Ione::Future.failed(StandardError.new('Borkzor')) }
|
203
|
-
server.override_handle_error { |e, r, c| error = e; request = r; connection = c }
|
208
|
+
server.override_handle_error { |e, q, r, c| error = e; request = q; response = r; connection = c }
|
204
209
|
peer = server.connections.first
|
205
210
|
peer.handle_message('FOOBAZ', 42)
|
206
211
|
error.should be_a(StandardError)
|
207
|
-
error.message.should
|
208
|
-
request.should
|
209
|
-
|
212
|
+
error.message.should eq('Borkzor')
|
213
|
+
request.should eq('FOOBAZ')
|
214
|
+
response.should be_nil
|
215
|
+
connection.should equal(peer)
|
210
216
|
end
|
211
217
|
|
212
218
|
it 'responds with the message returned by #handle_error' do
|
@@ -244,7 +250,7 @@ module Ione
|
|
244
250
|
server.override_handle_request { raise StandardError, 'Borkzor' }
|
245
251
|
peer = server.connections.first
|
246
252
|
peer.handle_message('FOOBAZ', 42)
|
247
|
-
logger.should have_received(:error).with(/^Unhandled error: Borkzor \(StandardError\)/i)
|
253
|
+
logger.should have_received(:error).with(/^Unhandled error for client.example.com:34534; Borkzor \(StandardError\)/i)
|
248
254
|
logger.should have_received(:debug).with(/\d+:in/)
|
249
255
|
end
|
250
256
|
|
@@ -253,7 +259,7 @@ module Ione
|
|
253
259
|
server.override_handle_error { |e| raise StandardError, "OH NOES: #{e.message}" }
|
254
260
|
peer = server.connections.first
|
255
261
|
peer.handle_message('FOOBAZ', 42)
|
256
|
-
logger.should have_received(:error).with(/^Unhandled error: OH NOES: Borkzor \(StandardError\)/i)
|
262
|
+
logger.should have_received(:error).with(/^Unhandled error for client.example.com:34534; OH NOES: Borkzor \(StandardError\)/i)
|
257
263
|
end
|
258
264
|
|
259
265
|
it 'logs when #handle_error returns a failed future' do
|
@@ -261,7 +267,7 @@ module Ione
|
|
261
267
|
server.override_handle_error { |e| Ione::Future.failed(StandardError.new("OH NOES: #{e.message}")) }
|
262
268
|
peer = server.connections.first
|
263
269
|
peer.handle_message('FOOBAZ', 42)
|
264
|
-
logger.should have_received(:error).with(/^Unhandled error: OH NOES: Borkzor \(StandardError\)/i)
|
270
|
+
logger.should have_received(:error).with(/^Unhandled error for client.example.com:34534; OH NOES: Borkzor \(StandardError\)/i)
|
265
271
|
end
|
266
272
|
|
267
273
|
context 'when the codec fails to encode the response' do
|
@@ -274,6 +280,20 @@ module Ione
|
|
274
280
|
server.errors.first.message.should == 'Borkzor'
|
275
281
|
end
|
276
282
|
|
283
|
+
it 'calls #handle_error with the error, the request, the response, and the connection' do
|
284
|
+
error, request, response, connection = nil, nil, nil, nil
|
285
|
+
server.override_handle_request { Ione::Future.resolved('OK') }
|
286
|
+
server.override_handle_error { |e, q, r, c| error = e; request = q; response = r; connection = c }
|
287
|
+
codec.stub(:encode).with('OK', 42).and_raise(CodecError.new('Borkzor'))
|
288
|
+
peer = server.connections.first
|
289
|
+
peer.handle_message('FOOBAZ', 42)
|
290
|
+
error.should be_a(CodecError)
|
291
|
+
error.message.should eq('Borkzor')
|
292
|
+
request.should eq('FOOBAZ')
|
293
|
+
response.should eq('OK')
|
294
|
+
connection.should equal(peer)
|
295
|
+
end
|
296
|
+
|
277
297
|
it 'responds with what #handle_error responds with' do
|
278
298
|
server.override_handle_request { Ione::Future.resolved('OK') }
|
279
299
|
server.override_handle_error { |e| Ione::Future.resolved("ERROR: #{e.message}") }
|
@@ -301,7 +321,26 @@ module Ione
|
|
301
321
|
codec.stub(:encode).with('ERROR: Borkzor', 42).and_raise(CodecError.new('Buzzfuzz'))
|
302
322
|
peer = server.connections.first
|
303
323
|
peer.handle_message('FOOBAZ', 42)
|
304
|
-
logger.should have_received(:error).with(/^Unhandled error: Buzzfuzz \(Ione::Rpc::CodecError\)/i)
|
324
|
+
logger.should have_received(:error).with(/^Unhandled error for client.example.com:34534; Buzzfuzz \(Ione::Rpc::CodecError\)/i)
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
context 'with a legacy server subclass' do
|
329
|
+
let :server do
|
330
|
+
ServerSpec::LegacyTestServer.new(4321, codec, io_reactor: io_reactor, logger: logger)
|
331
|
+
end
|
332
|
+
|
333
|
+
it 'calls #handle_error with the error, the request and the connection for legacy servers' do
|
334
|
+
error, request, response, connection = nil, nil, nil, nil
|
335
|
+
server.override_handle_request { Ione::Future.failed(StandardError.new('Borkzor')) }
|
336
|
+
server.override_handle_error { |e, q, r, c| error = e; request = q; response = r; connection = c }
|
337
|
+
peer = server.connections.first
|
338
|
+
peer.handle_message('FOOBAZ', 42)
|
339
|
+
error.should be_a(StandardError)
|
340
|
+
error.message.should eq('Borkzor')
|
341
|
+
request.should eq('FOOBAZ')
|
342
|
+
response.should eq(:legacy)
|
343
|
+
connection.should equal(peer)
|
305
344
|
end
|
306
345
|
end
|
307
346
|
end
|
@@ -333,10 +372,10 @@ module ServerSpec
|
|
333
372
|
@request_handler = handler
|
334
373
|
end
|
335
374
|
|
336
|
-
def handle_error(error, request, connection)
|
375
|
+
def handle_error(error, request, response, connection)
|
337
376
|
@errors << error
|
338
377
|
if @error_handler
|
339
|
-
@error_handler.call(error, request, connection)
|
378
|
+
@error_handler.call(error, request, response, connection)
|
340
379
|
else
|
341
380
|
super
|
342
381
|
end
|
@@ -351,4 +390,10 @@ module ServerSpec
|
|
351
390
|
end
|
352
391
|
end
|
353
392
|
end
|
393
|
+
|
394
|
+
class LegacyTestServer < TestServer
|
395
|
+
def handle_error(error, request, connection)
|
396
|
+
super(error, request, :legacy, connection)
|
397
|
+
end
|
398
|
+
end
|
354
399
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ione-rpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.1.0.pre0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Theo Hultberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ione
|
@@ -67,9 +67,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
67
|
version: 1.9.3
|
68
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
69
|
requirements:
|
70
|
-
- - '
|
70
|
+
- - '>'
|
71
71
|
- !ruby/object:Gem::Version
|
72
|
-
version:
|
72
|
+
version: 1.3.1
|
73
73
|
requirements: []
|
74
74
|
rubyforge_project:
|
75
75
|
rubygems_version: 2.2.1
|