grpc-java icon indicating copy to clipboard operation
grpc-java copied to clipboard

New method StreamObserver.complete(value) for unary RPC

Open panchenko opened this issue 1 year ago • 2 comments

I think unary RPCs are used quite often, then in the service implementation we have to write 2 lines:

responseObserver.onNext(result);
responseObserver.onCompleted();

It seems logical to introduce a default method:

default void complete(V value) {
  onNext(value);
  onCompleted();
}

WDYT?

panchenko avatar Feb 26 '24 14:02 panchenko

Thanks for the suggestion. We're going to discuss it with the team at our next API Stabilization meeting.

sergiitk avatar Mar 20 '24 21:03 sergiitk

API review notes (2024-04-09 (?) and 2024-04-18)

  • This could be done through a utility function. Doesn't seem people have made utility functions for themselves for this. If we just added it as a utility function, could be added to “StreamObservers” (and un-deprecate the class)
  • There's the option for Exception handling in the implementation - we don’t encourage try/catch around next in general, so not necessary for initial implementation
  • Shouldn’t name it “complete”; that's ambiguous - “onNextAndCompleted” seemed better
  • This is just added as syntactic sugar, users can use it if they want, but we don't need to encourage ti. We don't need to update the existing examples/docs to use it
  • Approved as experimental. @panchenko, feel free to send a PR to add StreamObserver.onNextAndCompleted()

ejona86 avatar Apr 23 '24 20:04 ejona86

Actually I am not sure about the "onNextAndCompleted" method name.

The fact that StreamObserver is used on both sides make naming harder. The 'on" prefix is usually used in consumers, but this method is for producers. Because of this duality, would be simpler not adding it to the interface now.

Potentially in the future stubs can be generated with a different type, and then method names could be more producer-specific 😄

I would suggest nextAndComplete() for the static method name.

panchenko avatar Dec 24 '24 10:12 panchenko

Will discuss in next week's API review meeting about nextAndCompleted proposed by the PR instead of the earlier agreed upon name onNextAndCompleted.

kannanjgithub avatar Mar 11 '25 10:03 kannanjgithub

We have merged PR #11778 with the static method name as nextAndComplete in StreamObservers.

kannanjgithub avatar Apr 17 '25 07:04 kannanjgithub