grpc / grpc/grpc-java

BlockingClientCall should be AutoCloseable

Offen
#12,197 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Java
Sterne
12.1k
Forks
4k
Ø Merge
2 T. 17 Std.
Gemergte PRs (30 T.)
37

Beschreibung

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();
}
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Start by locating the BlockingClientCall interface and its blocking v2 stub implementations, then inspect existing cancellation and close-related tests. Add AutoCloseable behavior so try-with-resources cleans up calls without an unnecessary reset after successful completion, and update tests to cover normal and exceptional exits.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
java
Bereich
api
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
52/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.