From 3e5bf86b1891598c7299cffe208682702f69b2a9 Mon Sep 17 00:00:00 2001 From: Jayden Teoh Date: Wed, 11 Oct 2023 23:55:52 +0800 Subject: [PATCH] minor changes to documentation --- dialoptions.go | 4 ++-- examples/helloworld/greeter_server/main.go | 6 ++---- internal/transport/http2_client.go | 1 + server.go | 9 ++++----- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/dialoptions.go b/dialoptions.go index eeaae43eb3a1..4e3f728fb7c2 100644 --- a/dialoptions.go +++ b/dialoptions.go @@ -414,8 +414,8 @@ func WithTimeout(d time.Duration) DialOption { // returned by f, gRPC checks the error's Temporary() method to decide if it // should try to reconnect to the network address. // -// Note: Go overrides the OS defaults for TCP keepalive interval to 15s. -// To retain the OS defaults, pass in a custom dialer with KeepAlive set to a negative duration. +// Note: Go overrides the OS defaults for TCP keepalive time and interval to 15s. +// To retain OS defaults, use a net.Dialer with the KeepAlive field set to a negative value. func WithContextDialer(f func(context.Context, string) (net.Conn, error)) DialOption { return newFuncDialOption(func(o *dialOptions) { o.copts.Dialer = f diff --git a/examples/helloworld/greeter_server/main.go b/examples/helloworld/greeter_server/main.go index 08d8e058acf6..08b623d93300 100644 --- a/examples/helloworld/greeter_server/main.go +++ b/examples/helloworld/greeter_server/main.go @@ -48,10 +48,8 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe func main() { flag.Parse() - // ListenConfig allows options to be passed to the listener. - var lc net.ListenConfig - // Setting KeepAlive to -1 allows the listener to retain OS default TCP keep-alive interval. - lc.KeepAlive = time.Duration(-1) + // Setting KeepAlive to -1 allows the listener to retain OS default TCP keepalive time and interval. + lc := net.ListenConfig{KeepAlive: time.Duration(-1)} lis, err := lc.Listen(context.Background(), "tcp", fmt.Sprintf(":%d", *port)) if err != nil { log.Fatalf("failed to listen: %v", err) diff --git a/internal/transport/http2_client.go b/internal/transport/http2_client.go index 6d142d7f3362..ec72b2d797fb 100644 --- a/internal/transport/http2_client.go +++ b/internal/transport/http2_client.go @@ -176,6 +176,7 @@ func dial(ctx context.Context, fn func(context.Context, string) (net.Conn, error if networkType == "tcp" && useProxy { return proxyDial(ctx, address, grpcUA) } + // KeepAlive is set to a negative value to prevent Go's override of the TCP keepalive time and interval; retain the OS default. return (&net.Dialer{KeepAlive: time.Duration(-1)}).DialContext(ctx, networkType, address) } diff --git a/server.go b/server.go index 6e1b3557a59d..de0feeb7b99d 100644 --- a/server.go +++ b/server.go @@ -814,11 +814,10 @@ func (l *listenSocket) Close() error { // this method returns. // Serve will return a non-nil error unless Stop or GracefulStop is called. // -// Note: Go overrides the OS defaults for TCP keepalive interval to 15s. -// To retain the OS defaults, you will need to create a net.ListenConfig with the -// KeepAlive parameter set to a negative value and use the Listen method on it to create -// the net.Listener to pass to this function. -// See https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_client/main.go#L51 +// Note: Go overrides OS defaults for TCP keepalive time and interval to 15s. +// To retain OS defaults, pass a net.Listener created by calling the Listen method +// on a net.ListenConfig with the `KeepAlive` field set to a negative value. See +// helloworld/greeter_server/main.go for an example. func (s *Server) Serve(lis net.Listener) error { s.mu.Lock() s.printf("serving")