Skip to content

Commit

Permalink
fix: add grpc keep_alive (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mryashbhardwaj authored Sep 11, 2023
1 parent e3a56ad commit 377842b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client/cmd/internal/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"

"github.com/goto/optimus/config"
)
Expand Down Expand Up @@ -72,6 +73,11 @@ func defaultDialOptions() []grpc.DialOption {
otelgrpc.StreamClientInterceptor(),
grpc_prometheus.StreamClientInterceptor,
)),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: time.Minute, // send pings every 1 Minute if there is no activity
Timeout: 1 * time.Second, // wait 1 second for ping ack before considering the connection dead
PermitWithoutStream: true, // send pings even without active streams
}),
)
return opts
}
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -92,6 +93,10 @@ func setupGRPCServer(l log.Logger) (*grpc.Server, error) {
),
grpc.MaxRecvMsgSize(GRPCMaxRecvMsgSize),
grpc.MaxSendMsgSize(GRPCMaxSendMsgSize),
grpc.KeepaliveParams(keepalive.ServerParameters{
Time: 1 * time.Minute, // Ping the client if it is idle for 1 minute to ensure the connection is still active
Timeout: 1 * time.Second, // Wait 1 second for the ping ack before assuming the connection is dead
}),
}
grpcServer := grpc.NewServer(grpcOpts...)

Expand Down

0 comments on commit 377842b

Please sign in to comment.