From 7752404888c17e6a099ce57a841a84cc86e32634 Mon Sep 17 00:00:00 2001 From: jeffyanta Date: Mon, 29 Jun 2026 08:50:45 -0400 Subject: [PATCH] Setup gRPC app keep alive settings --- grpc/app/app.go | 17 +++++++++++++---- grpc/app/config.go | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/grpc/app/app.go b/grpc/app/app.go index 4247e8d..36e289e 100644 --- a/grpc/app/app.go +++ b/grpc/app/app.go @@ -24,6 +24,7 @@ import ( "google.golang.org/grpc/credentials" "google.golang.org/grpc/health" health_grpc "google.golang.org/grpc/health/grpc_health_v1" + "google.golang.org/grpc/keepalive" "github.com/code-payments/ocp-server/grpc/client" "github.com/code-payments/ocp-server/grpc/headers" @@ -261,15 +262,23 @@ func Run(app App, options ...Option) error { os.Exit(1) } - secureServ := grpc.NewServer( + keepaliveOpts := []grpc.ServerOption{ + grpc.KeepaliveParams(keepalive.ServerParameters{ + MaxConnectionIdle: 15 * time.Minute, + MaxConnectionAge: 30 * time.Minute, + MaxConnectionAgeGrace: config.ShutdownGracePeriod / 2, + }), + } + + secureServ := grpc.NewServer(append([]grpc.ServerOption{ grpc.Creds(transportCreds), grpc_middleware.WithUnaryServerChain(opts.unaryServerInterceptors...), grpc_middleware.WithStreamServerChain(opts.streamServerInterceptors...), - ) - insecureServ := grpc.NewServer( + }, keepaliveOpts...)...) + insecureServ := grpc.NewServer(append([]grpc.ServerOption{ grpc_middleware.WithUnaryServerChain(opts.unaryServerInterceptors...), grpc_middleware.WithStreamServerChain(opts.streamServerInterceptors...), - ) + }, keepaliveOpts...)...) app.RegisterWithGRPC(secureServ) app.RegisterWithGRPC(insecureServ) diff --git a/grpc/app/config.go b/grpc/app/config.go index ba5a9e8..ae64b6e 100644 --- a/grpc/app/config.go +++ b/grpc/app/config.go @@ -68,7 +68,7 @@ var defaultConfig = BaseConfig{ InsecureListenAddress: "localhost:8086", DebugListenAddress: ":8123", - ShutdownGracePeriod: 30 * time.Second, + ShutdownGracePeriod: 60 * time.Second, EnablePprof: true, EnableExpvar: true,