grpc / grpc/grpc-java

BlockingClientCall should be AutoCloseable

Aperta
#12,197 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
12.1k
Fork
4k
Merge medio
2g 17h
PR unite (30g)
37

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java
Ambito
api
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.