anhldbk
anhldbk

Reputation: 4587

gRPC Java: How to know if a client disconnects abruptly (server side)?

When serving a gRPC client, my server allocates resources which I want to release them when the client disconnect.

My question is: how to properly handle the situation when clients disconnect abruptly?

Upvotes: 3

Views: 2272

Answers (2)

bianbian
bianbian

Reputation: 548

When build your Server, add a ServerTransportFilter that has a method: transportTerminated(Attributes transportAttrs).

Upvotes: 0

Carl Mastrangelo
Carl Mastrangelo

Reputation: 6658

This is generally not an issue, because the Server API is based on RPCs rather than transports. For example, you can't allocate resources specific to a connection, because the connection isn't exposed to you.

It's a good idea to not base resource decisions on connections, because it breaks down when proxies get involved. For example, if you have a TLS terminating proxy in front of your gRPC server, all the connections will appear to come from the same "client" even though in reality they don't.

Note: there are ways to approximate a connection (by looking at the client's socket address), but there is no way to know when the connection is gone. The address is purely informational.

Upvotes: 1

Related Questions