Problems in wrting a client interceptor for python

83 views
Skip to first unread message

cyc19...@gmail.com

unread,
Jun 8, 2020, 3:04:56 AM6/8/20
to grpc.io

I'm now working on a feature that needs server headers in a client interceptor. Unfortunately, I see a previous interceptor implemented like this:
def wrapper(iterator):
 
for entry in iterator:
   
yield entry
class MyInterceptor(grpc.StreamStreamClientInterceptor):
 
def intercept_stream_stream(self, continuation, client_call_details, request_iterator):
   
return wrapper(continuation(client_call_details, request_iterator))
The returned object is a bare iterator so that I cannot use `response.initial_metadata` to get the headers.

I'm considering three ways to retain the metadata:
1. Delegate last response and do my job in the delegate class.
2. Override `Rendezvous` classes and make new ones to return. (It may lose changes made in previous interceptors.)
3. Monkey patch last response. (It cannot patch magic methods)

So the the only clean way is to delegate the response.
However, the delegation looks ugly, because the class has a dozen of methods to delegate.
Is there a clean and beautiful way to do the job?

cyc19...@gmail.com

unread,
Jun 8, 2020, 3:17:54 AM6/8/20
to grpc.io
Also, `grpc._channel._Rendezvous` is a private class. It makes me nervous to inherit.

Lidi Zheng

unread,
Jun 8, 2020, 1:09:58 PM6/8/20
to cyc19...@gmail.com, grpc.io
One example of how to implement the grpc.Call interface is to composite the original returned object (_Rendezvous) to a new class with the logic you want to add: https://github.com/googleads/google-ads-python/blob/ab71f5286e66b402125eafa5dd9db4f4c37b4806/google/ads/google_ads/interceptors/exception_interceptor.py#L29

On Mon, Jun 8, 2020 at 12:17 AM <cyc19...@gmail.com> wrote:
Also, `grpc._channel._Rendezvous` is a private class. It makes me nervous to inherit.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/bf23e779-3219-448f-9d90-e7cae112e957o%40googlegroups.com.

cyc19...@gmail.com

unread,
Jun 9, 2020, 2:47:59 AM6/9/20
to grpc.io
That's exactly I do in my code.
Hope it could be provided in `grpcio` package.
Java gRPC provides a class `SimpleForwardingClientCall`, which makes things much simpler.

On Tuesday, June 9, 2020 at 1:09:58 AM UTC+8, Lidi Zheng wrote:
One example of how to implement the grpc.Call interface is to composite the original returned object (_Rendezvous) to a new class with the logic you want to add: https://github.com/googleads/google-ads-python/blob/ab71f5286e66b402125eafa5dd9db4f4c37b4806/google/ads/google_ads/interceptors/exception_interceptor.py#L29

On Mon, Jun 8, 2020 at 12:17 AM <cyc19...@gmail.com> wrote:
Also, `grpc._channel._Rendezvous` is a private class. It makes me nervous to inherit.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grp...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages