Skip to content

Commit

Permalink
Enabled keep alive by default (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns authored Nov 30, 2023
1 parent 288a04f commit 89c8dba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
13 changes: 8 additions & 5 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,23 +491,26 @@ type (
// This value only used when TLS is nil.
Authority string

// Enables keep alive ping from client to the server, which can help detect abruptly closed connections faster.
EnableKeepAliveCheck bool
// Disable keep alive ping from client to the server.
DisableKeepAliveCheck bool

// After a duration of this time if the client doesn't see any activity it
// pings the server to see if the transport is still alive.
// If set below 10s, a minimum value of 10s will be used instead.
// default: 15s
KeepAliveTime time.Duration

// After having pinged for keepalive check, the client waits for a duration
// of Timeout and if no activity is seen even after that the connection is
// closed.
// default: 30s
KeepAliveTimeout time.Duration

// If true, client sends keepalive pings even with no active RPCs. If false,
// when there are no active RPCs, Time and Timeout will be ignored and no
// if true, when there are no active RPCs, Time and Timeout will be ignored and no
// keepalive pings will be sent.
KeepAlivePermitWithoutStream bool
// If false, client sends keepalive pings even with no active RPCs
// default: false
DisableKeepAlivePermitWithoutStream bool

// MaxPayloadSize is a number of bytes that gRPC would allow to travel to and from server. Defaults to 128 MB.
MaxPayloadSize int
Expand Down
26 changes: 21 additions & 5 deletions internal/grpc_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"context"
"time"

"google.golang.org/grpc/status"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"go.temporal.io/api/serviceerror"
"go.temporal.io/sdk/internal/common/metrics"
Expand All @@ -40,6 +39,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

type (
Expand All @@ -64,10 +64,18 @@ const (

// attemptSuffix is a suffix added to the metric name for individual call attempts made to the server, which includes retries.
attemptSuffix = "_attempt"

// mb is a number of bytes in a megabyte
mb = 1024 * 1024

// defaultMaxPayloadSize is a maximum size of the payload that grpc client would allow.
defaultMaxPayloadSize = 128 * mb

// defaultKeepAliveTime is the keep alive time if one is not specified.
defaultKeepAliveTime = 15 * time.Second

// defaultKeepAliveTimeout is the keep alive timeout if one is not specified.
defaultKeepAliveTimeout = 30 * time.Second
)

func dial(params dialParameters) (*grpc.ClientConn, error) {
Expand Down Expand Up @@ -110,14 +118,22 @@ func dial(params dialParameters) (*grpc.ClientConn, error) {
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(maxPayloadSize)))
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxPayloadSize)))

if params.UserConnectionOptions.EnableKeepAliveCheck {
if !params.UserConnectionOptions.DisableKeepAliveCheck {
// gRPC utilizes keep alive mechanism to detect dead connections in case if server didn't close them
// gracefully. Client would ping the server periodically and expect replies withing the specified timeout.
// Learn more by reading https://github.com/grpc/grpc/blob/master/doc/keepalive.md
keepAliveTime := params.UserConnectionOptions.KeepAliveTime
if keepAliveTime == 0 {
keepAliveTime = defaultKeepAliveTime
}
keepAliveTimeout := params.UserConnectionOptions.KeepAliveTimeout
if keepAliveTimeout == 0 {
keepAliveTimeout = defaultKeepAliveTimeout
}
var kap = keepalive.ClientParameters{
Time: params.UserConnectionOptions.KeepAliveTime,
Timeout: params.UserConnectionOptions.KeepAliveTimeout,
PermitWithoutStream: params.UserConnectionOptions.KeepAlivePermitWithoutStream,
Time: keepAliveTime,
Timeout: keepAliveTimeout,
PermitWithoutStream: !params.UserConnectionOptions.DisableKeepAlivePermitWithoutStream,
}
opts = append(opts, grpc.WithKeepaliveParams(kap))
}
Expand Down

0 comments on commit 89c8dba

Please sign in to comment.