Skip to content

Commit

Permalink
Go: Disable keep alive for gRPC channel (#481)
Browse files Browse the repository at this point in the history
*Go: Disable keep alive for gRPC channel
  • Loading branch information
lfz757077613 committed Apr 18, 2023
1 parent ed2ba2e commit ae289ea
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 46 deletions.
8 changes: 0 additions & 8 deletions golang/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
validator "github.com/go-playground/validator/v10"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)

var (
Expand Down Expand Up @@ -107,13 +106,6 @@ func (c *clientConn) Close() error {
}

func (c *clientConn) dialSetupOpts(dopts ...grpc.DialOption) (opts []grpc.DialOption, err error) {
if c.opts.DialKeepAliveTime > 0 {
opts = append(opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: c.opts.DialKeepAliveTime,
Timeout: c.opts.DialKeepAliveTimeout,
PermitWithoutStream: c.opts.PermitWithoutStream,
}))
}
opts = append(opts, dopts...)
if c.creds != nil {
opts = append(opts, grpc.WithTransportCredentials(c.creds))
Expand Down
38 changes: 0 additions & 38 deletions golang/conn_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ type connOptions struct {
// other operations that do not have an explicit context.
Context context.Context

// DialKeepAliveTime is the time after which client pings the server to see if
// transport is alive.
DialKeepAliveTime time.Duration

// DialKeepAliveTimeout is the time that the client waits for a response for the
// keep-alive probe. If the response is not received in this time, the connection is closed.
DialKeepAliveTimeout time.Duration

// PermitWithoutStream when set will allow client to send keepalive pings to server without any active streams(RPCs).
PermitWithoutStream bool

// DialTimeout is the timeout for failing to establish a connection.
DialTimeout time.Duration

Expand All @@ -79,7 +68,6 @@ var defaultConnOptions = connOptions{
RootCAs: x509.NewCertPool(),
InsecureSkipVerify: true,
},
DialKeepAliveTime: time.Second * 30,
Logger: zaplog.New(),
}

Expand Down Expand Up @@ -157,32 +145,6 @@ func WithContext(ctx context.Context) ConnOption {
})
}

// WithDialKeepAliveTime returns a ConnOption that sets DialKeepAliveTime for grpc.DialContext.
// DialKeepAliveTime is the time after which client pings the server to see if transport is alive.
func WithDialKeepAliveTime(d time.Duration) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.DialKeepAliveTime = d
})
}

// WithDialKeepAliveTimeout returns a ConnOption that sets DialKeepAliveTimeout for grpc.DialContext.
// DialKeepAliveTimeout is the time that the client waits for a response for the keep-alive probe.
// If the response is not received in this time, the connection is closed.
func WithDialKeepAliveTimeout(d time.Duration) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.DialKeepAliveTimeout = d
})
}

// WithPermitWithoutStream returns a ConnOption that sets PermitWithoutStream for grpc.DialContext.
// PermitWithoutStream when set will allow client to send keepalive pings to server without any
// active streams(RPCs).
func WithPermitWithoutStream(permit bool) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.PermitWithoutStream = permit
})
}

func WithZapLogger(logger *zap.Logger) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.Logger = logger
Expand Down

0 comments on commit ae289ea

Please sign in to comment.