grpc-node
grpc-node copied to clipboard
`grpc.Client.close()` should have a callback when all calls complete
Is your feature request related to a problem? Please describe.
I'd like to call close
on all grpc clients when the app is shutting down, and wait for the in-flight requests to complete.
Describe the solution you'd like
As per #1211:
The
close
method prevents new calls from being started and frees up resources once all active calls have finished
I'd like to get a callback "once all active calls have finished" so I know it's safe to shutdown the server
(ideally it'd return a Promise
, but since the API is using callbacks, this should as well for consistency)
Describe alternatives you've considered
Right now we call close
then immediately process.exit(0)
. It generally works fine, but there might be inflight requests we then ignore.
Additional context
I'm curious, what's your use case for waiting to shut down the server until the Client
's close
callback has been called? I ask because shutdown itself actually has similar semantics: Server#tryShutdown
stops the server from accepting new requests, and calls its callback when there are no more active requests.
My server in this case is an express.js server serving user facing http traffic, not a grpc server. And this express app speaks grpc to the backend. When the app receives a sigterm it refuses new requests, but waits until all inbound requests are completed (similar to the tryShutdown
function). But we have some grpc requests that haven't completed yet even though all client requests have been handled, and I'd like to wait for those to complete before completing the graceful shutdown. Most of the time it's not issue as those internal requests usually takes a few ms from start to finish, but we've seen a couple of instances where it created som log noise.
I have a similar use case in which I want to know when all grpc connections have been fully closed out after calling close
. In my case, I'm spawning a proxy server and connecting grpc through that. I want to shut down the proxy server only after all existing connections have been fully closed. If I don't do this grpc itself prints an error for every connection that is still established at the time when I shutdown the proxy.
Failed to connect to proxy 127.0.0.1:9065 with error read ECONNRESET
Ideally, I'd await client.close()
before shutting down my proxy.
I am OK with adding this, but it will require some plumbing. Currently the client doesn't track calls that it started, so it doesn't know when they end.
Would be useful for some tests that do assertions on the connection state after closing client connections.
I have a client interceptor that needs to complete its task before I close the client. I need a way to ensure that everything is completed by the time I decide to close the client.
If there isn't a call to await on, the interceptor runs afterwards, and I get ordering bugs.
Is there something to poll on to know whether the client connections are all done?
If this callback did exist, it would track whether all requests have finished at the network level. It would not account for interceptor code that may be running after that.
I created my own interceptor that does track asynchronous interceptor code for now. I call it FlowCountInterceptor
and it uses a sort of reference count to eventually emit an event when the count of flows reaches 0. Each flow represents all asynchronous ops running from the inbound interceptors. Except for onReceiveMessage
because I found that only works on unary and client streaming calls, but not duplex nor server streaming calls.