From 0de85119e8a8a5640b887f96b2640c7268b29a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 19 Jul 2024 16:12:17 +0200 Subject: [PATCH] allow configuring grpc max connection age MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- .../add-grpc-max-connection-age-env.md | 5 ++++ pkg/rgrpc/keepalive.go | 24 +++++++++++++++++++ pkg/rgrpc/rgrpc.go | 4 ++++ 3 files changed, 33 insertions(+) create mode 100644 changelog/unreleased/add-grpc-max-connection-age-env.md create mode 100644 pkg/rgrpc/keepalive.go diff --git a/changelog/unreleased/add-grpc-max-connection-age-env.md b/changelog/unreleased/add-grpc-max-connection-age-env.md new file mode 100644 index 0000000000..ffa8cf2ffe --- /dev/null +++ b/changelog/unreleased/add-grpc-max-connection-age-env.md @@ -0,0 +1,5 @@ +Enhancement: Allow configuring grpc max connection age + +We added a GRPC_MAX_CONNECTION_AGE env var that allows limiting the lifespan of connections. A closed connection triggers grpc clients to do a new DNS lookup to pick up new IPs. + +https://github.com/cs3org/reva/pull/4772 diff --git a/pkg/rgrpc/keepalive.go b/pkg/rgrpc/keepalive.go new file mode 100644 index 0000000000..1baab9efb2 --- /dev/null +++ b/pkg/rgrpc/keepalive.go @@ -0,0 +1,24 @@ +package rgrpc + +import ( + "math" + "os" + "time" +) + +const ( + _serverMaxConnectionAgeEnv = "GRPC_MAX_CONNECTION_AGE" + + // same default as grpc + infinity = time.Duration(math.MaxInt64) + _defaultMaxConnectionAge = infinity +) + +// GetMaxConnectionAge returns the maximum grpc connection age. +func GetMaxConnectionAge() time.Duration { + d, err := time.ParseDuration(os.Getenv(_serverMaxConnectionAgeEnv)) + if err != nil { + return _defaultMaxConnectionAge + } + return d +} diff --git a/pkg/rgrpc/rgrpc.go b/pkg/rgrpc/rgrpc.go index 1085a5e034..c04bedc3da 100644 --- a/pkg/rgrpc/rgrpc.go +++ b/pkg/rgrpc/rgrpc.go @@ -42,6 +42,7 @@ import ( "go.opentelemetry.io/otel/trace" "google.golang.org/grpc" "google.golang.org/grpc/credentials" + "google.golang.org/grpc/keepalive" "google.golang.org/grpc/reflection" ) @@ -245,6 +246,9 @@ func (s *Server) registerServices() error { if s.conf.TLSSettings.tlsConfig != nil { opts = append(opts, grpc.Creds(credentials.NewTLS(s.conf.TLSSettings.tlsConfig))) } + opts = append(opts, grpc.KeepaliveParams(keepalive.ServerParameters{ + MaxConnectionAge: GetMaxConnectionAge(), // this forces clients to reconnect after 30 seconds, triggering a new DNS lookup to pick up new IPs + })) grpcServer := grpc.NewServer(opts...)