Skip to content

Commit

Permalink
allow configuring grpc max connection age
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Jul 19, 2024
1 parent d42ea8f commit 0de8511
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/add-grpc-max-connection-age-env.md
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions pkg/rgrpc/keepalive.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions pkg/rgrpc/rgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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...)

Expand Down

0 comments on commit 0de8511

Please sign in to comment.