-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Open
Description
Using the blocking v2 stubs, I've often found myself using a try
-finally
pattern like this:
var call = SomeGrpc.newBlockingV2Stub(channel).bidiRpc();
try {
while (true) {
var serverMessage = call.read();
if (serverMessage == null) {
break;
}
var clientMessage = doSomeWork(serverMessage);
call.write(clientMessage);
}
call.halfClose();
} finally {
call.cancel("done", null);
}
The idea is that if doSomeWork
throws an exception, the call is properly cleaned up.
This pattern is cumbersome, though. I also believe the unconditional cancel
invocation can send a superfluous RST_STREAM
frame in the case that the call is already successfully closed.
It would be nice if BlockingClientCall
had a close()
that did whatever was necessary to clean up the call. Then, try
-with-resources could be used like this:
try (var call = SomeGrpc.newBlockingV2Stub(channel).bidiRpc()) {
while (true) {
var serverMessage = call.read();
if (serverMessage == null) {
break;
}
var clientMessage = doSomeWork(serverMessage);
call.write(clientMessage);
}
call.halfClose();
}
Metadata
Metadata
Assignees
Labels
No labels