Skip to content

Commit 52961f7

Browse files
grpc: add docs for generic stream interfaces (#7470)
* Adding Inline comments for stream interfaces in stream_interfaces.go * Updating the Inline comments for stream interface in detail * Removing Inline comments for parent interfaces(ClientStream,ServerStream) * Updating the description of stream interfaces in stream_interfaces.go file * Updated the description as per the comments * Updating the description as per the comments addressed * Updating the description as per the comments addressed * Reverting generated code line * Removing extra space in generated code line * Updated the stream interfaces description as per the documentation and comments * Moving error and end of stream to interface docstring * dummy commit for re-trigger * Moved bidi handler line to interface docstring, updated the send in server stream and moved error lines to separate line * Fixed linter issues for superfluous-else, increment-decrement, indent-error-flow, var-declaration * Reverting context-as-argument in server.go * Revert "Optimising the code by fixing var-declaration, indent-error-flow, increment-decrement, superfluous-else" * Formatting comments and updating the docstring * Formatting comments * Updated the description by adding newline before the sentence. * updating file for format description * Doc updates * Remove leading spaces * Add comment to explain how to close streams from Servers --------- Co-authored-by: Doug Fawley <dfawley@google.com>
1 parent 005b092 commit 52961f7

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

stream_interfaces.go

+86
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,35 @@ package grpc
2222
// request, many responses) RPC. It is generic over the type of the response
2323
// message. It is used in generated code.
2424
type ServerStreamingClient[Res any] interface {
25+
// Recv receives the next response message from the server. The client may
26+
// repeatedly call Recv to read messages from the response stream. If
27+
// io.EOF is returned, the stream has terminated with an OK status. Any
28+
// other error is compatible with the status package and indicates the
29+
// RPC's status code and message.
2530
Recv() (*Res, error)
31+
32+
// ClientStream is embedded to provide Context, Header, and Trailer
33+
// functionality. No other methods in the ClientStream should be called
34+
// directly.
2635
ClientStream
2736
}
2837

2938
// ServerStreamingServer represents the server side of a server-streaming (one
3039
// request, many responses) RPC. It is generic over the type of the response
3140
// message. It is used in generated code.
41+
//
42+
// To terminate the response stream, return from the handler method and return
43+
// an error from the status package, or use nil to indicate an OK status code.
3244
type ServerStreamingServer[Res any] interface {
45+
// Send sends a response message to the client. The server handler may
46+
// call Send multiple times to send multiple messages to the client. An
47+
// error is returned if the stream was terminated unexpectedly, and the
48+
// handler method should return, as the stream is no longer usable.
3349
Send(*Res) error
50+
51+
// ServerStream is embedded to provide Context, SetHeader, SendHeader, and
52+
// SetTrailer functionality. No other methods in the ServerStream should
53+
// be called directly.
3454
ServerStream
3555
}
3656

@@ -39,18 +59,51 @@ type ServerStreamingServer[Res any] interface {
3959
// message stream and the type of the unary response message. It is used in
4060
// generated code.
4161
type ClientStreamingClient[Req any, Res any] interface {
62+
// Send sends a request message to the server. The client may call Send
63+
// multiple times to send multiple messages to the server. On error, Send
64+
// aborts the stream. If the error was generated by the client, the status
65+
// is returned directly. Otherwise, io.EOF is returned, and the status of
66+
// the stream may be discovered using CloseAndRecv().
4267
Send(*Req) error
68+
69+
// CloseAndRecv closes the request stream and waits for the server's
70+
// response. This method must be called once and only once after sending
71+
// all request messages. Any error returned is implemented by the status
72+
// package.
4373
CloseAndRecv() (*Res, error)
74+
75+
// ClientStream is embedded to provide Context, Header, and Trailer
76+
// functionality. No other methods in the ClientStream should be called
77+
// directly.
4478
ClientStream
4579
}
4680

4781
// ClientStreamingServer represents the server side of a client-streaming (many
4882
// requests, one response) RPC. It is generic over both the type of the request
4983
// message stream and the type of the unary response message. It is used in
5084
// generated code.
85+
//
86+
// To terminate the RPC, call SendAndClose and return nil from the method
87+
// handler or do not call SendAndClose and return an error from the status
88+
// package.
5189
type ClientStreamingServer[Req any, Res any] interface {
90+
// Recv receives the next request message from the client. The server may
91+
// repeatedly call Recv to read messages from the request stream. If
92+
// io.EOF is returned, it indicates the client called CloseAndRecv on its
93+
// ClientStreamingClient. Any other error indicates the stream was
94+
// terminated unexpectedly, and the handler method should return, as the
95+
// stream is no longer usable.
5296
Recv() (*Req, error)
97+
98+
// SendAndClose sends a single response message to the client and closes
99+
// the stream. This method must be called once and only once after all
100+
// request messages have been processed. Recv should not be called after
101+
// calling SendAndClose.
53102
SendAndClose(*Res) error
103+
104+
// ServerStream is embedded to provide Context, SetHeader, SendHeader, and
105+
// SetTrailer functionality. No other methods in the ServerStream should
106+
// be called directly.
54107
ServerStream
55108
}
56109

@@ -59,18 +112,51 @@ type ClientStreamingServer[Req any, Res any] interface {
59112
// request message stream and the type of the response message stream. It is
60113
// used in generated code.
61114
type BidiStreamingClient[Req any, Res any] interface {
115+
// Send sends a request message to the server. The client may call Send
116+
// multiple times to send multiple messages to the server. On error, Send
117+
// aborts the stream. If the error was generated by the client, the status
118+
// is returned directly. Otherwise, io.EOF is returned, and the status of
119+
// the stream may be discovered using Recv().
62120
Send(*Req) error
121+
122+
// Recv receives the next response message from the server. The client may
123+
// repeatedly call Recv to read messages from the response stream. If
124+
// io.EOF is returned, the stream has terminated with an OK status. Any
125+
// other error is compatible with the status package and indicates the
126+
// RPC's status code and message.
63127
Recv() (*Res, error)
128+
129+
// ClientStream is embedded to provide Context, Header, Trailer, and
130+
// CloseSend functionality. No other methods in the ClientStream should be
131+
// called directly.
64132
ClientStream
65133
}
66134

67135
// BidiStreamingServer represents the server side of a bidirectional-streaming
68136
// (many requests, many responses) RPC. It is generic over both the type of the
69137
// request message stream and the type of the response message stream. It is
70138
// used in generated code.
139+
//
140+
// To terminate the stream, return from the handler method and return
141+
// an error from the status package, or use nil to indicate an OK status code.
71142
type BidiStreamingServer[Req any, Res any] interface {
143+
// Recv receives the next request message from the client. The server may
144+
// repeatedly call Recv to read messages from the request stream. If
145+
// io.EOF is returned, it indicates the client called CloseSend on its
146+
// BidiStreamingClient. Any other error indicates the stream was
147+
// terminated unexpectedly, and the handler method should return, as the
148+
// stream is no longer usable.
72149
Recv() (*Req, error)
150+
151+
// Send sends a response message to the client. The server handler may
152+
// call Send multiple times to send multiple messages to the client. An
153+
// error is returned if the stream was terminated unexpectedly, and the
154+
// handler method should return, as the stream is no longer usable.
73155
Send(*Res) error
156+
157+
// ServerStream is embedded to provide Context, SetHeader, SendHeader, and
158+
// SetTrailer functionality. No other methods in the ServerStream should
159+
// be called directly.
74160
ServerStream
75161
}
76162

0 commit comments

Comments
 (0)