From def75b666d0fe0c3f00c2cfcdbd8040efb1d457d Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Thu, 1 Aug 2024 12:23:58 +0530 Subject: [PATCH 01/23] Adding Inline comments for stream interfaces in stream_interfaces.go --- stream_interfaces.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/stream_interfaces.go b/stream_interfaces.go index 8b813529c0cc..91d0f85a2e62 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -22,7 +22,14 @@ package grpc // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. type ServerStreamingClient[Res any] interface { + // Recv receives a message from the server. The message type is determined + // by the Res type parameter. This method blocks until a message is received + // or an error occurs. Recv() (*Res, error) + + // ClientStream represents the basic client-side streaming operations. + // It provides methods like SendMsg, RecvMsg, etc., for interacting with + // the streaming RPC. ClientStream } @@ -30,7 +37,14 @@ type ServerStreamingClient[Res any] interface { // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. type ServerStreamingServer[Res any] interface { + // Send sends a message to the client. The message type is determined by + // the Res type parameter. This method blocks until the message is sent + // or an error occurs. Send(*Res) error + + // ServerStream represents the basic server-side streaming operations. + // It provides methods like SendMsg, RecvMsg, etc., for interacting with + // the streaming RPC. ServerStream } @@ -39,8 +53,19 @@ type ServerStreamingServer[Res any] interface { // message stream and the type of the unary response message. It is used in // generated code. type ClientStreamingClient[Req any, Res any] interface { + // Send sends a message to the server. The message type is determined by + // the Req type parameter. This method blocks until the message is sent + // or an error occurs. Send(*Req) error + + // CloseAndRecv closes the sending side of the stream and receives the + // unary response from the server. The response message type is determined + // by the Res type parameter. CloseAndRecv() (*Res, error) + + // ClientStream represents the basic client-side streaming operations. + // It provides methods like SendMsg, RecvMsg, etc., for interacting with + // the streaming RPC. ClientStream } @@ -49,8 +74,18 @@ type ClientStreamingClient[Req any, Res any] interface { // message stream and the type of the unary response message. It is used in // generated code. type ClientStreamingServer[Req any, Res any] interface { + // Recv receives a message from the client. The message type is determined + // by the Req type parameter. This method blocks until a message is received + // or an error occurs. Recv() (*Req, error) + + // SendAndClose sends the unary response to the client and closes the stream. + // The response message type is determined by the Res type parameter. SendAndClose(*Res) error + + // ServerStream represents the basic server-side streaming operations. + // It provides methods like SendMsg, RecvMsg, etc., for interacting with + // the streaming RPC. ServerStream } @@ -59,8 +94,19 @@ type ClientStreamingServer[Req any, Res any] interface { // request message stream and the type of the response message stream. It is // used in generated code. type BidiStreamingClient[Req any, Res any] interface { + // Send sends a message to the server. The message type is determined by + // the Req type parameter. This method blocks until the message is sent + // or an error occurs. Send(*Req) error + + // Recv receives a message from the server. The message type is determined + // by the Res type parameter. This method blocks until a message is received + // or an error occurs. Recv() (*Res, error) + + // ClientStream represents the basic client-side streaming operations. + // It provides methods like SendMsg, RecvMsg, etc., for interacting with + // the streaming RPC. ClientStream } @@ -69,8 +115,19 @@ type BidiStreamingClient[Req any, Res any] interface { // request message stream and the type of the response message stream. It is // used in generated code. type BidiStreamingServer[Req any, Res any] interface { + // Recv receives a message from the client. The message type is determined + // by the Req type parameter. This method blocks until a message is received + // or an error occurs. Recv() (*Req, error) + + // Send sends a message to the client. The message type is determined by + // the Res type parameter. This method blocks until the message is sent + // or an error occurs. Send(*Res) error + + // ServerStream represents the basic server-side streaming operations. + // It provides methods like SendMsg, RecvMsg, etc., for interacting with + // the streaming RPC. ServerStream } From 19dcd9b1aad7a2c71d3ba5b17ff8b3ad785e712f Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Thu, 1 Aug 2024 18:32:12 +0530 Subject: [PATCH 02/23] Updating the Inline comments for stream interface in detail --- stream_interfaces.go | 89 +++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 91d0f85a2e62..a1a9975a2eca 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -22,9 +22,12 @@ package grpc // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. type ServerStreamingClient[Res any] interface { - // Recv receives a message from the server. The message type is determined - // by the Res type parameter. This method blocks until a message is received - // or an error occurs. + // Recv receives the next message from the server's response stream. This + // method is called repeatedly to receive all messages sent by the server. + // The message type is determined by the Res type parameter of the + // ServerStreamingClient receiver. + // It returns the received message and an error if any occurred. + // It returns (nil, io.EOF) when the server has finished sending messages. Recv() (*Res, error) // ClientStream represents the basic client-side streaming operations. @@ -37,9 +40,12 @@ type ServerStreamingClient[Res any] interface { // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. type ServerStreamingServer[Res any] interface { - // Send sends a message to the client. The message type is determined by - // the Res type parameter. This method blocks until the message is sent - // or an error occurs. + // Send sends a response message to the client. This method is called to + // send each individual message as part of the server's response stream. + // The message type is determined by the Res type parameter + // of the ServerStreamingServer receiver. + // It returns an error if the message could not be sent. + // The end-of-stream is indicated by the return of the server-side handler. Send(*Res) error // ServerStream represents the basic server-side streaming operations. @@ -53,14 +59,20 @@ type ServerStreamingServer[Res any] interface { // message stream and the type of the unary response message. It is used in // generated code. type ClientStreamingClient[Req any, Res any] interface { - // Send sends a message to the server. The message type is determined by - // the Req type parameter. This method blocks until the message is sent - // or an error occurs. + // Send sends a request message to the server. This method is called repeatedly + // to send all messages as part of the client’s request stream. + // The message type is determined by the Req type parameter + // of the ClientStreamingClient receiver. It returns an error + // if the message could not be sent. Send(*Req) error - // CloseAndRecv closes the sending side of the stream and receives the - // unary response from the server. The response message type is determined - // by the Res type parameter. + // CloseAndRecv closes the sending side of the request stream and waits for the server + // to send a unary response message. This method is typically called after + // sending all request messages to signal the end of the stream and to receive + // the final response from the server. The response message type is determined + // by the Res type parameter of the ClientStreamingClient receiver. + // It returns the received response message and an error if any occurred. + // The CloseAndRecv method must be called once and only once to complete the RPC. CloseAndRecv() (*Res, error) // ClientStream represents the basic client-side streaming operations. @@ -74,13 +86,21 @@ type ClientStreamingClient[Req any, Res any] interface { // message stream and the type of the unary response message. It is used in // generated code. type ClientStreamingServer[Req any, Res any] interface { - // Recv receives a message from the client. The message type is determined - // by the Req type parameter. This method blocks until a message is received - // or an error occurs. + // Recv reads a request message from the client. This method is called repeatedly + // to receive all messages sent by the client as part of the request stream. + // The message type is determined by the Req type parameter of the + // ClientStreamingServer receiver. + // It returns the received message and an error if any occurred. + // It returns (nil, io.EOF) when the client has finished sending messages. Recv() (*Req, error) - // SendAndClose sends the unary response to the client and closes the stream. - // The response message type is determined by the Res type parameter. + // SendAndClose sends the unary response message to the client and closes + // the stream. This method is typically called after processing all + // request messages from the client to send the final response and close + // the stream. The response type is determined by the Res type parameter + // of the ClientStreamingServer receiver. + // It returns an error if the response could not be sent. + // SendAndClose must be called once and only once to complete the RPC. SendAndClose(*Res) error // ServerStream represents the basic server-side streaming operations. @@ -94,14 +114,18 @@ type ClientStreamingServer[Req any, Res any] interface { // request message stream and the type of the response message stream. It is // used in generated code. type BidiStreamingClient[Req any, Res any] interface { - // Send sends a message to the server. The message type is determined by - // the Req type parameter. This method blocks until the message is sent - // or an error occurs. + // Send sends a single message to the server. This method is called repeatedly + // to send all messages as part of the client’s request stream. + // The message type is determined by the Req type parameter + // of the BidiStreamingClient receiver. + // It returns an error if the message could not be sent. Send(*Req) error - // Recv receives a message from the server. The message type is determined - // by the Res type parameter. This method blocks until a message is received - // or an error occurs. + // Recv receives the next message from the server's response stream. This + // method is called repeatedly to receive all messages sent by the server. The + // message type is determined by the Res type parameter of the BidiStreamingClient + // receiver. It returns the received message and an error if any occurred. + // It returns (nil, io.EOF) when the server has finished sending messages. Recv() (*Res, error) // ClientStream represents the basic client-side streaming operations. @@ -115,14 +139,21 @@ type BidiStreamingClient[Req any, Res any] interface { // request message stream and the type of the response message stream. It is // used in generated code. type BidiStreamingServer[Req any, Res any] interface { - // Recv receives a message from the client. The message type is determined - // by the Req type parameter. This method blocks until a message is received - // or an error occurs. + // Recv receives a request message from the client. This method is called repeatedly + // to receive all messages sent by the client as part of the request stream. + // The message type is determined by the Req type parameter of the + // BidiStreamingServer receiver. + // It returns the received message and an error if any occurred. + // It returns (nil, io.EOF) when the client has finished sending messages. Recv() (*Req, error) - // Send sends a message to the client. The message type is determined by - // the Res type parameter. This method blocks until the message is sent - // or an error occurs. + // Send sends a response message to the client. This method is called + // repeatedly to send all messages as part of the server's response stream. + // The message type is determined by the Res type parameter of the + // BidiStreamingServer receiver. + // It returns an error if the message could not be sent. + // The end-of-stream for the server-to-client stream is indicated by the return + // of the bidirectional streaming method handler. Send(*Res) error // ServerStream represents the basic server-side streaming operations. From 979530286e3648fb6526771b27a91c7641b0d62d Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Fri, 2 Aug 2024 10:26:40 +0530 Subject: [PATCH 03/23] Removing Inline comments for parent interfaces(ClientStream,ServerStream) --- stream_interfaces.go | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index a1a9975a2eca..01e6f347a186 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -29,10 +29,6 @@ type ServerStreamingClient[Res any] interface { // It returns the received message and an error if any occurred. // It returns (nil, io.EOF) when the server has finished sending messages. Recv() (*Res, error) - - // ClientStream represents the basic client-side streaming operations. - // It provides methods like SendMsg, RecvMsg, etc., for interacting with - // the streaming RPC. ClientStream } @@ -47,10 +43,6 @@ type ServerStreamingServer[Res any] interface { // It returns an error if the message could not be sent. // The end-of-stream is indicated by the return of the server-side handler. Send(*Res) error - - // ServerStream represents the basic server-side streaming operations. - // It provides methods like SendMsg, RecvMsg, etc., for interacting with - // the streaming RPC. ServerStream } @@ -74,10 +66,6 @@ type ClientStreamingClient[Req any, Res any] interface { // It returns the received response message and an error if any occurred. // The CloseAndRecv method must be called once and only once to complete the RPC. CloseAndRecv() (*Res, error) - - // ClientStream represents the basic client-side streaming operations. - // It provides methods like SendMsg, RecvMsg, etc., for interacting with - // the streaming RPC. ClientStream } @@ -102,10 +90,6 @@ type ClientStreamingServer[Req any, Res any] interface { // It returns an error if the response could not be sent. // SendAndClose must be called once and only once to complete the RPC. SendAndClose(*Res) error - - // ServerStream represents the basic server-side streaming operations. - // It provides methods like SendMsg, RecvMsg, etc., for interacting with - // the streaming RPC. ServerStream } @@ -127,10 +111,6 @@ type BidiStreamingClient[Req any, Res any] interface { // receiver. It returns the received message and an error if any occurred. // It returns (nil, io.EOF) when the server has finished sending messages. Recv() (*Res, error) - - // ClientStream represents the basic client-side streaming operations. - // It provides methods like SendMsg, RecvMsg, etc., for interacting with - // the streaming RPC. ClientStream } @@ -155,10 +135,6 @@ type BidiStreamingServer[Req any, Res any] interface { // The end-of-stream for the server-to-client stream is indicated by the return // of the bidirectional streaming method handler. Send(*Res) error - - // ServerStream represents the basic server-side streaming operations. - // It provides methods like SendMsg, RecvMsg, etc., for interacting with - // the streaming RPC. ServerStream } From d56889f9181f913f211daa9e2e1b1c49b611ba61 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Wed, 7 Aug 2024 20:07:00 +0530 Subject: [PATCH 04/23] Updating the description of stream interfaces in stream_interfaces.go file --- stream_interfaces.go | 108 +++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 01e6f347a186..cb1767a439da 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -22,12 +22,11 @@ package grpc // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. type ServerStreamingClient[Res any] interface { - // Recv receives the next message from the server's response stream. This - // method is called repeatedly to receive all messages sent by the server. - // The message type is determined by the Res type parameter of the - // ServerStreamingClient receiver. - // It returns the received message and an error if any occurred. - // It returns (nil, io.EOF) when the server has finished sending messages. + // Recv receives the next message from the server in the stream. The type of + // message is determined by the Res type parameter of the ServerStreamingClient. + // The client can repeatedly call the Recv method on the returned ServerStreamingClient + // stream in order to read the server-to-client response stream. This Recv method returns + // (nil, io.EOF) once the server-to-client stream has been completely read through. Recv() (*Res, error) ClientStream } @@ -38,10 +37,10 @@ type ServerStreamingClient[Res any] interface { type ServerStreamingServer[Res any] interface { // Send sends a response message to the client. This method is called to // send each individual message as part of the server's response stream. - // The message type is determined by the Res type parameter - // of the ServerStreamingServer receiver. - // It returns an error if the message could not be sent. - // The end-of-stream is indicated by the return of the server-side handler. + // It takes a pointer to the response message of type Res and returns an error if + // there was an issue sending the message. The server-side handler can send a + // stream of protobuf messages to the client through this parameter’s Send method. + // End-of-stream for the server-to-client stream is caused by the return of the handler method. Send(*Res) error ServerStream } @@ -51,20 +50,21 @@ type ServerStreamingServer[Res any] interface { // message stream and the type of the unary response message. It is used in // generated code. type ClientStreamingClient[Req any, Res any] interface { - // Send sends a request message to the server. This method is called repeatedly - // to send all messages as part of the client’s request stream. - // The message type is determined by the Req type parameter - // of the ClientStreamingClient receiver. It returns an error - // if the message could not be sent. + // Send sends a request message to the server. This method is called repeatedly to + // send all messages as part of the client’s request stream. The type of message is + // determined by the Req type parameter of the ClientStreamingClient. The client can + // repeatedly call the Send method on the returned ClientStreamingClient stream in + // order to send the client-to-server message stream. It returns an error if the + // message could not be sent. Send(*Req) error // CloseAndRecv closes the sending side of the request stream and waits for the server - // to send a unary response message. This method is typically called after - // sending all request messages to signal the end of the stream and to receive - // the final response from the server. The response message type is determined - // by the Res type parameter of the ClientStreamingClient receiver. - // It returns the received response message and an error if any occurred. - // The CloseAndRecv method must be called once and only once to complete the RPC. + // to send a unary response message. This method is typically called after sending all + // request messages to signal the end of the stream and to receive the final response + // from the server. The response message type is determined by the Res type parameter + // of the ClientStreamingClient. The CloseAndRecv method on this stream must be called + // once and only once, in order to both close the client-to-server stream and receive + // the single response message from the server. CloseAndRecv() (*Res, error) ClientStream } @@ -76,19 +76,18 @@ type ClientStreamingClient[Req any, Res any] interface { type ClientStreamingServer[Req any, Res any] interface { // Recv reads a request message from the client. This method is called repeatedly // to receive all messages sent by the client as part of the request stream. - // The message type is determined by the Req type parameter of the - // ClientStreamingServer receiver. - // It returns the received message and an error if any occurred. - // It returns (nil, io.EOF) when the client has finished sending messages. + // by the Req type parameter of the ClientStreamingServer. The server-side handler + // can repeatedly call Recv on this parameter in order to receive the full stream + // of messages from the client. Recv returns (nil, io.EOF) once it has reached + // the end of the stream. Recv() (*Req, error) - // SendAndClose sends the unary response message to the client and closes - // the stream. This method is typically called after processing all - // request messages from the client to send the final response and close - // the stream. The response type is determined by the Res type parameter - // of the ClientStreamingServer receiver. - // It returns an error if the response could not be sent. - // SendAndClose must be called once and only once to complete the RPC. + // SendAndClose sends the unary response message to the client and closes the stream. + // This method is typically called after processing all request messages from the client + // to send the final response and close the stream. The single response message from + // the server is sent by calling the SendAndClose method on this ClientStreamingServer + // parameter. It returns an error if the response could not be sent. SendAndClose must + // be called once and only once to complete the RPC. SendAndClose(*Res) error ServerStream } @@ -99,17 +98,21 @@ type ClientStreamingServer[Req any, Res any] interface { // used in generated code. type BidiStreamingClient[Req any, Res any] interface { // Send sends a single message to the server. This method is called repeatedly - // to send all messages as part of the client’s request stream. - // The message type is determined by the Req type parameter - // of the BidiStreamingClient receiver. - // It returns an error if the message could not be sent. + // to send all messages as part of the client’s request stream. The type of message + // is determined by the Req type parameter of the BidiStreamingClient. The client + // can repeatedly call the Send method on the returned BidiStreamingClient stream + // in order to send the client-to-server message stream. It returns an error + // if the message could not be sent. Send(*Req) error - // Recv receives the next message from the server's response stream. This - // method is called repeatedly to receive all messages sent by the server. The - // message type is determined by the Res type parameter of the BidiStreamingClient - // receiver. It returns the received message and an error if any occurred. - // It returns (nil, io.EOF) when the server has finished sending messages. + // Recv receives the next message from the server's response stream. This method + // is called repeatedly to receive all messages sent by the server. The type of + // message is determined by the Res type parameter of the BidiStreamingClient. + // The client can repeatedly call Recv on this stream in order to receive the + // full server-to-client message stream. End-of-stream for the server-to-client + // stream is indicated by a return value of (nil, io.EOF) on the Recv method of + // the stream. End-of-stream for the client-to-server stream can be indicated from + // the client by calling the CloseSend method on the stream. Recv() (*Res, error) ClientStream } @@ -119,21 +122,18 @@ type BidiStreamingClient[Req any, Res any] interface { // request message stream and the type of the response message stream. It is // used in generated code. type BidiStreamingServer[Req any, Res any] interface { - // Recv receives a request message from the client. This method is called repeatedly - // to receive all messages sent by the client as part of the request stream. - // The message type is determined by the Req type parameter of the - // BidiStreamingServer receiver. - // It returns the received message and an error if any occurred. - // It returns (nil, io.EOF) when the client has finished sending messages. + // Recv receives a request message from the client. The server-side handler can + // repeatedly call Recv on this parameter in order to read the client-to-server + // message stream. The type of message is determined by the Req type parameter + // of the BidiStreamingServer. Recv returns (nil, io.EOF) once it has reached + // the end of the client-to-server stream. when the client has finished sending messages. Recv() (*Req, error) - // Send sends a response message to the client. This method is called - // repeatedly to send all messages as part of the server's response stream. - // The message type is determined by the Res type parameter of the - // BidiStreamingServer receiver. - // It returns an error if the message could not be sent. - // The end-of-stream for the server-to-client stream is indicated by the return - // of the bidirectional streaming method handler. + // Send sends a response message to the client. The type of message is determined by the + // Res type parameter of the BidiStreamingServer. It returns an error if the message could not + // be sent. The response server-to-client message stream is sent by repeatedly calling + // the Send method of on this BidiStreamingServer parameter. The end-of-stream for the + // server-to-client stream is indicated by the return of the bidi streaming method handler. Send(*Res) error ServerStream } From 5b08be953aa27e8a10ae0fd7d1007c8f063db710 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Thu, 8 Aug 2024 15:04:26 +0530 Subject: [PATCH 05/23] Updated the description as per the comments --- stream_interfaces.go | 110 +++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 72 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index cb1767a439da..daaa28bd037b 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -20,126 +20,92 @@ package grpc // ServerStreamingClient represents the client side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response -// message. It is used in generated code. +// message. type ServerStreamingClient[Res any] interface { - // Recv receives the next message from the server in the stream. The type of - // message is determined by the Res type parameter of the ServerStreamingClient. - // The client can repeatedly call the Recv method on the returned ServerStreamingClient - // stream in order to read the server-to-client response stream. This Recv method returns - // (nil, io.EOF) once the server-to-client stream has been completely read through. + // Recv receives the next message from the server. If an error occurs on the + // stream, it will be returned as an instance of the 'status' package. Refer to + // the 'status' package documentation for more details. The stream ends with + // (nil, io.EOF) once all messages are received. Recv() (*Res, error) ClientStream } // ServerStreamingServer represents the server side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response -// message. It is used in generated code. +// message. type ServerStreamingServer[Res any] interface { - // Send sends a response message to the client. This method is called to - // send each individual message as part of the server's response stream. - // It takes a pointer to the response message of type Res and returns an error if - // there was an issue sending the message. The server-side handler can send a - // stream of protobuf messages to the client through this parameter’s Send method. - // End-of-stream for the server-to-client stream is caused by the return of the handler method. + // Send sends a response message to the client. It may be called multiple times + // to send multiple messages. The stream ends when the handler method returns. + // No methods on this interface should be called after that. Send(*Res) error ServerStream } // ClientStreamingClient represents the client side of a client-streaming (many // requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. It is used in -// generated code. +// message stream and the type of the unary response message. type ClientStreamingClient[Req any, Res any] interface { - // Send sends a request message to the server. This method is called repeatedly to - // send all messages as part of the client’s request stream. The type of message is - // determined by the Req type parameter of the ClientStreamingClient. The client can - // repeatedly call the Send method on the returned ClientStreamingClient stream in - // order to send the client-to-server message stream. It returns an error if the - // message could not be sent. + // Send sends a request message to the server. It may be called multiple times + // to send all messages in the request stream. If an error occurs, it is returned. Send(*Req) error - // CloseAndRecv closes the sending side of the request stream and waits for the server - // to send a unary response message. This method is typically called after sending all - // request messages to signal the end of the stream and to receive the final response - // from the server. The response message type is determined by the Res type parameter - // of the ClientStreamingClient. The CloseAndRecv method on this stream must be called - // once and only once, in order to both close the client-to-server stream and receive - // the single response message from the server. + // CloseAndRecv closes the sending side of the request stream and waits for + // the server's unary response. The response is returned as a single message + // or an error if the stream could not be closed or the message could not be received. CloseAndRecv() (*Res, error) ClientStream } // ClientStreamingServer represents the server side of a client-streaming (many // requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. It is used in -// generated code. +// message stream and the type of the unary response message. type ClientStreamingServer[Req any, Res any] interface { - // Recv reads a request message from the client. This method is called repeatedly - // to receive all messages sent by the client as part of the request stream. - // by the Req type parameter of the ClientStreamingServer. The server-side handler - // can repeatedly call Recv on this parameter in order to receive the full stream - // of messages from the client. Recv returns (nil, io.EOF) once it has reached - // the end of the stream. + // Recv reads a request message from the client. It may be called multiple times + // to receive all messages in the request stream. The stream ends with (nil, io.EOF) + // once all messages have been received. Recv() (*Req, error) - // SendAndClose sends the unary response message to the client and closes the stream. - // This method is typically called after processing all request messages from the client - // to send the final response and close the stream. The single response message from - // the server is sent by calling the SendAndClose method on this ClientStreamingServer - // parameter. It returns an error if the response could not be sent. SendAndClose must - // be called once and only once to complete the RPC. + // SendAndClose sends a unary response message to the client and closes the stream. + // The stream is terminated upon calling this method. No further methods on this + // interface should be called afterward. SendAndClose(*Res) error ServerStream } // BidiStreamingClient represents the client side of a bidirectional-streaming // (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. It is -// used in generated code. +// request message stream and the type of the response message stream. type BidiStreamingClient[Req any, Res any] interface { - // Send sends a single message to the server. This method is called repeatedly - // to send all messages as part of the client’s request stream. The type of message - // is determined by the Req type parameter of the BidiStreamingClient. The client - // can repeatedly call the Send method on the returned BidiStreamingClient stream - // in order to send the client-to-server message stream. It returns an error - // if the message could not be sent. + // Send sends a message to the server. It may be called multiple times to + // send all messages in the request stream. If an error occurs, it is returned. Send(*Req) error - // Recv receives the next message from the server's response stream. This method - // is called repeatedly to receive all messages sent by the server. The type of - // message is determined by the Res type parameter of the BidiStreamingClient. - // The client can repeatedly call Recv on this stream in order to receive the - // full server-to-client message stream. End-of-stream for the server-to-client - // stream is indicated by a return value of (nil, io.EOF) on the Recv method of - // the stream. End-of-stream for the client-to-server stream can be indicated from - // the client by calling the CloseSend method on the stream. + // Recv receives the next message from the server. The stream ends with + // (nil, io.EOF) once all messages have been received. If an error occurs on the + // stream, it will be returned as an instance of the 'status' package. Refer to + // the 'status' package documentation for more details. Recv() (*Res, error) ClientStream } // BidiStreamingServer represents the server side of a bidirectional-streaming // (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. It is -// used in generated code. +// request message stream and the type of the response message stream. type BidiStreamingServer[Req any, Res any] interface { - // Recv receives a request message from the client. The server-side handler can - // repeatedly call Recv on this parameter in order to read the client-to-server - // message stream. The type of message is determined by the Req type parameter - // of the BidiStreamingServer. Recv returns (nil, io.EOF) once it has reached - // the end of the client-to-server stream. when the client has finished sending messages. + // Recv receives a request message from the client. It may be called multiple times + // to receive all messages in the request stream. The stream ends with (nil, io.EOF) + // once all messages have been received. Recv() (*Req, error) - // Send sends a response message to the client. The type of message is determined by the - // Res type parameter of the BidiStreamingServer. It returns an error if the message could not - // be sent. The response server-to-client message stream is sent by repeatedly calling - // the Send method of on this BidiStreamingServer parameter. The end-of-stream for the - // server-to-client stream is indicated by the return of the bidi streaming method handler. + // Send sends a response message to the client. It may be called multiple times + // to send all messages in the response stream. The stream ends when the handler + // method returns. No further methods on this interface should be called afterward. Send(*Res) error ServerStream } // GenericClientStream implements the ServerStreamingClient, ClientStreamingClient, -// and BidiStreamingClient interfaces. It is used in generated code. +// and BidiStreamingClient interfaces. type GenericClientStream[Req any, Res any] struct { ClientStream } @@ -181,7 +147,7 @@ func (x *GenericClientStream[Req, Res]) CloseAndRecv() (*Res, error) { } // GenericServerStream implements the ServerStreamingServer, ClientStreamingServer, -// and BidiStreamingServer interfaces. It is used in generated code. +// and BidiStreamingServer interfaces. type GenericServerStream[Req any, Res any] struct { ServerStream } From fc8da54748c37b014a543d6de93500be35aec18c Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Fri, 9 Aug 2024 12:19:10 +0530 Subject: [PATCH 06/23] Updating the description as per the comments addressed --- stream_interfaces.go | 101 ++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 31 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index daaa28bd037b..09be768c638d 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -20,9 +20,11 @@ package grpc // ServerStreamingClient represents the client side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response -// message. +// message. It is used in generated code. type ServerStreamingClient[Res any] interface { - // Recv receives the next message from the server. If an error occurs on the + // Recv receives the next message from the server. The client can repeatedly + // call the Recv method on the returned ServerStreamingClient stream in order + // to read the server-to-client response stream. If an error occurs on the // stream, it will be returned as an instance of the 'status' package. Refer to // the 'status' package documentation for more details. The stream ends with // (nil, io.EOF) once all messages are received. @@ -32,74 +34,111 @@ type ServerStreamingClient[Res any] interface { // ServerStreamingServer represents the server side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response -// message. +// message. It is used in generated code. type ServerStreamingServer[Res any] interface { - // Send sends a response message to the client. It may be called multiple times - // to send multiple messages. The stream ends when the handler method returns. - // No methods on this interface should be called after that. + // Send sends a response message to the client. This method is called to + // send each individual message as part of the server's response stream. + // It may be called multiple times to send multiple messages. If an error + // occurs on the stream, it will be returned as an instance of the 'status' + // package. Refer to the 'status' package documentation for more details. The + // stream ends when the handler method returns. No methods on this interface + // should be called after that. Send(*Res) error ServerStream } // ClientStreamingClient represents the client side of a client-streaming (many // requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. +// message stream and the type of the unary response message. It is used in +// generated code. type ClientStreamingClient[Req any, Res any] interface { - // Send sends a request message to the server. It may be called multiple times - // to send all messages in the request stream. If an error occurs, it is returned. + // Send sends a request message to the server. The client can repeatedly call + // the Send method on the returned ClientStreamingClient stream in order to send + // the client-to-server message stream. If an error occurs on the stream, it will + // be returned as an instance of the 'status' package. Refer to the 'status' package + // documentation for more details. Send(*Req) error // CloseAndRecv closes the sending side of the request stream and waits for - // the server's unary response. The response is returned as a single message - // or an error if the stream could not be closed or the message could not be received. + // the server's unary response. This method is typically called after sending all + // request messages to signal the end of the stream and to receive the final response + // from the server. If an error occurs on the stream, it will be returned as an + // instance of the 'status' package. Refer to the 'status' package documentation + // for more details. The CloseAndRecv method on this stream must be called once and + // only once, in order to both close the client-to-server stream and receive the + // single response message from the server. CloseAndRecv() (*Res, error) ClientStream } // ClientStreamingServer represents the server side of a client-streaming (many // requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. +// message stream and the type of the unary response message. It is used in +// generated code. type ClientStreamingServer[Req any, Res any] interface { - // Recv reads a request message from the client. It may be called multiple times - // to receive all messages in the request stream. The stream ends with (nil, io.EOF) - // once all messages have been received. + // Recv reads a request message from the client. This method is called repeatedly + // to receive all messages sent by the client as part of the request stream. + // by the Req type parameter of the ClientStreamingServer. If an error occurs on + // the stream, it will be returned as an instance of the 'status' package. Refer to + // the 'status' package documentation for more details. The stream ends with + // (nil, io.EOF) once all messages have been received. Recv() (*Req, error) // SendAndClose sends a unary response message to the client and closes the stream. - // The stream is terminated upon calling this method. No further methods on this - // interface should be called afterward. + // This method is typically called after processing all request messages from the client + // to send the final response and close the stream. The single response message from + // the server is sent by calling the SendAndClose method on this ClientStreamingServer + // parameter. If an error occurs on the stream, it will be returned as an instance of the + // 'status' package. Refer to the 'status' package documentation for more details. The + // stream is terminated upon calling this method. No further methods on this interface + // should be called afterward. SendAndClose(*Res) error ServerStream } // BidiStreamingClient represents the client side of a bidirectional-streaming // (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. +// request message stream and the type of the response message stream. It is +// used in generated code. type BidiStreamingClient[Req any, Res any] interface { - // Send sends a message to the server. It may be called multiple times to - // send all messages in the request stream. If an error occurs, it is returned. + // Send sends a message to the server. This method is called repeatedly + // to send all messages as part of the client’s request stream. The client + // can repeatedly call the Send method on the returned BidiStreamingClient + // stream in order to send the client-to-server message stream. If an error + // occurs on the stream, it will be returned as an instance of the 'status' + // package. Refer to the 'status' package documentation for more details. Send(*Req) error - // Recv receives the next message from the server. The stream ends with - // (nil, io.EOF) once all messages have been received. If an error occurs on the - // stream, it will be returned as an instance of the 'status' package. Refer to - // the 'status' package documentation for more details. + // Recv receives the next message from the server's response stream. This method + // is called repeatedly to receive all messages sent by the server. The client can + // repeatedly call Recv on this stream in order to receive the full server-to-client + // message stream. If an error occurs on the stream, it will be returned as an instance + // of the 'status' package. Refer to the 'status' package documentation for more details. + // End-of-stream for the client-to-server stream can be indicated from the client by + // calling the CloseSend method on the stream. Recv() (*Res, error) ClientStream } // BidiStreamingServer represents the server side of a bidirectional-streaming // (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. +// request message stream and the type of the response message stream. It is +// used in generated code. type BidiStreamingServer[Req any, Res any] interface { - // Recv receives a request message from the client. It may be called multiple times - // to receive all messages in the request stream. The stream ends with (nil, io.EOF) - // once all messages have been received. + // Recv receives a request message from the client. The server-side handler can + // repeatedly call Recv on this parameter in order to read the client-to-server + // message stream. If an error occurs on the stream, it will be returned as an + // instance of the 'status' package. Refer to the 'status' package documentation + // for more details.Recv returns (nil, io.EOF) once it has reached the end of the + // client-to-server stream. when the client has finished sending messages. Recv() (*Req, error) - // Send sends a response message to the client. It may be called multiple times - // to send all messages in the response stream. The stream ends when the handler - // method returns. No further methods on this interface should be called afterward. + // Send sends a response message to the client. The response server-to-client + // message stream is sent by repeatedly calling the Send method of on this + // BidiStreamingServer parameter. If an error occurs on the stream, it will be + // returned as an instance of the 'status' package. Refer to the 'status' package + // documentation for more details. No further methods on this interface should be + // called afterward. Send(*Res) error ServerStream } From 83098945c3cdddd936908c62a35f8cf00989a907 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Sun, 11 Aug 2024 18:34:00 +0530 Subject: [PATCH 07/23] Updating the description as per the comments addressed --- stream_interfaces.go | 100 +++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 09be768c638d..11b21c53bde8 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -23,11 +23,10 @@ package grpc // message. It is used in generated code. type ServerStreamingClient[Res any] interface { // Recv receives the next message from the server. The client can repeatedly - // call the Recv method on the returned ServerStreamingClient stream in order - // to read the server-to-client response stream. If an error occurs on the - // stream, it will be returned as an instance of the 'status' package. Refer to - // the 'status' package documentation for more details. The stream ends with - // (nil, io.EOF) once all messages are received. + // call the Recv method to read the response stream. If an error occurs on + // the stream, it will be returned as an instance of the status package. + // Refer to the status package documentation for more details. The stream + // ends with (nil, io.EOF) once all messages are received. Recv() (*Res, error) ClientStream } @@ -36,13 +35,12 @@ type ServerStreamingClient[Res any] interface { // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. type ServerStreamingServer[Res any] interface { - // Send sends a response message to the client. This method is called to - // send each individual message as part of the server's response stream. - // It may be called multiple times to send multiple messages. If an error - // occurs on the stream, it will be returned as an instance of the 'status' - // package. Refer to the 'status' package documentation for more details. The - // stream ends when the handler method returns. No methods on this interface - // should be called after that. + // Send sends a response message to the client. This method can be called + // multiple times to send multiple messages as part of the server's response + // stream. If an error occurs on the stream, it will be returned as an instance + // of the status package. Refer to the status package documentation for more + // details. The stream ends when the handler method returns. No methods on this + // interface should be called afterward. Send(*Res) error ServerStream } @@ -53,20 +51,18 @@ type ServerStreamingServer[Res any] interface { // generated code. type ClientStreamingClient[Req any, Res any] interface { // Send sends a request message to the server. The client can repeatedly call - // the Send method on the returned ClientStreamingClient stream in order to send - // the client-to-server message stream. If an error occurs on the stream, it will - // be returned as an instance of the 'status' package. Refer to the 'status' package - // documentation for more details. + // the Send method to send the request message stream. If an error occurs on + // the stream, it will be returned as an instance of the status package. Refer + // to the status package documentation for more details. Send(*Req) error // CloseAndRecv closes the sending side of the request stream and waits for - // the server's unary response. This method is typically called after sending all - // request messages to signal the end of the stream and to receive the final response - // from the server. If an error occurs on the stream, it will be returned as an - // instance of the 'status' package. Refer to the 'status' package documentation - // for more details. The CloseAndRecv method on this stream must be called once and - // only once, in order to both close the client-to-server stream and receive the - // single response message from the server. + // the server's unary response. This method is typically called after sending + // all request messages to signal the end of the stream and to receive the final + // response from the server. If an error occurs on the stream, it will be returned + // as an instance of the status package. Refer to the status package documentation + // for more details. The CloseAndRecv method must be called once and only once, + // to close the request stream and receive the single response message from the server. CloseAndRecv() (*Res, error) ClientStream } @@ -78,20 +74,17 @@ type ClientStreamingClient[Req any, Res any] interface { type ClientStreamingServer[Req any, Res any] interface { // Recv reads a request message from the client. This method is called repeatedly // to receive all messages sent by the client as part of the request stream. - // by the Req type parameter of the ClientStreamingServer. If an error occurs on - // the stream, it will be returned as an instance of the 'status' package. Refer to - // the 'status' package documentation for more details. The stream ends with - // (nil, io.EOF) once all messages have been received. + // If an error occurs on the stream, it will be returned as an instance of the + // status package. Refer to the status package documentation for more details. The + // stream ends with (nil, io.EOF) once all messages have been received. Recv() (*Req, error) // SendAndClose sends a unary response message to the client and closes the stream. - // This method is typically called after processing all request messages from the client - // to send the final response and close the stream. The single response message from - // the server is sent by calling the SendAndClose method on this ClientStreamingServer - // parameter. If an error occurs on the stream, it will be returned as an instance of the - // 'status' package. Refer to the 'status' package documentation for more details. The - // stream is terminated upon calling this method. No further methods on this interface - // should be called afterward. + // This method is typically called after processing all request messages from the + // client to send the final response and close the stream. If an error occurs on + // the stream, it will be returned as an instance of the status package. Refer to + // the status package documentation for more details. The stream is terminated upon + // calling this method. No further methods on this interface should be called afterward. SendAndClose(*Res) error ServerStream } @@ -102,20 +95,16 @@ type ClientStreamingServer[Req any, Res any] interface { // used in generated code. type BidiStreamingClient[Req any, Res any] interface { // Send sends a message to the server. This method is called repeatedly - // to send all messages as part of the client’s request stream. The client - // can repeatedly call the Send method on the returned BidiStreamingClient - // stream in order to send the client-to-server message stream. If an error - // occurs on the stream, it will be returned as an instance of the 'status' - // package. Refer to the 'status' package documentation for more details. + // to send all messages as part of the request message stream. If an error + // occurs on the stream, it will be returned as an instance of the status + // package. Refer to the status package documentation for more details. Send(*Req) error // Recv receives the next message from the server's response stream. This method - // is called repeatedly to receive all messages sent by the server. The client can - // repeatedly call Recv on this stream in order to receive the full server-to-client - // message stream. If an error occurs on the stream, it will be returned as an instance - // of the 'status' package. Refer to the 'status' package documentation for more details. - // End-of-stream for the client-to-server stream can be indicated from the client by - // calling the CloseSend method on the stream. + // can be called repeatedly to receive all messages sent by the server. If an + // error occurs on the stream, it will be returned as an instance of the status + // package. Refer to the status package documentation for more details. End-of-stream + // can be indicated by calling the CloseSend method on the stream. Recv() (*Res, error) ClientStream } @@ -126,25 +115,22 @@ type BidiStreamingClient[Req any, Res any] interface { // used in generated code. type BidiStreamingServer[Req any, Res any] interface { // Recv receives a request message from the client. The server-side handler can - // repeatedly call Recv on this parameter in order to read the client-to-server - // message stream. If an error occurs on the stream, it will be returned as an - // instance of the 'status' package. Refer to the 'status' package documentation - // for more details.Recv returns (nil, io.EOF) once it has reached the end of the - // client-to-server stream. when the client has finished sending messages. + // repeatedly call Recv to read the request message stream. If an error occurs on + // the stream, it will be returned as an instance of the status package. Refer to + // the status package documentation for more details. Recv returns (nil, io.EOF) + // once it has reached the end of the request stream from the client. Recv() (*Req, error) - // Send sends a response message to the client. The response server-to-client - // message stream is sent by repeatedly calling the Send method of on this - // BidiStreamingServer parameter. If an error occurs on the stream, it will be - // returned as an instance of the 'status' package. Refer to the 'status' package - // documentation for more details. No further methods on this interface should be - // called afterward. + // Send sends a response message to the client. The server-side handler can repeatedly + // call Send to write to the response message stream. If an error occurs, it will be + // returned as an instance of the status package. Refer to the status package documentation + // for more details. No further methods on this interface should be called afterward. Send(*Res) error ServerStream } // GenericClientStream implements the ServerStreamingClient, ClientStreamingClient, -// and BidiStreamingClient interfaces. +// and BidiStreamingClient interfaces. It is used in generated code. type GenericClientStream[Req any, Res any] struct { ClientStream } From 2403e3ca7dd6d9b4db8bd8d6cebfd632a2cd94b4 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Mon, 12 Aug 2024 14:59:52 +0530 Subject: [PATCH 08/23] Reverting generated code line --- stream_interfaces.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 11b21c53bde8..316b38bf88a7 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -172,7 +172,7 @@ func (x *GenericClientStream[Req, Res]) CloseAndRecv() (*Res, error) { } // GenericServerStream implements the ServerStreamingServer, ClientStreamingServer, -// and BidiStreamingServer interfaces. +// and BidiStreamingServer interfaces. It is used in generated code. type GenericServerStream[Req any, Res any] struct { ServerStream } From 3874b04b212924c18b1cd6da9da3f28100b3e3c0 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Mon, 12 Aug 2024 15:05:10 +0530 Subject: [PATCH 09/23] Removing extra space in generated code line --- stream_interfaces.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 316b38bf88a7..1a8bf7a67676 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -172,7 +172,7 @@ func (x *GenericClientStream[Req, Res]) CloseAndRecv() (*Res, error) { } // GenericServerStream implements the ServerStreamingServer, ClientStreamingServer, -// and BidiStreamingServer interfaces. It is used in generated code. +// and BidiStreamingServer interfaces. It is used in generated code. type GenericServerStream[Req any, Res any] struct { ServerStream } From 5eb8edaaad21591226294e84e68ed0ccf32a0b0a Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Tue, 13 Aug 2024 20:56:52 +0530 Subject: [PATCH 10/23] Updated the stream interfaces description as per the documentation and comments --- stream_interfaces.go | 89 +++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 1a8bf7a67676..5d361347c9fa 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -23,10 +23,10 @@ package grpc // message. It is used in generated code. type ServerStreamingClient[Res any] interface { // Recv receives the next message from the server. The client can repeatedly - // call the Recv method to read the response stream. If an error occurs on - // the stream, it will be returned as an instance of the status package. - // Refer to the status package documentation for more details. The stream - // ends with (nil, io.EOF) once all messages are received. + // call Recv to read messages from the server-to-client response stream. Recv + // returns (nil, io.EOF) once the server-to-client stream is completely read. + // If an error occurs on the stream, it will be returned as an instance of the + // status package. Refer to the status package documentation for more details. Recv() (*Res, error) ClientStream } @@ -35,12 +35,12 @@ type ServerStreamingClient[Res any] interface { // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. type ServerStreamingServer[Res any] interface { - // Send sends a response message to the client. This method can be called - // multiple times to send multiple messages as part of the server's response - // stream. If an error occurs on the stream, it will be returned as an instance - // of the status package. Refer to the status package documentation for more - // details. The stream ends when the handler method returns. No methods on this - // interface should be called afterward. + // Send can send a stream of messages to the client. It may be called multiple + // times to send multiple messages. However, once the handler method has returned, + // no further calls to this method should be made. + // If an error occurs on the stream, it will be returned as an instance of the status + // package. Refer to the status package documentation for more details. End-of-stream + // for the server-to-client stream is indicated by the return of the handler method. Send(*Res) error ServerStream } @@ -51,18 +51,16 @@ type ServerStreamingServer[Res any] interface { // generated code. type ClientStreamingClient[Req any, Res any] interface { // Send sends a request message to the server. The client can repeatedly call - // the Send method to send the request message stream. If an error occurs on - // the stream, it will be returned as an instance of the status package. Refer - // to the status package documentation for more details. + // Send to send messages as part of the client-to-server request stream. If an + // error occurs on the stream, it will be returned as an instance of the status + // package. Refer to the status package documentation for more details. Send(*Req) error - // CloseAndRecv closes the sending side of the request stream and waits for - // the server's unary response. This method is typically called after sending - // all request messages to signal the end of the stream and to receive the final - // response from the server. If an error occurs on the stream, it will be returned - // as an instance of the status package. Refer to the status package documentation - // for more details. The CloseAndRecv method must be called once and only once, - // to close the request stream and receive the single response message from the server. + // CloseAndRecv closes the client-to-server request stream and waits for the server's + // unary response. This method must be called once and only once after sending all + // request messages to close the stream and receive the final response from the server. + // If an error occurs on the stream, it will be returned as an instance of the status + // package. Refer to the status package documentation for more details. CloseAndRecv() (*Res, error) ClientStream } @@ -72,19 +70,18 @@ type ClientStreamingClient[Req any, Res any] interface { // message stream and the type of the unary response message. It is used in // generated code. type ClientStreamingServer[Req any, Res any] interface { - // Recv reads a request message from the client. This method is called repeatedly - // to receive all messages sent by the client as part of the request stream. - // If an error occurs on the stream, it will be returned as an instance of the - // status package. Refer to the status package documentation for more details. The - // stream ends with (nil, io.EOF) once all messages have been received. + // Recv reads a request message from the client. This method can be called + // repeatedly to receive the full stream of messages from the client. Recv returns + // (nil, io.EOF) once the end of the stream is reached. Errors will be returned + // as instances of the status package. Refer to the status package documentation + // for more details. Recv() (*Req, error) - // SendAndClose sends a unary response message to the client and closes the stream. - // This method is typically called after processing all request messages from the - // client to send the final response and close the stream. If an error occurs on - // the stream, it will be returned as an instance of the status package. Refer to - // the status package documentation for more details. The stream is terminated upon - // calling this method. No further methods on this interface should be called afterward. + // SendAndClose sends a single response message to the client and closes the stream. + // This method must be called once and only once after all request messages have + // been processed. If an error occurs on the stream, the error returned will be + // implemented by the status package. Please see the documentation in that package + // for more information. No further methods should be called after SendAndClose. SendAndClose(*Res) error ServerStream } @@ -94,17 +91,18 @@ type ClientStreamingServer[Req any, Res any] interface { // request message stream and the type of the response message stream. It is // used in generated code. type BidiStreamingClient[Req any, Res any] interface { - // Send sends a message to the server. This method is called repeatedly - // to send all messages as part of the request message stream. If an error - // occurs on the stream, it will be returned as an instance of the status - // package. Refer to the status package documentation for more details. + // Send sends a message to the server. This method can be called repeatedly + // to send messages as part of the client-to-server request stream. If an + // error occurs on the stream, it will be returned as an instance of the status + // package. Refer to the status package documentation for more details. End-of-stream + // for the client-to-server stream can be indicated by calling the CloseSend method. Send(*Req) error // Recv receives the next message from the server's response stream. This method - // can be called repeatedly to receive all messages sent by the server. If an - // error occurs on the stream, it will be returned as an instance of the status - // package. Refer to the status package documentation for more details. End-of-stream - // can be indicated by calling the CloseSend method on the stream. + // can be called repeatedly to receive all messages sent by the server. Recv returns + // (nil, io.EOF) once the server-to-client stream is completely read. If an error occurs + // on the stream, it will be returned as an instance of the status package. Refer to + // the status package documentation for more details. Recv() (*Res, error) ClientStream } @@ -115,16 +113,15 @@ type BidiStreamingClient[Req any, Res any] interface { // used in generated code. type BidiStreamingServer[Req any, Res any] interface { // Recv receives a request message from the client. The server-side handler can - // repeatedly call Recv to read the request message stream. If an error occurs on - // the stream, it will be returned as an instance of the status package. Refer to - // the status package documentation for more details. Recv returns (nil, io.EOF) - // once it has reached the end of the request stream from the client. + // repeatedly call Recv to read the request message stream. Recv returns (nil, io.EOF) + // once the end of the client-to-server stream is reached. Errors are returned as + // instances of the status package. Refer to the status package documentation for details. Recv() (*Req, error) // Send sends a response message to the client. The server-side handler can repeatedly - // call Send to write to the response message stream. If an error occurs, it will be - // returned as an instance of the status package. Refer to the status package documentation - // for more details. No further methods on this interface should be called afterward. + // call Send to write to the server-to-client message stream. The end of the response + // stream is indicated by the return of the bidi method handler. Errors are returned as + // instances of the status package. Refer to the status package documentation for details. Send(*Res) error ServerStream } From 49eb9d25740b3fccb50ddeb495e09c366a65b051 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Wed, 14 Aug 2024 11:27:59 +0530 Subject: [PATCH 11/23] Moving error and end of stream to interface docstring --- stream_interfaces.go | 67 +++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 5d361347c9fa..3fc6d6168223 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -20,108 +20,99 @@ package grpc // ServerStreamingClient represents the client side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response -// message. It is used in generated code. +// message. If an error occurs on the stream, it will be returned as an instance +// of the status package. Refer to the status package documentation for more +// details. It is used in generated code. type ServerStreamingClient[Res any] interface { // Recv receives the next message from the server. The client can repeatedly // call Recv to read messages from the server-to-client response stream. Recv // returns (nil, io.EOF) once the server-to-client stream is completely read. - // If an error occurs on the stream, it will be returned as an instance of the - // status package. Refer to the status package documentation for more details. Recv() (*Res, error) ClientStream } // ServerStreamingServer represents the server side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response -// message. It is used in generated code. +// message. If an error occurs on the stream, it will be returned as an instance +// of the status package. Refer to the status package documentation for more +// details. End-of-stream for the server-to-client stream is indicated by the +// return of the handler method. It is used in generated code. type ServerStreamingServer[Res any] interface { // Send can send a stream of messages to the client. It may be called multiple - // times to send multiple messages. However, once the handler method has returned, - // no further calls to this method should be made. - // If an error occurs on the stream, it will be returned as an instance of the status - // package. Refer to the status package documentation for more details. End-of-stream - // for the server-to-client stream is indicated by the return of the handler method. + // times to send multiple messages. Send(*Res) error ServerStream } // ClientStreamingClient represents the client side of a client-streaming (many // requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. It is used in -// generated code. +// message stream and the type of the unary response message. If an error occurs +// on the stream, it will be returned as an instance of the status package. Refer +// to the status package documentation for more details. It is used in generated code. type ClientStreamingClient[Req any, Res any] interface { // Send sends a request message to the server. The client can repeatedly call - // Send to send messages as part of the client-to-server request stream. If an - // error occurs on the stream, it will be returned as an instance of the status - // package. Refer to the status package documentation for more details. + // Send to send messages as part of the client-to-server request stream. Send(*Req) error // CloseAndRecv closes the client-to-server request stream and waits for the server's // unary response. This method must be called once and only once after sending all // request messages to close the stream and receive the final response from the server. - // If an error occurs on the stream, it will be returned as an instance of the status - // package. Refer to the status package documentation for more details. CloseAndRecv() (*Res, error) ClientStream } // ClientStreamingServer represents the server side of a client-streaming (many // requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. It is used in -// generated code. +// message stream and the type of the unary response message. If an error occurs +// on the stream, it will be returned as an instance of the status package. Refer +// to the status package documentation for more details. It is used in generated code. type ClientStreamingServer[Req any, Res any] interface { // Recv reads a request message from the client. This method can be called // repeatedly to receive the full stream of messages from the client. Recv returns - // (nil, io.EOF) once the end of the stream is reached. Errors will be returned - // as instances of the status package. Refer to the status package documentation - // for more details. + // (nil, io.EOF) once the end of the stream is reached. Recv() (*Req, error) // SendAndClose sends a single response message to the client and closes the stream. // This method must be called once and only once after all request messages have - // been processed. If an error occurs on the stream, the error returned will be - // implemented by the status package. Please see the documentation in that package - // for more information. No further methods should be called after SendAndClose. + // been processed. No further methods should be called after SendAndClose. SendAndClose(*Res) error ServerStream } // BidiStreamingClient represents the client side of a bidirectional-streaming // (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. It is -// used in generated code. +// request message stream and the type of the response message stream. If an +// error occurs on the stream, it will be returned as an instance of the status +// package. Refer to the status package documentation for more details. End-of-stream +// for the client-to-server stream can be indicated by calling the CloseSend method. +// It is used in generated code. type BidiStreamingClient[Req any, Res any] interface { // Send sends a message to the server. This method can be called repeatedly - // to send messages as part of the client-to-server request stream. If an - // error occurs on the stream, it will be returned as an instance of the status - // package. Refer to the status package documentation for more details. End-of-stream - // for the client-to-server stream can be indicated by calling the CloseSend method. + // to send messages as part of the client-to-server request stream. Send(*Req) error // Recv receives the next message from the server's response stream. This method // can be called repeatedly to receive all messages sent by the server. Recv returns - // (nil, io.EOF) once the server-to-client stream is completely read. If an error occurs - // on the stream, it will be returned as an instance of the status package. Refer to - // the status package documentation for more details. + // (nil, io.EOF) once the server-to-client stream is completely read. Recv() (*Res, error) ClientStream } // BidiStreamingServer represents the server side of a bidirectional-streaming // (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. It is +// request message stream and the type of the response message stream. If an +// error occurs on the stream, it will be returned as an instance of the status +// package. Refer to the status package documentation for more details. It is // used in generated code. type BidiStreamingServer[Req any, Res any] interface { // Recv receives a request message from the client. The server-side handler can // repeatedly call Recv to read the request message stream. Recv returns (nil, io.EOF) - // once the end of the client-to-server stream is reached. Errors are returned as - // instances of the status package. Refer to the status package documentation for details. + // once the end of the client-to-server stream is reached. Recv() (*Req, error) // Send sends a response message to the client. The server-side handler can repeatedly // call Send to write to the server-to-client message stream. The end of the response - // stream is indicated by the return of the bidi method handler. Errors are returned as - // instances of the status package. Refer to the status package documentation for details. + // stream is indicated by the return of the bidi method handler. Send(*Res) error ServerStream } From 1db4de7333c617a0dead9e6814e3ec1c91d2f5d8 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Wed, 14 Aug 2024 12:14:40 +0530 Subject: [PATCH 12/23] dummy commit for re-trigger --- stream_interfaces.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 3fc6d6168223..2ecea017ae4b 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -110,9 +110,9 @@ type BidiStreamingServer[Req any, Res any] interface { // once the end of the client-to-server stream is reached. Recv() (*Req, error) - // Send sends a response message to the client. The server-side handler can repeatedly - // call Send to write to the server-to-client message stream. The end of the response - // stream is indicated by the return of the bidi method handler. + // Send sends a response message to the client. The server-side handler can + // repeatedly call Send to write to the server-to-client message stream. The end + // of the response stream is indicated by the return of the bidi method handler. Send(*Res) error ServerStream } From 921241a1df514a406d85cbde475b7f218208c655 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Wed, 14 Aug 2024 12:47:17 +0530 Subject: [PATCH 13/23] Moved bidi handler line to interface docstring, updated the send in server stream and moved error lines to separate line --- stream_interfaces.go | 54 ++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 2ecea017ae4b..7134ff96a87e 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -20,9 +20,9 @@ package grpc // ServerStreamingClient represents the client side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response -// message. If an error occurs on the stream, it will be returned as an instance -// of the status package. Refer to the status package documentation for more -// details. It is used in generated code. +// message. It is used in generated code. +// If an error occurs on the stream, it will be returned as an instance of the status +// package. Refer to the status package documentation for more details. type ServerStreamingClient[Res any] interface { // Recv receives the next message from the server. The client can repeatedly // call Recv to read messages from the server-to-client response stream. Recv @@ -33,22 +33,24 @@ type ServerStreamingClient[Res any] interface { // ServerStreamingServer represents the server side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response -// message. If an error occurs on the stream, it will be returned as an instance -// of the status package. Refer to the status package documentation for more -// details. End-of-stream for the server-to-client stream is indicated by the -// return of the handler method. It is used in generated code. +// message. It is used in generated code. +// If an error occurs on the stream, it will be returned as an instance of the status +// package. Refer to the status package documentation for more details. +// End-of-stream for the server-to-client stream is indicated by the return of +// the handler method. type ServerStreamingServer[Res any] interface { - // Send can send a stream of messages to the client. It may be called multiple - // times to send multiple messages. + // Send can send a stream of messages to the client. Server handler may call + // Send multiple times to send multiple messages to the client. Send(*Res) error ServerStream } // ClientStreamingClient represents the client side of a client-streaming (many // requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. If an error occurs -// on the stream, it will be returned as an instance of the status package. Refer -// to the status package documentation for more details. It is used in generated code. +// message stream and the type of the unary response message. It is used in +// generated code. +// If an error occurs on the stream, it will be returned as an instance of the +// status package. Refer to the status package documentation for more details. type ClientStreamingClient[Req any, Res any] interface { // Send sends a request message to the server. The client can repeatedly call // Send to send messages as part of the client-to-server request stream. @@ -63,9 +65,10 @@ type ClientStreamingClient[Req any, Res any] interface { // ClientStreamingServer represents the server side of a client-streaming (many // requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. If an error occurs -// on the stream, it will be returned as an instance of the status package. Refer -// to the status package documentation for more details. It is used in generated code. +// message stream and the type of the unary response message. It is used in +// generated code. +// If an error occurs on the stream, it will be returned as an instance of the +// status package. Refer to the status package documentation for more details. type ClientStreamingServer[Req any, Res any] interface { // Recv reads a request message from the client. This method can be called // repeatedly to receive the full stream of messages from the client. Recv returns @@ -81,11 +84,12 @@ type ClientStreamingServer[Req any, Res any] interface { // BidiStreamingClient represents the client side of a bidirectional-streaming // (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. If an -// error occurs on the stream, it will be returned as an instance of the status -// package. Refer to the status package documentation for more details. End-of-stream -// for the client-to-server stream can be indicated by calling the CloseSend method. -// It is used in generated code. +// request message stream and the type of the response message stream. It is +// used in generated code. +// If an error occurs on the stream, it will be returned as an instance of the +// status package. Refer to the status package documentation for more details. +// End-of-stream for the client-to-server stream can be indicated by calling +// the CloseSend method. type BidiStreamingClient[Req any, Res any] interface { // Send sends a message to the server. This method can be called repeatedly // to send messages as part of the client-to-server request stream. @@ -100,10 +104,11 @@ type BidiStreamingClient[Req any, Res any] interface { // BidiStreamingServer represents the server side of a bidirectional-streaming // (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. If an -// error occurs on the stream, it will be returned as an instance of the status -// package. Refer to the status package documentation for more details. It is +// request message stream and the type of the response message stream. It is // used in generated code. +// If an error occurs on the stream, it will be returned as an instance of the status +// package. Refer to the status package documentation for more details. +// The end of the response stream is indicated by the return of the bidi method handler. type BidiStreamingServer[Req any, Res any] interface { // Recv receives a request message from the client. The server-side handler can // repeatedly call Recv to read the request message stream. Recv returns (nil, io.EOF) @@ -111,8 +116,7 @@ type BidiStreamingServer[Req any, Res any] interface { Recv() (*Req, error) // Send sends a response message to the client. The server-side handler can - // repeatedly call Send to write to the server-to-client message stream. The end - // of the response stream is indicated by the return of the bidi method handler. + // repeatedly call Send to write to the server-to-client message stream. Send(*Res) error ServerStream } From ed434dc9f561741f355f745e753ec4fc2087339f Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Wed, 14 Aug 2024 15:06:58 +0530 Subject: [PATCH 14/23] Fixed linter issues for superfluous-else, increment-decrement, indent-error-flow, var-declaration --- cmd/protoc-gen-go-grpc/grpc.go | 13 +++++++------ examples/features/flow_control/client/main.go | 2 +- examples/features/flow_control/server/main.go | 2 +- internal/channelz/funcs.go | 2 +- internal/internal.go | 4 ++-- internal/transport/http2_client.go | 6 ++---- internal/transport/http_util.go | 2 +- profiling/cmd/remote.go | 6 +++--- server.go | 4 ++-- stats/opentelemetry/internal/testutils/testutils.go | 4 ++-- 10 files changed, 22 insertions(+), 23 deletions(-) diff --git a/cmd/protoc-gen-go-grpc/grpc.go b/cmd/protoc-gen-go-grpc/grpc.go index abc216022928..720b203bdebf 100644 --- a/cmd/protoc-gen-go-grpc/grpc.go +++ b/cmd/protoc-gen-go-grpc/grpc.go @@ -347,11 +347,12 @@ func clientStreamInterface(g *protogen.GeneratedFile, method *protogen.Method) s typeParam := g.QualifiedGoIdent(method.Input.GoIdent) + ", " + g.QualifiedGoIdent(method.Output.GoIdent) if method.Desc.IsStreamingClient() && method.Desc.IsStreamingServer() { return g.QualifiedGoIdent(grpcPackage.Ident("BidiStreamingClient")) + "[" + typeParam + "]" - } else if method.Desc.IsStreamingClient() { + } + if method.Desc.IsStreamingClient() { return g.QualifiedGoIdent(grpcPackage.Ident("ClientStreamingClient")) + "[" + typeParam + "]" - } else { // i.e. if method.Desc.IsStreamingServer() - return g.QualifiedGoIdent(grpcPackage.Ident("ServerStreamingClient")) + "[" + g.QualifiedGoIdent(method.Output.GoIdent) + "]" } + return g.QualifiedGoIdent(grpcPackage.Ident("ServerStreamingClient")) + "[" + g.QualifiedGoIdent(method.Output.GoIdent) + "]" + } func genClientMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, method *protogen.Method, index int) { @@ -514,11 +515,11 @@ func serverStreamInterface(g *protogen.GeneratedFile, method *protogen.Method) s typeParam := g.QualifiedGoIdent(method.Input.GoIdent) + ", " + g.QualifiedGoIdent(method.Output.GoIdent) if method.Desc.IsStreamingClient() && method.Desc.IsStreamingServer() { return g.QualifiedGoIdent(grpcPackage.Ident("BidiStreamingServer")) + "[" + typeParam + "]" - } else if method.Desc.IsStreamingClient() { + } + if method.Desc.IsStreamingClient() { return g.QualifiedGoIdent(grpcPackage.Ident("ClientStreamingServer")) + "[" + typeParam + "]" - } else { // i.e. if method.Desc.IsStreamingServer() - return g.QualifiedGoIdent(grpcPackage.Ident("ServerStreamingServer")) + "[" + g.QualifiedGoIdent(method.Output.GoIdent) + "]" } + return g.QualifiedGoIdent(grpcPackage.Ident("ServerStreamingServer")) + "[" + g.QualifiedGoIdent(method.Output.GoIdent) + "]" } func genServerMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, method *protogen.Method, hnameFuncNameFormatter func(string) string) string { diff --git a/examples/features/flow_control/client/main.go b/examples/features/flow_control/client/main.go index 5aea26b9bd4b..b1296f529e1a 100644 --- a/examples/features/flow_control/client/main.go +++ b/examples/features/flow_control/client/main.go @@ -34,7 +34,7 @@ import ( var addr = flag.String("addr", "localhost:50052", "the address to connect to") -var payload string = string(make([]byte, 8*1024)) // 8KB +var payload = string(make([]byte, 8*1024)) // 8KB func main() { flag.Parse() diff --git a/examples/features/flow_control/server/main.go b/examples/features/flow_control/server/main.go index 1b674cab416b..5452c3e622a5 100644 --- a/examples/features/flow_control/server/main.go +++ b/examples/features/flow_control/server/main.go @@ -35,7 +35,7 @@ import ( var port = flag.Int("port", 50052, "port number") -var payload string = string(make([]byte, 8*1024)) // 8KB +var payload = string(make([]byte, 8*1024)) // 8KB // server is used to implement EchoServer. type server struct { diff --git a/internal/channelz/funcs.go b/internal/channelz/funcs.go index 03e24e1507aa..078bb81238bc 100644 --- a/internal/channelz/funcs.go +++ b/internal/channelz/funcs.go @@ -33,7 +33,7 @@ var ( // outside this package except by tests. IDGen IDGenerator - db *channelMap = newChannelMap() + db = newChannelMap() // EntriesPerPage defines the number of channelz entries to be shown on a web page. EntriesPerPage = 50 curState int32 diff --git a/internal/internal.go b/internal/internal.go index e1e1422e15c4..0eb4a4fedb2d 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -183,7 +183,7 @@ var ( // GRPCResolverSchemeExtraMetadata determines when gRPC will add extra // metadata to RPCs. - GRPCResolverSchemeExtraMetadata string = "xds" + GRPCResolverSchemeExtraMetadata = "xds" // EnterIdleModeForTesting gets the ClientConn to enter IDLE mode. EnterIdleModeForTesting any // func(*grpc.ClientConn) @@ -203,7 +203,7 @@ var ( // UserSetDefaultScheme is set to true if the user has overridden the // default resolver scheme. - UserSetDefaultScheme bool = false + UserSetDefaultScheme = false // ShuffleAddressListForTesting pseudo-randomizes the order of addresses. n // is the number of elements. swap swaps the elements with indexes i and j. diff --git a/internal/transport/http2_client.go b/internal/transport/http2_client.go index 3c63c706986d..cc7ca4c07623 100644 --- a/internal/transport/http2_client.go +++ b/internal/transport/http2_client.go @@ -1642,11 +1642,9 @@ func (t *http2Client) reader(errCh chan<- error) { t.closeStream(s, status.Error(code, msg), true, http2.ErrCodeProtocol, status.New(code, msg), nil, false) } continue - } else { - // Transport error. - t.Close(connectionErrorf(true, err, "error reading from server: %v", err)) - return } + t.Close(connectionErrorf(true, err, "error reading from server: %v", err)) + return } switch frame := frame.(type) { case *http2.MetaHeadersFrame: diff --git a/internal/transport/http_util.go b/internal/transport/http_util.go index 39cef3bd442e..b5303c54622d 100644 --- a/internal/transport/http_util.go +++ b/internal/transport/http_util.go @@ -389,7 +389,7 @@ type framer struct { fr *http2.Framer } -var writeBufferPoolMap map[int]*sync.Pool = make(map[int]*sync.Pool) +var writeBufferPoolMap = make(map[int]*sync.Pool) var writeBufferMutex sync.Mutex func newFramer(conn net.Conn, writeBufferSize, readBufferSize int, sharedWriteBuffer bool, maxHeaderListSize uint32) *framer { diff --git a/profiling/cmd/remote.go b/profiling/cmd/remote.go index 71c21c332b74..b75c3e3bb33b 100644 --- a/profiling/cmd/remote.go +++ b/profiling/cmd/remote.go @@ -90,9 +90,9 @@ func remoteCommand() error { if *flagEnableProfiling || *flagDisableProfiling { return setEnabled(ctx, c, *flagEnableProfiling) - } else if *flagRetrieveSnapshot { + } + if *flagRetrieveSnapshot { return retrieveSnapshot(ctx, c, *flagSnapshot) - } else { - return fmt.Errorf("what should I do with the remote target?") } + return fmt.Errorf("what should I do with the remote target?") } diff --git a/server.go b/server.go index 89f8e4792bf1..58f2311b5020 100644 --- a/server.go +++ b/server.go @@ -86,7 +86,7 @@ func init() { var statusOK = status.New(codes.OK, "") var logger = grpclog.Component("core") -type methodHandler func(srv any, ctx context.Context, dec func(any) error, interceptor UnaryServerInterceptor) (any, error) +type methodHandler func(ctx context.Context, srv any, dec func(any) error, interceptor UnaryServerInterceptor) (any, error) // MethodDesc represents an RPC service's method specification. type MethodDesc struct { @@ -1376,7 +1376,7 @@ func (s *Server) processUnaryRPC(ctx context.Context, t transport.ServerTranspor return nil } ctx = NewContextWithServerTransportStream(ctx, stream) - reply, appErr := md.Handler(info.serviceImpl, ctx, df, s.opts.unaryInt) + reply, appErr := md.Handler(ctx, info.serviceImpl, df, s.opts.unaryInt) if appErr != nil { appStatus, ok := status.FromError(appErr) if !ok { diff --git a/stats/opentelemetry/internal/testutils/testutils.go b/stats/opentelemetry/internal/testutils/testutils.go index 10856409eeaf..721ce00cd347 100644 --- a/stats/opentelemetry/internal/testutils/testutils.go +++ b/stats/opentelemetry/internal/testutils/testutils.go @@ -131,8 +131,8 @@ func createBucketCounts(recordingPoints []float64, bounds []float64) []uint64 { continue } for recordingPoints[recordingPointIndex] <= bound { - bucketCount += 1 - recordingPointIndex += 1 + bucketCount++ + recordingPointIndex++ if recordingPointIndex >= len(recordingPoints) { break } From 3ea5488a3e021fb9bfdd24a4fa821543f61ae11a Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Wed, 14 Aug 2024 16:00:13 +0530 Subject: [PATCH 15/23] Reverting context-as-argument in server.go --- server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index 58f2311b5020..89f8e4792bf1 100644 --- a/server.go +++ b/server.go @@ -86,7 +86,7 @@ func init() { var statusOK = status.New(codes.OK, "") var logger = grpclog.Component("core") -type methodHandler func(ctx context.Context, srv any, dec func(any) error, interceptor UnaryServerInterceptor) (any, error) +type methodHandler func(srv any, ctx context.Context, dec func(any) error, interceptor UnaryServerInterceptor) (any, error) // MethodDesc represents an RPC service's method specification. type MethodDesc struct { @@ -1376,7 +1376,7 @@ func (s *Server) processUnaryRPC(ctx context.Context, t transport.ServerTranspor return nil } ctx = NewContextWithServerTransportStream(ctx, stream) - reply, appErr := md.Handler(ctx, info.serviceImpl, df, s.opts.unaryInt) + reply, appErr := md.Handler(info.serviceImpl, ctx, df, s.opts.unaryInt) if appErr != nil { appStatus, ok := status.FromError(appErr) if !ok { From bd4478732c62c50e6e1a1a231d66f4270b236be7 Mon Sep 17 00:00:00 2001 From: janardhanvissa <47281167+janardhanvissa@users.noreply.github.com> Date: Sun, 18 Aug 2024 11:20:48 +0530 Subject: [PATCH 16/23] Revert "Optimising the code by fixing var-declaration, indent-error-flow, increment-decrement, superfluous-else" --- cmd/protoc-gen-go-grpc/grpc.go | 13 ++++++------- examples/features/flow_control/client/main.go | 2 +- examples/features/flow_control/server/main.go | 2 +- internal/channelz/funcs.go | 2 +- internal/internal.go | 4 ++-- internal/transport/http2_client.go | 6 ++++-- internal/transport/http_util.go | 2 +- profiling/cmd/remote.go | 6 +++--- stats/opentelemetry/internal/testutils/testutils.go | 4 ++-- 9 files changed, 21 insertions(+), 20 deletions(-) diff --git a/cmd/protoc-gen-go-grpc/grpc.go b/cmd/protoc-gen-go-grpc/grpc.go index 720b203bdebf..abc216022928 100644 --- a/cmd/protoc-gen-go-grpc/grpc.go +++ b/cmd/protoc-gen-go-grpc/grpc.go @@ -347,12 +347,11 @@ func clientStreamInterface(g *protogen.GeneratedFile, method *protogen.Method) s typeParam := g.QualifiedGoIdent(method.Input.GoIdent) + ", " + g.QualifiedGoIdent(method.Output.GoIdent) if method.Desc.IsStreamingClient() && method.Desc.IsStreamingServer() { return g.QualifiedGoIdent(grpcPackage.Ident("BidiStreamingClient")) + "[" + typeParam + "]" - } - if method.Desc.IsStreamingClient() { + } else if method.Desc.IsStreamingClient() { return g.QualifiedGoIdent(grpcPackage.Ident("ClientStreamingClient")) + "[" + typeParam + "]" + } else { // i.e. if method.Desc.IsStreamingServer() + return g.QualifiedGoIdent(grpcPackage.Ident("ServerStreamingClient")) + "[" + g.QualifiedGoIdent(method.Output.GoIdent) + "]" } - return g.QualifiedGoIdent(grpcPackage.Ident("ServerStreamingClient")) + "[" + g.QualifiedGoIdent(method.Output.GoIdent) + "]" - } func genClientMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, method *protogen.Method, index int) { @@ -515,11 +514,11 @@ func serverStreamInterface(g *protogen.GeneratedFile, method *protogen.Method) s typeParam := g.QualifiedGoIdent(method.Input.GoIdent) + ", " + g.QualifiedGoIdent(method.Output.GoIdent) if method.Desc.IsStreamingClient() && method.Desc.IsStreamingServer() { return g.QualifiedGoIdent(grpcPackage.Ident("BidiStreamingServer")) + "[" + typeParam + "]" - } - if method.Desc.IsStreamingClient() { + } else if method.Desc.IsStreamingClient() { return g.QualifiedGoIdent(grpcPackage.Ident("ClientStreamingServer")) + "[" + typeParam + "]" + } else { // i.e. if method.Desc.IsStreamingServer() + return g.QualifiedGoIdent(grpcPackage.Ident("ServerStreamingServer")) + "[" + g.QualifiedGoIdent(method.Output.GoIdent) + "]" } - return g.QualifiedGoIdent(grpcPackage.Ident("ServerStreamingServer")) + "[" + g.QualifiedGoIdent(method.Output.GoIdent) + "]" } func genServerMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, method *protogen.Method, hnameFuncNameFormatter func(string) string) string { diff --git a/examples/features/flow_control/client/main.go b/examples/features/flow_control/client/main.go index b1296f529e1a..5aea26b9bd4b 100644 --- a/examples/features/flow_control/client/main.go +++ b/examples/features/flow_control/client/main.go @@ -34,7 +34,7 @@ import ( var addr = flag.String("addr", "localhost:50052", "the address to connect to") -var payload = string(make([]byte, 8*1024)) // 8KB +var payload string = string(make([]byte, 8*1024)) // 8KB func main() { flag.Parse() diff --git a/examples/features/flow_control/server/main.go b/examples/features/flow_control/server/main.go index 5452c3e622a5..1b674cab416b 100644 --- a/examples/features/flow_control/server/main.go +++ b/examples/features/flow_control/server/main.go @@ -35,7 +35,7 @@ import ( var port = flag.Int("port", 50052, "port number") -var payload = string(make([]byte, 8*1024)) // 8KB +var payload string = string(make([]byte, 8*1024)) // 8KB // server is used to implement EchoServer. type server struct { diff --git a/internal/channelz/funcs.go b/internal/channelz/funcs.go index 078bb81238bc..03e24e1507aa 100644 --- a/internal/channelz/funcs.go +++ b/internal/channelz/funcs.go @@ -33,7 +33,7 @@ var ( // outside this package except by tests. IDGen IDGenerator - db = newChannelMap() + db *channelMap = newChannelMap() // EntriesPerPage defines the number of channelz entries to be shown on a web page. EntriesPerPage = 50 curState int32 diff --git a/internal/internal.go b/internal/internal.go index 0eb4a4fedb2d..e1e1422e15c4 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -183,7 +183,7 @@ var ( // GRPCResolverSchemeExtraMetadata determines when gRPC will add extra // metadata to RPCs. - GRPCResolverSchemeExtraMetadata = "xds" + GRPCResolverSchemeExtraMetadata string = "xds" // EnterIdleModeForTesting gets the ClientConn to enter IDLE mode. EnterIdleModeForTesting any // func(*grpc.ClientConn) @@ -203,7 +203,7 @@ var ( // UserSetDefaultScheme is set to true if the user has overridden the // default resolver scheme. - UserSetDefaultScheme = false + UserSetDefaultScheme bool = false // ShuffleAddressListForTesting pseudo-randomizes the order of addresses. n // is the number of elements. swap swaps the elements with indexes i and j. diff --git a/internal/transport/http2_client.go b/internal/transport/http2_client.go index cc7ca4c07623..3c63c706986d 100644 --- a/internal/transport/http2_client.go +++ b/internal/transport/http2_client.go @@ -1642,9 +1642,11 @@ func (t *http2Client) reader(errCh chan<- error) { t.closeStream(s, status.Error(code, msg), true, http2.ErrCodeProtocol, status.New(code, msg), nil, false) } continue + } else { + // Transport error. + t.Close(connectionErrorf(true, err, "error reading from server: %v", err)) + return } - t.Close(connectionErrorf(true, err, "error reading from server: %v", err)) - return } switch frame := frame.(type) { case *http2.MetaHeadersFrame: diff --git a/internal/transport/http_util.go b/internal/transport/http_util.go index b5303c54622d..39cef3bd442e 100644 --- a/internal/transport/http_util.go +++ b/internal/transport/http_util.go @@ -389,7 +389,7 @@ type framer struct { fr *http2.Framer } -var writeBufferPoolMap = make(map[int]*sync.Pool) +var writeBufferPoolMap map[int]*sync.Pool = make(map[int]*sync.Pool) var writeBufferMutex sync.Mutex func newFramer(conn net.Conn, writeBufferSize, readBufferSize int, sharedWriteBuffer bool, maxHeaderListSize uint32) *framer { diff --git a/profiling/cmd/remote.go b/profiling/cmd/remote.go index b75c3e3bb33b..71c21c332b74 100644 --- a/profiling/cmd/remote.go +++ b/profiling/cmd/remote.go @@ -90,9 +90,9 @@ func remoteCommand() error { if *flagEnableProfiling || *flagDisableProfiling { return setEnabled(ctx, c, *flagEnableProfiling) - } - if *flagRetrieveSnapshot { + } else if *flagRetrieveSnapshot { return retrieveSnapshot(ctx, c, *flagSnapshot) + } else { + return fmt.Errorf("what should I do with the remote target?") } - return fmt.Errorf("what should I do with the remote target?") } diff --git a/stats/opentelemetry/internal/testutils/testutils.go b/stats/opentelemetry/internal/testutils/testutils.go index 721ce00cd347..10856409eeaf 100644 --- a/stats/opentelemetry/internal/testutils/testutils.go +++ b/stats/opentelemetry/internal/testutils/testutils.go @@ -131,8 +131,8 @@ func createBucketCounts(recordingPoints []float64, bounds []float64) []uint64 { continue } for recordingPoints[recordingPointIndex] <= bound { - bucketCount++ - recordingPointIndex++ + bucketCount += 1 + recordingPointIndex += 1 if recordingPointIndex >= len(recordingPoints) { break } From 7b41ccd2521da19086a7c08c092570d6872dfcb3 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Thu, 22 Aug 2024 11:25:37 +0530 Subject: [PATCH 17/23] Formatting comments and updating the docstring --- stream_interfaces.go | 72 +++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 7134ff96a87e..29488975f9cd 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -21,12 +21,13 @@ package grpc // ServerStreamingClient represents the client side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. -// If an error occurs on the stream, it will be returned as an instance of the status -// package. Refer to the status package documentation for more details. +// If an error occurs on the stream, it will be compatible with the status +// package. type ServerStreamingClient[Res any] interface { // Recv receives the next message from the server. The client can repeatedly - // call Recv to read messages from the server-to-client response stream. Recv - // returns (nil, io.EOF) once the server-to-client stream is completely read. + // call Recv to read messages from the server-to-client response stream. + // Recv returns (nil, io.EOF) once the server-to-client stream is completely + // read. Recv() (*Res, error) ClientStream } @@ -34,10 +35,9 @@ type ServerStreamingClient[Res any] interface { // ServerStreamingServer represents the server side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. -// If an error occurs on the stream, it will be returned as an instance of the status -// package. Refer to the status package documentation for more details. -// End-of-stream for the server-to-client stream is indicated by the return of -// the handler method. +// If an error occurs on the stream, it will be compatible with the status +// package. End-of-stream for the server-to-client stream is indicated by the +// return of the handler method. type ServerStreamingServer[Res any] interface { // Send can send a stream of messages to the client. Server handler may call // Send multiple times to send multiple messages to the client. @@ -49,16 +49,17 @@ type ServerStreamingServer[Res any] interface { // requests, one response) RPC. It is generic over both the type of the request // message stream and the type of the unary response message. It is used in // generated code. -// If an error occurs on the stream, it will be returned as an instance of the -// status package. Refer to the status package documentation for more details. +// If an error occurs on the stream, it will be compatible with the status +// package. type ClientStreamingClient[Req any, Res any] interface { - // Send sends a request message to the server. The client can repeatedly call - // Send to send messages as part of the client-to-server request stream. + // Send sends a request message to the server. The client can repeatedly + // call Send to send messages as part of the client-to-server request stream. Send(*Req) error - // CloseAndRecv closes the client-to-server request stream and waits for the server's - // unary response. This method must be called once and only once after sending all - // request messages to close the stream and receive the final response from the server. + // CloseAndRecv closes the client-to-server request stream and waits for the + // server's unary response. This method must be called once and only once + // after sending all request messages to close the stream and receive the + // final response from the server. CloseAndRecv() (*Res, error) ClientStream } @@ -67,17 +68,18 @@ type ClientStreamingClient[Req any, Res any] interface { // requests, one response) RPC. It is generic over both the type of the request // message stream and the type of the unary response message. It is used in // generated code. -// If an error occurs on the stream, it will be returned as an instance of the -// status package. Refer to the status package documentation for more details. +// If an error occurs on the stream, it will be compatible with the status +// package. type ClientStreamingServer[Req any, Res any] interface { // Recv reads a request message from the client. This method can be called - // repeatedly to receive the full stream of messages from the client. Recv returns - // (nil, io.EOF) once the end of the stream is reached. + // repeatedly to receive the full stream of messages from the client. Recv + // returns (nil, io.EOF) once the end of the stream is reached. Recv() (*Req, error) - // SendAndClose sends a single response message to the client and closes the stream. - // This method must be called once and only once after all request messages have - // been processed. No further methods should be called after SendAndClose. + // SendAndClose sends a single response message to the client and closes the + // stream. This method must be called once and only once after all request + // messages have been processed. No further methods should be called after + // SendAndClose. SendAndClose(*Res) error ServerStream } @@ -86,18 +88,18 @@ type ClientStreamingServer[Req any, Res any] interface { // (many requests, many responses) RPC. It is generic over both the type of the // request message stream and the type of the response message stream. It is // used in generated code. -// If an error occurs on the stream, it will be returned as an instance of the -// status package. Refer to the status package documentation for more details. -// End-of-stream for the client-to-server stream can be indicated by calling -// the CloseSend method. +// If an error occurs on the stream, it will be compatible with the status +// package. End-of-stream for the client-to-server stream can be indicated by +// calling the CloseSend method. type BidiStreamingClient[Req any, Res any] interface { // Send sends a message to the server. This method can be called repeatedly // to send messages as part of the client-to-server request stream. Send(*Req) error - // Recv receives the next message from the server's response stream. This method - // can be called repeatedly to receive all messages sent by the server. Recv returns - // (nil, io.EOF) once the server-to-client stream is completely read. + // Recv receives the next message from the server's response stream. This + // method can be called repeatedly to receive all messages sent by the server. + // Recv returns (nil, io.EOF) once the server-to-client stream is completely + // read. Recv() (*Res, error) ClientStream } @@ -106,13 +108,13 @@ type BidiStreamingClient[Req any, Res any] interface { // (many requests, many responses) RPC. It is generic over both the type of the // request message stream and the type of the response message stream. It is // used in generated code. -// If an error occurs on the stream, it will be returned as an instance of the status -// package. Refer to the status package documentation for more details. -// The end of the response stream is indicated by the return of the bidi method handler. +// If an error occurs on the stream, it will be compatible with the status +// package. The end of the response stream is indicated by the return of the +// bidi method handler. type BidiStreamingServer[Req any, Res any] interface { - // Recv receives a request message from the client. The server-side handler can - // repeatedly call Recv to read the request message stream. Recv returns (nil, io.EOF) - // once the end of the client-to-server stream is reached. + // Recv receives a request message from the client. The server-side handler + // can repeatedly call Recv to read the request message stream. Recv returns + // (nil, io.EOF) once the end of the client-to-server stream is reached. Recv() (*Req, error) // Send sends a response message to the client. The server-side handler can From a009b7cad361527f2079d098edb80af1d5c5f670 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Thu, 22 Aug 2024 11:36:30 +0530 Subject: [PATCH 18/23] Formatting comments --- stream_interfaces.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 29488975f9cd..0db57597150a 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -97,9 +97,9 @@ type BidiStreamingClient[Req any, Res any] interface { Send(*Req) error // Recv receives the next message from the server's response stream. This - // method can be called repeatedly to receive all messages sent by the server. - // Recv returns (nil, io.EOF) once the server-to-client stream is completely - // read. + // method can be called repeatedly to receive all messages sent by the + // server. Recv returns (nil, io.EOF) once the server-to-client stream is + // completely read. Recv() (*Res, error) ClientStream } From ea1eabd98e63df767d3da4a187088a78408ba9d3 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Thu, 22 Aug 2024 23:03:51 +0530 Subject: [PATCH 19/23] Updated the description by adding newline before the sentence. --- stream_interfaces.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 0db57597150a..bf32821130b9 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -21,8 +21,8 @@ package grpc // ServerStreamingClient represents the client side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. -// If an error occurs on the stream, it will be compatible with the status -// package. +// +// The error returned will be compatible to the status package. type ServerStreamingClient[Res any] interface { // Recv receives the next message from the server. The client can repeatedly // call Recv to read messages from the server-to-client response stream. @@ -35,9 +35,10 @@ type ServerStreamingClient[Res any] interface { // ServerStreamingServer represents the server side of a server-streaming (one // request, many responses) RPC. It is generic over the type of the response // message. It is used in generated code. -// If an error occurs on the stream, it will be compatible with the status -// package. End-of-stream for the server-to-client stream is indicated by the -// return of the handler method. +// +// The error returned will be compatible to the status package. +// End-of-stream for the server-to-client stream is indicated by the return of +// the handler method. type ServerStreamingServer[Res any] interface { // Send can send a stream of messages to the client. Server handler may call // Send multiple times to send multiple messages to the client. @@ -49,8 +50,8 @@ type ServerStreamingServer[Res any] interface { // requests, one response) RPC. It is generic over both the type of the request // message stream and the type of the unary response message. It is used in // generated code. -// If an error occurs on the stream, it will be compatible with the status -// package. +// +// The error returned will be compatible to the status package. type ClientStreamingClient[Req any, Res any] interface { // Send sends a request message to the server. The client can repeatedly // call Send to send messages as part of the client-to-server request stream. @@ -68,8 +69,8 @@ type ClientStreamingClient[Req any, Res any] interface { // requests, one response) RPC. It is generic over both the type of the request // message stream and the type of the unary response message. It is used in // generated code. -// If an error occurs on the stream, it will be compatible with the status -// package. +// +// The error returned will be compatible to the status package. type ClientStreamingServer[Req any, Res any] interface { // Recv reads a request message from the client. This method can be called // repeatedly to receive the full stream of messages from the client. Recv @@ -88,9 +89,10 @@ type ClientStreamingServer[Req any, Res any] interface { // (many requests, many responses) RPC. It is generic over both the type of the // request message stream and the type of the response message stream. It is // used in generated code. -// If an error occurs on the stream, it will be compatible with the status -// package. End-of-stream for the client-to-server stream can be indicated by -// calling the CloseSend method. +// +// The error returned will be compatible to the status package. +// End-of-stream for the client-to-server stream can be indicated by calling +// the CloseSend method. type BidiStreamingClient[Req any, Res any] interface { // Send sends a message to the server. This method can be called repeatedly // to send messages as part of the client-to-server request stream. @@ -108,9 +110,10 @@ type BidiStreamingClient[Req any, Res any] interface { // (many requests, many responses) RPC. It is generic over both the type of the // request message stream and the type of the response message stream. It is // used in generated code. -// If an error occurs on the stream, it will be compatible with the status -// package. The end of the response stream is indicated by the return of the -// bidi method handler. +// +// The error returned will be compatible to the status package. +// The end of the response stream is indicated by the return of the bidi +// method handler. type BidiStreamingServer[Req any, Res any] interface { // Recv receives a request message from the client. The server-side handler // can repeatedly call Recv to read the request message stream. Recv returns From 10122ac248111bc373115331298faf1026c38868 Mon Sep 17 00:00:00 2001 From: vissajanardhan Date: Thu, 22 Aug 2024 23:17:01 +0530 Subject: [PATCH 20/23] updating file for format description --- stream_interfaces.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index bf32821130b9..75bca4d0789c 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -37,8 +37,8 @@ type ServerStreamingClient[Res any] interface { // message. It is used in generated code. // // The error returned will be compatible to the status package. -// End-of-stream for the server-to-client stream is indicated by the return of -// the handler method. +// End-of-stream for the server-to-client stream is indicated by the return +// of the handler method. type ServerStreamingServer[Res any] interface { // Send can send a stream of messages to the client. Server handler may call // Send multiple times to send multiple messages to the client. From 2d54e07b889f2f7e6bcbf2fb47601db837ab955d Mon Sep 17 00:00:00 2001 From: Doug Fawley Date: Wed, 28 Aug 2024 14:25:29 -0700 Subject: [PATCH 21/23] Doc updates --- stream_interfaces.go | 399 +++++++++++++++++++++++-------------------- 1 file changed, 213 insertions(+), 186 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index 75bca4d0789c..b8abda64d471 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -16,189 +16,216 @@ * */ -package grpc - -// ServerStreamingClient represents the client side of a server-streaming (one -// request, many responses) RPC. It is generic over the type of the response -// message. It is used in generated code. -// -// The error returned will be compatible to the status package. -type ServerStreamingClient[Res any] interface { - // Recv receives the next message from the server. The client can repeatedly - // call Recv to read messages from the server-to-client response stream. - // Recv returns (nil, io.EOF) once the server-to-client stream is completely - // read. - Recv() (*Res, error) - ClientStream -} - -// ServerStreamingServer represents the server side of a server-streaming (one -// request, many responses) RPC. It is generic over the type of the response -// message. It is used in generated code. -// -// The error returned will be compatible to the status package. -// End-of-stream for the server-to-client stream is indicated by the return -// of the handler method. -type ServerStreamingServer[Res any] interface { - // Send can send a stream of messages to the client. Server handler may call - // Send multiple times to send multiple messages to the client. - Send(*Res) error - ServerStream -} - -// ClientStreamingClient represents the client side of a client-streaming (many -// requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. It is used in -// generated code. -// -// The error returned will be compatible to the status package. -type ClientStreamingClient[Req any, Res any] interface { - // Send sends a request message to the server. The client can repeatedly - // call Send to send messages as part of the client-to-server request stream. - Send(*Req) error - - // CloseAndRecv closes the client-to-server request stream and waits for the - // server's unary response. This method must be called once and only once - // after sending all request messages to close the stream and receive the - // final response from the server. - CloseAndRecv() (*Res, error) - ClientStream -} - -// ClientStreamingServer represents the server side of a client-streaming (many -// requests, one response) RPC. It is generic over both the type of the request -// message stream and the type of the unary response message. It is used in -// generated code. -// -// The error returned will be compatible to the status package. -type ClientStreamingServer[Req any, Res any] interface { - // Recv reads a request message from the client. This method can be called - // repeatedly to receive the full stream of messages from the client. Recv - // returns (nil, io.EOF) once the end of the stream is reached. - Recv() (*Req, error) - - // SendAndClose sends a single response message to the client and closes the - // stream. This method must be called once and only once after all request - // messages have been processed. No further methods should be called after - // SendAndClose. - SendAndClose(*Res) error - ServerStream -} - -// BidiStreamingClient represents the client side of a bidirectional-streaming -// (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. It is -// used in generated code. -// -// The error returned will be compatible to the status package. -// End-of-stream for the client-to-server stream can be indicated by calling -// the CloseSend method. -type BidiStreamingClient[Req any, Res any] interface { - // Send sends a message to the server. This method can be called repeatedly - // to send messages as part of the client-to-server request stream. - Send(*Req) error - - // Recv receives the next message from the server's response stream. This - // method can be called repeatedly to receive all messages sent by the - // server. Recv returns (nil, io.EOF) once the server-to-client stream is - // completely read. - Recv() (*Res, error) - ClientStream -} - -// BidiStreamingServer represents the server side of a bidirectional-streaming -// (many requests, many responses) RPC. It is generic over both the type of the -// request message stream and the type of the response message stream. It is -// used in generated code. -// -// The error returned will be compatible to the status package. -// The end of the response stream is indicated by the return of the bidi -// method handler. -type BidiStreamingServer[Req any, Res any] interface { - // Recv receives a request message from the client. The server-side handler - // can repeatedly call Recv to read the request message stream. Recv returns - // (nil, io.EOF) once the end of the client-to-server stream is reached. - Recv() (*Req, error) - - // Send sends a response message to the client. The server-side handler can - // repeatedly call Send to write to the server-to-client message stream. - Send(*Res) error - ServerStream -} - -// GenericClientStream implements the ServerStreamingClient, ClientStreamingClient, -// and BidiStreamingClient interfaces. It is used in generated code. -type GenericClientStream[Req any, Res any] struct { - ClientStream -} - -var _ ServerStreamingClient[string] = (*GenericClientStream[int, string])(nil) -var _ ClientStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) -var _ BidiStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) - -// Send pushes one message into the stream of requests to be consumed by the -// server. The type of message which can be sent is determined by the Req type -// parameter of the GenericClientStream receiver. -func (x *GenericClientStream[Req, Res]) Send(m *Req) error { - return x.ClientStream.SendMsg(m) -} - -// Recv reads one message from the stream of responses generated by the server. -// The type of the message returned is determined by the Res type parameter -// of the GenericClientStream receiver. -func (x *GenericClientStream[Req, Res]) Recv() (*Res, error) { - m := new(Res) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -// CloseAndRecv closes the sending side of the stream, then receives the unary -// response from the server. The type of message which it returns is determined -// by the Res type parameter of the GenericClientStream receiver. -func (x *GenericClientStream[Req, Res]) CloseAndRecv() (*Res, error) { - if err := x.ClientStream.CloseSend(); err != nil { - return nil, err - } - m := new(Res) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -// GenericServerStream implements the ServerStreamingServer, ClientStreamingServer, -// and BidiStreamingServer interfaces. It is used in generated code. -type GenericServerStream[Req any, Res any] struct { - ServerStream -} - -var _ ServerStreamingServer[string] = (*GenericServerStream[int, string])(nil) -var _ ClientStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) -var _ BidiStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) - -// Send pushes one message into the stream of responses to be consumed by the -// client. The type of message which can be sent is determined by the Res -// type parameter of the serverStreamServer receiver. -func (x *GenericServerStream[Req, Res]) Send(m *Res) error { - return x.ServerStream.SendMsg(m) -} - -// SendAndClose pushes the unary response to the client. The type of message -// which can be sent is determined by the Res type parameter of the -// clientStreamServer receiver. -func (x *GenericServerStream[Req, Res]) SendAndClose(m *Res) error { - return x.ServerStream.SendMsg(m) -} - -// Recv reads one message from the stream of requests generated by the client. -// The type of the message returned is determined by the Req type parameter -// of the clientStreamServer receiver. -func (x *GenericServerStream[Req, Res]) Recv() (*Req, error) { - m := new(Req) - if err := x.ServerStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} + package grpc + + // ServerStreamingClient represents the client side of a server-streaming (one + // request, many responses) RPC. It is generic over the type of the response + // message. It is used in generated code. + type ServerStreamingClient[Res any] interface { + // Recv receives the next response message from the server. The client may + // repeatedly call Recv to read messages from the response stream. If + // io.EOF is returned, the stream has terminated with an OK status. Any + // other error is compatible with the status package and indicates the + // RPC's status code and message. + Recv() (*Res, error) + + // ClientStream is embedded to provide Context, Header, and Trailer + // functionality. No other methods in the ClientStream should be called + // directly. + ClientStream + } + + // ServerStreamingServer represents the server side of a server-streaming (one + // request, many responses) RPC. It is generic over the type of the response + // message. It is used in generated code. + // + // To terminate the response stream, return from the handler method and return + // an error from the status package, or use nil to indicate an OK status code. + type ServerStreamingServer[Res any] interface { + // Send sends a response message to the client. The server handler may + // call Send multiple times to send multiple messages to the client. An + // error is returned if the stream was terminated unexpectedly, and the + // handler method should return, as the stream is no longer usable. + Send(*Res) error + + // ServerStream is embedded to provide Context, SetHeader, SendHeader, and + // SetTrailer functionality. No other methods in the ServerStream should + // be called directly. + ServerStream + } + + // ClientStreamingClient represents the client side of a client-streaming (many + // requests, one response) RPC. It is generic over both the type of the request + // message stream and the type of the unary response message. It is used in + // generated code. + type ClientStreamingClient[Req any, Res any] interface { + // Send sends a request message to the server. The client may call Send + // multiple times to send multiple messages to the server. On error, Send + // aborts the stream. If the error was generated by the client, the status + // is returned directly. Otherwise, io.EOF is returned, and the status of + // the stream may be discovered using CloseAndRecv(). + Send(*Req) error + + // CloseAndRecv closes the request stream and waits for the server's + // response. This method must be called once and only once after sending + // all request messages. Any error returned is implemented by the status + // package. + CloseAndRecv() (*Res, error) + + // ClientStream is embedded to provide Context, Header, and Trailer + // functionality. No other methods in the ClientStream should be called + // directly. + ClientStream + } + + // ClientStreamingServer represents the server side of a client-streaming (many + // requests, one response) RPC. It is generic over both the type of the request + // message stream and the type of the unary response message. It is used in + // generated code. + type ClientStreamingServer[Req any, Res any] interface { + // Recv receives the next request message from the client. The server may + // repeatedly call Recv to read messages from the request stream. If + // io.EOF is returned, it indicates the client called CloseAndRecv on its + // ClientStreamingClient. Any other error indicates the stream was + // terminated unexpectedly, and the handler method should return, as the + // stream is no longer usable. + Recv() (*Req, error) + + // SendAndClose sends a single response message to the client and closes + // the stream. This method must be called once and only once after all + // request messages have been processed. Recv should not be called after + // calling SendAndClose. + SendAndClose(*Res) error + + // ServerStream is embedded to provide Context, SetHeader, SendHeader, and + // SetTrailer functionality. No other methods in the ServerStream should + // be called directly. + ServerStream + } + + // BidiStreamingClient represents the client side of a bidirectional-streaming + // (many requests, many responses) RPC. It is generic over both the type of the + // request message stream and the type of the response message stream. It is + // used in generated code. + type BidiStreamingClient[Req any, Res any] interface { + // Send sends a request message to the server. The client may call Send + // multiple times to send multiple messages to the server. On error, Send + // aborts the stream. If the error was generated by the client, the status + // is returned directly. Otherwise, io.EOF is returned, and the status of + // the stream may be discovered using Recv(). + Send(*Req) error + + // Recv receives the next response message from the server. The client may + // repeatedly call Recv to read messages from the response stream. If + // io.EOF is returned, the stream has terminated with an OK status. Any + // other error is compatible with the status package and indicates the + // RPC's status code and message. + Recv() (*Res, error) + + // ClientStream is embedded to provide Context, Header, Trailer, and + // CloseSend functionality. No other methods in the ClientStream should be + // called directly. + ClientStream + } + + // BidiStreamingServer represents the server side of a bidirectional-streaming + // (many requests, many responses) RPC. It is generic over both the type of the + // request message stream and the type of the response message stream. It is + // used in generated code. + type BidiStreamingServer[Req any, Res any] interface { + // Recv receives the next request message from the client. The server may + // repeatedly call Recv to read messages from the request stream. If + // io.EOF is returned, it indicates the client called CloseSend on its + // BidiStreamingClient. Any other error indicates the stream was + // terminated unexpectedly, and the handler method should return, as the + // stream is no longer usable. + Recv() (*Req, error) + + // Send sends a response message to the client. The server handler may + // call Send multiple times to send multiple messages to the client. An + // error is returned if the stream was terminated unexpectedly, and the + // handler method should return, as the stream is no longer usable. + Send(*Res) error + + // ServerStream is embedded to provide Context, SetHeader, SendHeader, and + // SetTrailer functionality. No other methods in the ServerStream should + // be called directly. + ServerStream + } + + // GenericClientStream implements the ServerStreamingClient, ClientStreamingClient, + // and BidiStreamingClient interfaces. It is used in generated code. + type GenericClientStream[Req any, Res any] struct { + ClientStream + } + + var _ ServerStreamingClient[string] = (*GenericClientStream[int, string])(nil) + var _ ClientStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) + var _ BidiStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) + + // Send pushes one message into the stream of requests to be consumed by the + // server. The type of message which can be sent is determined by the Req type + // parameter of the GenericClientStream receiver. + func (x *GenericClientStream[Req, Res]) Send(m *Req) error { + return x.ClientStream.SendMsg(m) + } + + // Recv reads one message from the stream of responses generated by the server. + // The type of the message returned is determined by the Res type parameter + // of the GenericClientStream receiver. + func (x *GenericClientStream[Req, Res]) Recv() (*Res, error) { + m := new(Res) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil + } + + // CloseAndRecv closes the sending side of the stream, then receives the unary + // response from the server. The type of message which it returns is determined + // by the Res type parameter of the GenericClientStream receiver. + func (x *GenericClientStream[Req, Res]) CloseAndRecv() (*Res, error) { + if err := x.ClientStream.CloseSend(); err != nil { + return nil, err + } + m := new(Res) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil + } + + // GenericServerStream implements the ServerStreamingServer, ClientStreamingServer, + // and BidiStreamingServer interfaces. It is used in generated code. + type GenericServerStream[Req any, Res any] struct { + ServerStream + } + + var _ ServerStreamingServer[string] = (*GenericServerStream[int, string])(nil) + var _ ClientStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) + var _ BidiStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) + + // Send pushes one message into the stream of responses to be consumed by the + // client. The type of message which can be sent is determined by the Res + // type parameter of the serverStreamServer receiver. + func (x *GenericServerStream[Req, Res]) Send(m *Res) error { + return x.ServerStream.SendMsg(m) + } + + // SendAndClose pushes the unary response to the client. The type of message + // which can be sent is determined by the Res type parameter of the + // clientStreamServer receiver. + func (x *GenericServerStream[Req, Res]) SendAndClose(m *Res) error { + return x.ServerStream.SendMsg(m) + } + + // Recv reads one message from the stream of requests generated by the client. + // The type of the message returned is determined by the Req type parameter + // of the clientStreamServer receiver. + func (x *GenericServerStream[Req, Res]) Recv() (*Req, error) { + m := new(Req) + if err := x.ServerStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil + } From 335eaeee8c9cdefbfebdbc59e807f2768271e0bc Mon Sep 17 00:00:00 2001 From: Doug Fawley Date: Wed, 28 Aug 2024 14:27:00 -0700 Subject: [PATCH 22/23] Remove leading spaces --- stream_interfaces.go | 426 +++++++++++++++++++++---------------------- 1 file changed, 213 insertions(+), 213 deletions(-) diff --git a/stream_interfaces.go b/stream_interfaces.go index b8abda64d471..0d809763b389 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -16,216 +16,216 @@ * */ - package grpc - - // ServerStreamingClient represents the client side of a server-streaming (one - // request, many responses) RPC. It is generic over the type of the response - // message. It is used in generated code. - type ServerStreamingClient[Res any] interface { - // Recv receives the next response message from the server. The client may - // repeatedly call Recv to read messages from the response stream. If - // io.EOF is returned, the stream has terminated with an OK status. Any - // other error is compatible with the status package and indicates the - // RPC's status code and message. - Recv() (*Res, error) - - // ClientStream is embedded to provide Context, Header, and Trailer - // functionality. No other methods in the ClientStream should be called - // directly. - ClientStream - } - - // ServerStreamingServer represents the server side of a server-streaming (one - // request, many responses) RPC. It is generic over the type of the response - // message. It is used in generated code. - // - // To terminate the response stream, return from the handler method and return - // an error from the status package, or use nil to indicate an OK status code. - type ServerStreamingServer[Res any] interface { - // Send sends a response message to the client. The server handler may - // call Send multiple times to send multiple messages to the client. An - // error is returned if the stream was terminated unexpectedly, and the - // handler method should return, as the stream is no longer usable. - Send(*Res) error - - // ServerStream is embedded to provide Context, SetHeader, SendHeader, and - // SetTrailer functionality. No other methods in the ServerStream should - // be called directly. - ServerStream - } - - // ClientStreamingClient represents the client side of a client-streaming (many - // requests, one response) RPC. It is generic over both the type of the request - // message stream and the type of the unary response message. It is used in - // generated code. - type ClientStreamingClient[Req any, Res any] interface { - // Send sends a request message to the server. The client may call Send - // multiple times to send multiple messages to the server. On error, Send - // aborts the stream. If the error was generated by the client, the status - // is returned directly. Otherwise, io.EOF is returned, and the status of - // the stream may be discovered using CloseAndRecv(). - Send(*Req) error - - // CloseAndRecv closes the request stream and waits for the server's - // response. This method must be called once and only once after sending - // all request messages. Any error returned is implemented by the status - // package. - CloseAndRecv() (*Res, error) - - // ClientStream is embedded to provide Context, Header, and Trailer - // functionality. No other methods in the ClientStream should be called - // directly. - ClientStream - } - - // ClientStreamingServer represents the server side of a client-streaming (many - // requests, one response) RPC. It is generic over both the type of the request - // message stream and the type of the unary response message. It is used in - // generated code. - type ClientStreamingServer[Req any, Res any] interface { - // Recv receives the next request message from the client. The server may - // repeatedly call Recv to read messages from the request stream. If - // io.EOF is returned, it indicates the client called CloseAndRecv on its - // ClientStreamingClient. Any other error indicates the stream was - // terminated unexpectedly, and the handler method should return, as the - // stream is no longer usable. - Recv() (*Req, error) - - // SendAndClose sends a single response message to the client and closes - // the stream. This method must be called once and only once after all - // request messages have been processed. Recv should not be called after - // calling SendAndClose. - SendAndClose(*Res) error - - // ServerStream is embedded to provide Context, SetHeader, SendHeader, and - // SetTrailer functionality. No other methods in the ServerStream should - // be called directly. - ServerStream - } - - // BidiStreamingClient represents the client side of a bidirectional-streaming - // (many requests, many responses) RPC. It is generic over both the type of the - // request message stream and the type of the response message stream. It is - // used in generated code. - type BidiStreamingClient[Req any, Res any] interface { - // Send sends a request message to the server. The client may call Send - // multiple times to send multiple messages to the server. On error, Send - // aborts the stream. If the error was generated by the client, the status - // is returned directly. Otherwise, io.EOF is returned, and the status of - // the stream may be discovered using Recv(). - Send(*Req) error - - // Recv receives the next response message from the server. The client may - // repeatedly call Recv to read messages from the response stream. If - // io.EOF is returned, the stream has terminated with an OK status. Any - // other error is compatible with the status package and indicates the - // RPC's status code and message. - Recv() (*Res, error) - - // ClientStream is embedded to provide Context, Header, Trailer, and - // CloseSend functionality. No other methods in the ClientStream should be - // called directly. - ClientStream - } - - // BidiStreamingServer represents the server side of a bidirectional-streaming - // (many requests, many responses) RPC. It is generic over both the type of the - // request message stream and the type of the response message stream. It is - // used in generated code. - type BidiStreamingServer[Req any, Res any] interface { - // Recv receives the next request message from the client. The server may - // repeatedly call Recv to read messages from the request stream. If - // io.EOF is returned, it indicates the client called CloseSend on its - // BidiStreamingClient. Any other error indicates the stream was - // terminated unexpectedly, and the handler method should return, as the - // stream is no longer usable. - Recv() (*Req, error) - - // Send sends a response message to the client. The server handler may - // call Send multiple times to send multiple messages to the client. An - // error is returned if the stream was terminated unexpectedly, and the - // handler method should return, as the stream is no longer usable. - Send(*Res) error - - // ServerStream is embedded to provide Context, SetHeader, SendHeader, and - // SetTrailer functionality. No other methods in the ServerStream should - // be called directly. - ServerStream - } - - // GenericClientStream implements the ServerStreamingClient, ClientStreamingClient, - // and BidiStreamingClient interfaces. It is used in generated code. - type GenericClientStream[Req any, Res any] struct { - ClientStream - } - - var _ ServerStreamingClient[string] = (*GenericClientStream[int, string])(nil) - var _ ClientStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) - var _ BidiStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) - - // Send pushes one message into the stream of requests to be consumed by the - // server. The type of message which can be sent is determined by the Req type - // parameter of the GenericClientStream receiver. - func (x *GenericClientStream[Req, Res]) Send(m *Req) error { - return x.ClientStream.SendMsg(m) - } - - // Recv reads one message from the stream of responses generated by the server. - // The type of the message returned is determined by the Res type parameter - // of the GenericClientStream receiver. - func (x *GenericClientStream[Req, Res]) Recv() (*Res, error) { - m := new(Res) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil - } - - // CloseAndRecv closes the sending side of the stream, then receives the unary - // response from the server. The type of message which it returns is determined - // by the Res type parameter of the GenericClientStream receiver. - func (x *GenericClientStream[Req, Res]) CloseAndRecv() (*Res, error) { - if err := x.ClientStream.CloseSend(); err != nil { - return nil, err - } - m := new(Res) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil - } - - // GenericServerStream implements the ServerStreamingServer, ClientStreamingServer, - // and BidiStreamingServer interfaces. It is used in generated code. - type GenericServerStream[Req any, Res any] struct { - ServerStream - } - - var _ ServerStreamingServer[string] = (*GenericServerStream[int, string])(nil) - var _ ClientStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) - var _ BidiStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) - - // Send pushes one message into the stream of responses to be consumed by the - // client. The type of message which can be sent is determined by the Res - // type parameter of the serverStreamServer receiver. - func (x *GenericServerStream[Req, Res]) Send(m *Res) error { - return x.ServerStream.SendMsg(m) - } - - // SendAndClose pushes the unary response to the client. The type of message - // which can be sent is determined by the Res type parameter of the - // clientStreamServer receiver. - func (x *GenericServerStream[Req, Res]) SendAndClose(m *Res) error { - return x.ServerStream.SendMsg(m) - } - - // Recv reads one message from the stream of requests generated by the client. - // The type of the message returned is determined by the Req type parameter - // of the clientStreamServer receiver. - func (x *GenericServerStream[Req, Res]) Recv() (*Req, error) { - m := new(Req) - if err := x.ServerStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil - } +package grpc + +// ServerStreamingClient represents the client side of a server-streaming (one +// request, many responses) RPC. It is generic over the type of the response +// message. It is used in generated code. +type ServerStreamingClient[Res any] interface { + // Recv receives the next response message from the server. The client may + // repeatedly call Recv to read messages from the response stream. If + // io.EOF is returned, the stream has terminated with an OK status. Any + // other error is compatible with the status package and indicates the + // RPC's status code and message. + Recv() (*Res, error) + + // ClientStream is embedded to provide Context, Header, and Trailer + // functionality. No other methods in the ClientStream should be called + // directly. + ClientStream +} + +// ServerStreamingServer represents the server side of a server-streaming (one +// request, many responses) RPC. It is generic over the type of the response +// message. It is used in generated code. +// +// To terminate the response stream, return from the handler method and return +// an error from the status package, or use nil to indicate an OK status code. +type ServerStreamingServer[Res any] interface { + // Send sends a response message to the client. The server handler may + // call Send multiple times to send multiple messages to the client. An + // error is returned if the stream was terminated unexpectedly, and the + // handler method should return, as the stream is no longer usable. + Send(*Res) error + + // ServerStream is embedded to provide Context, SetHeader, SendHeader, and + // SetTrailer functionality. No other methods in the ServerStream should + // be called directly. + ServerStream +} + +// ClientStreamingClient represents the client side of a client-streaming (many +// requests, one response) RPC. It is generic over both the type of the request +// message stream and the type of the unary response message. It is used in +// generated code. +type ClientStreamingClient[Req any, Res any] interface { + // Send sends a request message to the server. The client may call Send + // multiple times to send multiple messages to the server. On error, Send + // aborts the stream. If the error was generated by the client, the status + // is returned directly. Otherwise, io.EOF is returned, and the status of + // the stream may be discovered using CloseAndRecv(). + Send(*Req) error + + // CloseAndRecv closes the request stream and waits for the server's + // response. This method must be called once and only once after sending + // all request messages. Any error returned is implemented by the status + // package. + CloseAndRecv() (*Res, error) + + // ClientStream is embedded to provide Context, Header, and Trailer + // functionality. No other methods in the ClientStream should be called + // directly. + ClientStream +} + +// ClientStreamingServer represents the server side of a client-streaming (many +// requests, one response) RPC. It is generic over both the type of the request +// message stream and the type of the unary response message. It is used in +// generated code. +type ClientStreamingServer[Req any, Res any] interface { + // Recv receives the next request message from the client. The server may + // repeatedly call Recv to read messages from the request stream. If + // io.EOF is returned, it indicates the client called CloseAndRecv on its + // ClientStreamingClient. Any other error indicates the stream was + // terminated unexpectedly, and the handler method should return, as the + // stream is no longer usable. + Recv() (*Req, error) + + // SendAndClose sends a single response message to the client and closes + // the stream. This method must be called once and only once after all + // request messages have been processed. Recv should not be called after + // calling SendAndClose. + SendAndClose(*Res) error + + // ServerStream is embedded to provide Context, SetHeader, SendHeader, and + // SetTrailer functionality. No other methods in the ServerStream should + // be called directly. + ServerStream +} + +// BidiStreamingClient represents the client side of a bidirectional-streaming +// (many requests, many responses) RPC. It is generic over both the type of the +// request message stream and the type of the response message stream. It is +// used in generated code. +type BidiStreamingClient[Req any, Res any] interface { + // Send sends a request message to the server. The client may call Send + // multiple times to send multiple messages to the server. On error, Send + // aborts the stream. If the error was generated by the client, the status + // is returned directly. Otherwise, io.EOF is returned, and the status of + // the stream may be discovered using Recv(). + Send(*Req) error + + // Recv receives the next response message from the server. The client may + // repeatedly call Recv to read messages from the response stream. If + // io.EOF is returned, the stream has terminated with an OK status. Any + // other error is compatible with the status package and indicates the + // RPC's status code and message. + Recv() (*Res, error) + + // ClientStream is embedded to provide Context, Header, Trailer, and + // CloseSend functionality. No other methods in the ClientStream should be + // called directly. + ClientStream +} + +// BidiStreamingServer represents the server side of a bidirectional-streaming +// (many requests, many responses) RPC. It is generic over both the type of the +// request message stream and the type of the response message stream. It is +// used in generated code. +type BidiStreamingServer[Req any, Res any] interface { + // Recv receives the next request message from the client. The server may + // repeatedly call Recv to read messages from the request stream. If + // io.EOF is returned, it indicates the client called CloseSend on its + // BidiStreamingClient. Any other error indicates the stream was + // terminated unexpectedly, and the handler method should return, as the + // stream is no longer usable. + Recv() (*Req, error) + + // Send sends a response message to the client. The server handler may + // call Send multiple times to send multiple messages to the client. An + // error is returned if the stream was terminated unexpectedly, and the + // handler method should return, as the stream is no longer usable. + Send(*Res) error + + // ServerStream is embedded to provide Context, SetHeader, SendHeader, and + // SetTrailer functionality. No other methods in the ServerStream should + // be called directly. + ServerStream +} + +// GenericClientStream implements the ServerStreamingClient, ClientStreamingClient, +// and BidiStreamingClient interfaces. It is used in generated code. +type GenericClientStream[Req any, Res any] struct { + ClientStream +} + +var _ ServerStreamingClient[string] = (*GenericClientStream[int, string])(nil) +var _ ClientStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) +var _ BidiStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) + +// Send pushes one message into the stream of requests to be consumed by the +// server. The type of message which can be sent is determined by the Req type +// parameter of the GenericClientStream receiver. +func (x *GenericClientStream[Req, Res]) Send(m *Req) error { + return x.ClientStream.SendMsg(m) +} + +// Recv reads one message from the stream of responses generated by the server. +// The type of the message returned is determined by the Res type parameter +// of the GenericClientStream receiver. +func (x *GenericClientStream[Req, Res]) Recv() (*Res, error) { + m := new(Res) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + +// CloseAndRecv closes the sending side of the stream, then receives the unary +// response from the server. The type of message which it returns is determined +// by the Res type parameter of the GenericClientStream receiver. +func (x *GenericClientStream[Req, Res]) CloseAndRecv() (*Res, error) { + if err := x.ClientStream.CloseSend(); err != nil { + return nil, err + } + m := new(Res) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + +// GenericServerStream implements the ServerStreamingServer, ClientStreamingServer, +// and BidiStreamingServer interfaces. It is used in generated code. +type GenericServerStream[Req any, Res any] struct { + ServerStream +} + +var _ ServerStreamingServer[string] = (*GenericServerStream[int, string])(nil) +var _ ClientStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) +var _ BidiStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) + +// Send pushes one message into the stream of responses to be consumed by the +// client. The type of message which can be sent is determined by the Res +// type parameter of the serverStreamServer receiver. +func (x *GenericServerStream[Req, Res]) Send(m *Res) error { + return x.ServerStream.SendMsg(m) +} + +// SendAndClose pushes the unary response to the client. The type of message +// which can be sent is determined by the Res type parameter of the +// clientStreamServer receiver. +func (x *GenericServerStream[Req, Res]) SendAndClose(m *Res) error { + return x.ServerStream.SendMsg(m) +} + +// Recv reads one message from the stream of requests generated by the client. +// The type of the message returned is determined by the Req type parameter +// of the clientStreamServer receiver. +func (x *GenericServerStream[Req, Res]) Recv() (*Req, error) { + m := new(Req) + if err := x.ServerStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} From 6649fa67bf294741f662e73006a89c0fc2a416ed Mon Sep 17 00:00:00 2001 From: Doug Fawley Date: Wed, 28 Aug 2024 14:32:14 -0700 Subject: [PATCH 23/23] Add comment to explain how to close streams from Servers --- stream_interfaces.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/stream_interfaces.go b/stream_interfaces.go index 0d809763b389..0037fee0bd71 100644 --- a/stream_interfaces.go +++ b/stream_interfaces.go @@ -82,6 +82,10 @@ type ClientStreamingClient[Req any, Res any] interface { // requests, one response) RPC. It is generic over both the type of the request // message stream and the type of the unary response message. It is used in // generated code. +// +// To terminate the RPC, call SendAndClose and return nil from the method +// handler or do not call SendAndClose and return an error from the status +// package. type ClientStreamingServer[Req any, Res any] interface { // Recv receives the next request message from the client. The server may // repeatedly call Recv to read messages from the request stream. If @@ -132,6 +136,9 @@ type BidiStreamingClient[Req any, Res any] interface { // (many requests, many responses) RPC. It is generic over both the type of the // request message stream and the type of the response message stream. It is // used in generated code. +// +// To terminate the stream, return from the handler method and return +// an error from the status package, or use nil to indicate an OK status code. type BidiStreamingServer[Req any, Res any] interface { // Recv receives the next request message from the client. The server may // repeatedly call Recv to read messages from the request stream. If