Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit aaeb633

Browse files
mechpenJoseph-Irving
authored andcommitted
enable gRPC keepalive to detect dead TCP connections (#217) (#337)
When a TCP connection dies, i.e. no more packet from peer, gRPC does not close the connection by default. We need to enable gRPC keepalive to detect and close dead TCP connections.
1 parent 44207ae commit aaeb633

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

cmd/kiam/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (opts *agentCommand) Run() {
8282
ctxGateway, cancelCtxGateway := context.WithTimeout(context.Background(), opts.timeoutKiamGateway)
8383
defer cancelCtxGateway()
8484

85-
gateway, err := kiamserver.NewGateway(ctxGateway, opts.serverAddress, opts.caPath, opts.certificatePath, opts.keyPath)
85+
gateway, err := kiamserver.NewGateway(ctxGateway, opts.serverAddress, opts.caPath, opts.certificatePath, opts.keyPath, opts.keepaliveParams)
8686
if err != nil {
8787
log.Fatalf("error creating server gateway: %s", err.Error())
8888
}

cmd/kiam/health.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (opts *healthCommand) Run() {
4343
ctxGateway, cancelCtxGateway := context.WithTimeout(context.Background(), opts.timeoutKiamGateway)
4444
defer cancelCtxGateway()
4545

46-
gateway, err := kiamserver.NewGateway(ctxGateway, opts.serverAddress, opts.caPath, opts.certificatePath, opts.keyPath)
46+
gateway, err := kiamserver.NewGateway(ctxGateway, opts.serverAddress, opts.caPath, opts.certificatePath, opts.keyPath, opts.keepaliveParams)
4747
if err != nil {
4848
log.Fatalf("error creating server gateway: %s", err.Error())
4949
}

cmd/kiam/opts.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"context"
1818
"fmt"
1919
log "github.com/sirupsen/logrus"
20+
"google.golang.org/grpc/keepalive"
2021
"github.com/uswitch/kiam/pkg/pprof"
2122
"github.com/uswitch/kiam/pkg/prometheus"
2223
"github.com/uswitch/kiam/pkg/statsd"
@@ -109,9 +110,13 @@ type clientOptions struct {
109110
serverAddress string
110111
serverAddressRefresh time.Duration
111112
timeoutKiamGateway time.Duration
113+
keepaliveParams keepalive.ClientParameters
112114
}
113115

114116
func (o *clientOptions) bind(parser parser) {
117+
parser.Flag("grpc-keepalive-time-ms", "gRPC keepalive time").Default("10s").DurationVar(&o.keepaliveParams.Time)
118+
parser.Flag("grpc-keepalive-timeout-ms", "gRPC keepalive timeout").Default("2s").DurationVar(&o.keepaliveParams.Timeout)
119+
parser.Flag("grpc-keepalive-permit-without-stream", "gRPC keepalive ping even with no RPC").BoolVar(&o.keepaliveParams.PermitWithoutStream)
115120
parser.Flag("server-address", "gRPC address to Kiam server service").Default("localhost:9610").StringVar(&o.serverAddress)
116121
parser.Flag("server-address-refresh", "Interval to refresh server service endpoints ( deprecated )").Default("0s").DurationVar(&o.serverAddressRefresh)
117122
parser.Flag("gateway-timeout-creation", "Timeout to create the kiam gateway ").Default("1s").DurationVar(&o.timeoutKiamGateway)

pkg/server/gateway.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"google.golang.org/grpc"
3232
"google.golang.org/grpc/balancer/roundrobin"
3333
"google.golang.org/grpc/credentials"
34+
"google.golang.org/grpc/keepalive"
3435

3536
status "google.golang.org/grpc/status"
3637
)
@@ -53,7 +54,7 @@ const (
5354
)
5455

5556
// NewGateway constructs a gRPC client to talk to the server
56-
func NewGateway(ctx context.Context, address string, caFile, certificateFile, keyFile string) (*KiamGateway, error) {
57+
func NewGateway(ctx context.Context, address string, caFile, certificateFile, keyFile string, keepaliveParams keepalive.ClientParameters) (*KiamGateway, error) {
5758
callOpts := []retry.CallOption{
5859
retry.WithBackoff(retry.BackoffLinear(RetryInterval)),
5960
}
@@ -85,6 +86,7 @@ func NewGateway(ctx context.Context, address string, caFile, certificateFile, ke
8586
dialAddress := fmt.Sprintf("dns:///%s", address)
8687

8788
dialOpts := []grpc.DialOption{
89+
grpc.WithKeepaliveParams(keepaliveParams),
8890
grpc.WithTransportCredentials(creds),
8991
grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(retry.UnaryClientInterceptor(callOpts...), grpc_prometheus.UnaryClientInterceptor)),
9092
grpc.WithBalancerName(roundrobin.Name),

0 commit comments

Comments
 (0)