BlockingClientCall should be AutoCloseable
- Dominant language
- Java
- Stars
- 12.1k
- Forks
- 4k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 37
Description
Using the blocking v2 stubs, I've often found myself using a `try`-`finally` pattern like this:
```java
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:
```java
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();
}
```
Contributor guide
Assessment
This issue has not been assessed yet.