Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a96fdc9

Browse files
committed
Pass the request event to the client_after_request hook
This is useful to get some context on the current task when the reply event is None (either because we are timing out or because we are talking to a PUSH/PULL or PUB/SUB). It also replaces the useless method argument from the patterns.
1 parent 9da0db8 commit a96fdc9

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

tests/test_middleware_client.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ def client_before_request(self, event):
9595
class ClientAfterRequestMiddleware(object):
9696
def __init__(self):
9797
self.called = False
98-
def client_after_request(self, event, exception):
98+
def client_after_request(self, req_event, rep_event, exception):
9999
self.called = True
100-
self.retcode = event.name
100+
assert req_event is not None
101+
assert req_event.name == "echo" or req_event.name == "echoes"
102+
self.retcode = rep_event.name
101103
assert exception is None
102104

103105
def test_hook_client_after_request():
@@ -158,9 +160,11 @@ def test_hook_client_after_request_timeout():
158160
class ClientAfterRequestMiddleware(object):
159161
def __init__(self):
160162
self.called = False
161-
def client_after_request(self, event, exception):
163+
def client_after_request(self, req_event, rep_event, exception):
162164
self.called = True
163-
assert event is None
165+
assert req_event is not None
166+
assert req_event.name == "timeout"
167+
assert rep_event is None
164168

165169
zero_ctx = zerorpc.Context()
166170
test_middleware = ClientAfterRequestMiddleware()
@@ -187,7 +191,9 @@ def client_after_request(self, event, exception):
187191
class ClientAfterFailedRequestMiddleware(object):
188192
def __init__(self):
189193
self.called = False
190-
def client_after_request(self, rep_event, exception):
194+
def client_after_request(self, req_event, rep_event, exception):
195+
assert req_event is not None
196+
assert req_event.name == "crash" or req_event.name == "echoes_crash"
191197
self.called = True
192198
assert isinstance(exception, zerorpc.RemoteError)
193199
assert exception.name == 'RuntimeError'
@@ -338,7 +344,9 @@ def client_handle_remote_error(self, event):
338344
etype = eval(name)
339345
e = etype(tb)
340346
return e
341-
def client_after_request(self, rep_event, exception):
347+
def client_after_request(self, req_event, rep_event, exception):
348+
assert req_event is not None
349+
assert req_event.name == "crash"
342350
self.called = True
343351
assert isinstance(exception, RuntimeError)
344352

zerorpc/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def hook_client_before_request(self, event):
151151
for functor in self._hooks['client_before_request']:
152152
functor(event)
153153

154-
def hook_client_after_request(self, reply_event, exception=None):
154+
def hook_client_after_request(self, request_event, reply_event, exception=None):
155155
"""Called when an answer or a timeout has been received from the server.
156156
157157
This hook is called right before the answer is returned to the client.
@@ -171,4 +171,4 @@ def hook_client_after_request(self, reply_event, exception=None):
171171
172172
"""
173173
for functor in self._hooks['client_after_request']:
174-
functor(reply_event, exception)
174+
functor(request_event, reply_event, exception)

zerorpc/core.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,17 @@ def _select_pattern(self, event):
208208
msg = 'Unable to find a pattern for: {0}'.format(event)
209209
raise RuntimeError(msg)
210210

211-
def _process_response(self, method, bufchan, timeout):
211+
def _process_response(self, request_event, bufchan, timeout):
212212
try:
213-
event = bufchan.recv(timeout)
214-
pattern = self._select_pattern(event)
215-
return pattern.process_answer(self._context, bufchan, event, method,
216-
self._handle_remote_error)
213+
reply_event = bufchan.recv(timeout)
214+
pattern = self._select_pattern(reply_event)
215+
return pattern.process_answer(self._context, bufchan, request_event,
216+
reply_event, self._handle_remote_error)
217217
except TimeoutExpired:
218218
bufchan.close()
219-
# FIXME: Add the method name as a separate argument? (so you
220-
# could get it back using the args attribute in the middleware).
221219
ex = TimeoutExpired(timeout,
222-
'calling remote method {0}'.format(method))
223-
self._context.hook_client_after_request(None, ex)
220+
'calling remote method {0}'.format(request_event.name))
221+
self._context.hook_client_after_request(request_event, None, ex)
224222
raise ex
225223
except:
226224
bufchan.close()
@@ -234,13 +232,13 @@ def __call__(self, method, *args, **kargs):
234232
bufchan = BufferedChannel(hbchan, inqueue_size=kargs.get('slots', 100))
235233

236234
xheader = self._context.hook_get_task_context()
237-
event = bufchan.create_event(method, args, xheader)
238-
self._context.hook_client_before_request(event)
239-
bufchan.emit_event(event)
235+
request_event = bufchan.create_event(method, args, xheader)
236+
self._context.hook_client_before_request(request_event)
237+
bufchan.emit_event(request_event)
240238

241239
try:
242240
if kargs.get('async', False) is False:
243-
return self._process_response(method, bufchan, timeout)
241+
return self._process_response(request_event, bufchan, timeout)
244242

245243
async_result = gevent.event.AsyncResult()
246244
gevent.spawn(self._process_response, method, bufchan,

zerorpc/patterns.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ def process_call(self, context, bufchan, req_event, functor):
3636
def accept_answer(self, event):
3737
return True
3838

39-
def process_answer(self, context, bufchan, rep_event, method,
39+
def process_answer(self, context, bufchan, req_event, rep_event,
4040
handle_remote_error):
4141
if rep_event.name == 'ERR':
4242
exception = handle_remote_error(rep_event)
43-
context.hook_client_after_request(rep_event, exception)
43+
context.hook_client_after_request(req_event, rep_event, exception)
4444
raise exception
45-
context.hook_client_after_request(rep_event)
45+
context.hook_client_after_request(req_event, rep_event)
4646
bufchan.close()
4747
result = rep_event.args[0]
4848
return result
@@ -66,27 +66,27 @@ def process_call(self, context, bufchan, req_event, functor):
6666
def accept_answer(self, event):
6767
return event.name in ('STREAM', 'STREAM_DONE')
6868

69-
def process_answer(self, context, bufchan, rep_event, method,
69+
def process_answer(self, context, bufchan, req_event, rep_event,
7070
handle_remote_error):
7171

7272
def is_stream_done(rep_event):
7373
return rep_event.name == 'STREAM_DONE'
7474
bufchan.on_close_if = is_stream_done
7575

76-
def iterator(rep_event):
76+
def iterator(req_event, rep_event):
7777
while rep_event.name == 'STREAM':
7878
# Like in process_call, we made the choice to call the
7979
# after_exec hook only when the stream is done.
8080
yield rep_event.args
8181
rep_event = bufchan.recv()
8282
if rep_event.name == 'ERR':
8383
exception = handle_remote_error(rep_event)
84-
context.hook_client_after_request(rep_event, exception)
84+
context.hook_client_after_request(req_event, rep_event, exception)
8585
raise exception
86-
context.hook_client_after_request(rep_event)
86+
context.hook_client_after_request(req_event, rep_event)
8787
bufchan.close()
8888

89-
return iterator(rep_event)
89+
return iterator(req_event, rep_event)
9090

9191

9292
patterns_list = [ReqStream(), ReqRep()]

0 commit comments

Comments
 (0)