How can I close Channel when using Interceptors

903 views
Skip to first unread message

Martin Grześlowski

unread,
Jun 15, 2015, 5:29:42 AM6/15/15
to grp...@googlegroups.com
Hi!

Normally when I was using stubs I was creating ChannelImpl (by NettyChannelBuilder) and then when program ends I used channelImpl.shutdown(). Below is some code snippet for thath use case:

ChannelImpl channel = NettyChannelBuilder.<...>.build();
Service.ServiceStub stub = Service.newStub(channel);
...do some stuf with stub...
channel
.shutdown(); // close channel and stub

Problem appears when I'm using interceptors. Method ClientInterceptors.intercept(channel, interceptor) return Channel (not ChannelImpl) which hasn't any shutdown method.

ClientHeaderInterceptor<?> interceptor interceptor();
Channel channel = ClientInterceptors.intercept(NettyChannelBuilder.<...>.build(), interceptor);
Service.ServiceStub stub = Service.newStub(channel);
...do some stuf with stub...
channel
.shutdown(); // Error, Channel does not have shutdown method
stub.shutdown(); // Also error

In my opinion stubs (normal, blocking and sync) should have method shutdown() to close them. Right now there is no way to end program.

FYI in RouteClient (from gRPC-java tutorials) you are also using approach with cloasing ChannelImpl.

Regards
Martin

Kun Zhang

unread,
Jun 15, 2015, 12:07:35 PM6/15/15
to grp...@googlegroups.com
By design the Stub and the Channel are not tied to particular resources. They are just interfaces that makes RPCs. ChannelImpl, however, is tied to a particular transport (connection). That's why only ChannelImpl has shutdown().

As a ChannelImpl can be used under multiple Stubs or intercepting Channels, adding shutdown() to Stub or Channel would require reference-counting the ChannelImpl, and it's hard to do it right.

Here we follow the rule that who creates an object is responsible for cleaning it up. Since it's you that call NettyChannelBuilder.build() and creates the ChannelImpl, you should save the ChannelImpl instance shut it down as desired.

Eric Anderson

unread,
Jun 15, 2015, 12:20:11 PM6/15/15
to Martin Grześlowski, grpc-io
Issue 64 suggested the same thing a while back. I was against it as it seems at odds with the interceptor design. I just now updated the issue with some explanation of a few things I see going wrong with it.

--
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 post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/251ea5c5-2579-4cdb-87aa-345a546ffbc7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Anderson

unread,
Jun 15, 2015, 1:15:55 PM6/15/15
to Martin Grześlowski, grpc-io
I just created issue 538 as an alternative. I'm not 100% sold on it either, but thought you might be interested.

Martin Grześlowski

unread,
Jun 16, 2015, 8:32:45 AM6/16/15
to grp...@googlegroups.com, martin.gr...@gmail.com
It would be great to have this feature from #538. Sometimes i far easier to have only one Channel object (ChannelImpl) and just care about this.
Reply all
Reply to author
Forward
0 new messages