How to use serviceTransportFilter in GRPC JAVA Server

55 views
Skip to first unread message

Sidhartha Thota

unread,
Aug 19, 2021, 3:57:26 AM8/19/21
to grpc.io
Hi,

I am looking for a java GRPC server to know if any client (clients are c++ if it matters) connection disconnected abruptly due to network issue. I learnt from google search that GRPC java have serviceTransportFilter(https://grpc.github.io/grpc-java/javadoc/io/grpc/ServerTransportFilter.html) which can be added to ServerBuilder and the functions `transportReady` and `transportTerminated` are called when client is connected and disconnected. I added transport filter to my serverbuilder and killed by client program using `kill -9` but I don't see these method getting triggered. Am i missing anything? or my understanding about transport filter is wrong?

My serverTransportFilter implementation
public class GRPCServerTransportFilter extends ServerTransportFilter {
private static final Logger LOG = LoggerFactory.getLogger(GRPCServerTransportFilter.class);

@Override
public Attributes transportReady(final Attributes transportAttrs) {
LOG.info("Client connected {}", transportAttrs.toString());
return transportAttrs;
}

@Override
public void transportTerminated(final Attributes transportAttrs) {
LOG.info("Client disconnected {}", transportAttrs.toString());
}
}


NettyServerBuilder build code
io.grpc.Server server server = NettyServerBuilder.forPort(port)
.permitKeepAliveWithoutCalls(true)
.permitKeepAliveTime(keepAliveTimeOut, TimeUnit.SECONDS)
.addService(new GeyserListenerGRPCService(this::executeHandler,
this::onError, this::onCompleted))
.addTransportFilter(new GRPCServerTransportFilter())
.build();
server.start();

--
Thanks,
Sid.

sanjay...@google.com

unread,
Aug 20, 2021, 5:30:39 PM8/20/21
to grpc.io
Do you see the callback `transportReady()` being called? Pls make sure `transportReady()` is called. Because of lazy init the connection may not even have been created unless you sent RPCs

Also what's your use case that you need to know about a client connection init/termination status? 

Sidhartha Thota

unread,
Aug 23, 2021, 10:57:41 AM8/23/21
to sanjay...@google.com, grpc.io
Hi Sanjay,

Yes. my requirement is that I need my server to know about a client connection init/termination status. Do we have any other way to accomplish this? Please note, I need client address when it disconnects.

I will try once again if transportReady() is called after RPC call is started.

--
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/a741f7c7-1c8c-4925-8a37-fdcc48c4ef4cn%40googlegroups.com.


--
Thanks,
Sidhartha Thota.

Sanjay Pujare

unread,
Aug 23, 2021, 11:39:44 AM8/23/21
to Sidhartha Thota, grpc.io
On Mon, Aug 23, 2021 at 7:57 AM Sidhartha Thota <siddhu...@gmail.com> wrote:
Hi Sanjay,

Yes. my requirement is that I need my server to know about a client connection init/termination status. Do we have any other way to accomplish this? Please note, I need client address when it disconnects.



I will try once again if transportReady() is called after RPC call is started.

I am confident that if transportReady() works, then transportTerminated will work too.
Reply all
Reply to author
Forward
0 new messages