Skip to content

Commit 555111c

Browse files
authored
Make keep alive configurable and opt-in (#371)
1 parent eccb99a commit 555111c

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

internal/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,20 @@ type (
405405
TLS *tls.Config
406406
DisableHealthCheck bool
407407
HealthCheckTimeout time.Duration
408+
// Enables keep alive ping from client to the server, which can help detect abruptly closed connections faster.
409+
EnableKeepAliveCheck bool
410+
// After a duration of this time if the client doesn't see any activity it
411+
// pings the server to see if the transport is still alive.
412+
// If set below 10s, a minimum value of 10s will be used instead.
413+
KeepAliveTime time.Duration
414+
// After having pinged for keepalive check, the client waits for a duration
415+
// of Timeout and if no activity is seen even after that the connection is
416+
// closed.
417+
KeepAliveTimeout time.Duration
418+
// If true, client sends keepalive pings even with no active RPCs. If false,
419+
// when there are no active RPCs, Time and Timeout will be ignored and no
420+
// keepalive pings will be sent.
421+
KeepAlivePermitWithoutStream bool
408422
}
409423

410424
// StartWorkflowOptions configuration parameters for starting a workflow execution.

internal/grpc_dialer.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,25 @@ func dial(params dialParameters) (*grpc.ClientConn, error) {
7979
}
8080
cp.Backoff.BaseDelay = retryPollOperationInitialInterval
8181
cp.Backoff.MaxDelay = retryPollOperationMaxInterval
82-
83-
// gRPC utilizes keep alive mechanism to detect dead connections in case if server didn't close them
84-
// gracefully. Client would ping the server periodically and expect replies withing the specified timeout.
85-
// Learn more by reading https://github.com/grpc/grpc/blob/master/doc/keepalive.md
86-
var kap = keepalive.ClientParameters{
87-
Time: 30 * time.Second,
88-
Timeout: 15 * time.Second,
89-
PermitWithoutStream: true,
90-
}
91-
92-
return grpc.Dial(params.HostPort,
82+
opts := []grpc.DialOption{
9383
grpcSecurityOptions,
9484
grpc.WithChainUnaryInterceptor(params.RequiredInterceptors...),
9585
grpc.WithDefaultServiceConfig(params.DefaultServiceConfig),
9686
grpc.WithConnectParams(cp),
97-
grpc.WithKeepaliveParams(kap),
98-
)
87+
}
88+
89+
if params.UserConnectionOptions.EnableKeepAliveCheck {
90+
// gRPC utilizes keep alive mechanism to detect dead connections in case if server didn't close them
91+
// gracefully. Client would ping the server periodically and expect replies withing the specified timeout.
92+
// Learn more by reading https://github.com/grpc/grpc/blob/master/doc/keepalive.md
93+
var kap = keepalive.ClientParameters{
94+
Time: params.UserConnectionOptions.KeepAliveTime,
95+
Timeout: params.UserConnectionOptions.KeepAliveTimeout,
96+
PermitWithoutStream: params.UserConnectionOptions.KeepAlivePermitWithoutStream,
97+
}
98+
opts = append(opts, grpc.WithKeepaliveParams(kap))
99+
}
100+
return grpc.Dial(params.HostPort, opts...)
99101
}
100102

101103
func requiredInterceptors(metricScope tally.Scope, headersProvider HeadersProvider) []grpc.UnaryClientInterceptor {

0 commit comments

Comments
 (0)