Skip to content

Commit

Permalink
minor changes to documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
JaydenTeoh committed Oct 11, 2023
1 parent 9fe3359 commit 3e5bf86
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
4 changes: 2 additions & 2 deletions dialoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions examples/helloworld/greeter_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
9 changes: 4 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3e5bf86

Please sign in to comment.