Skip to content

BlockingClientCall should be AutoCloseable #12197

@benjaminp

Description

@benjaminp

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions