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 e688ef3 commit fa2c1ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 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/owncloud/ocis/pull/9657
9 changes: 7 additions & 2 deletions ocis-pkg/service/grpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"go-micro.dev/v4"
"go-micro.dev/v4/client"
"go-micro.dev/v4/server"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)

// Service simply wraps the go-micro grpc service.
Expand All @@ -24,6 +26,9 @@ type Service struct {
func NewServiceWithClient(client client.Client, opts ...Option) (Service, error) {
var mServer server.Server
sopts := newOptions(opts...)
keepaliveParams := grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionAge: GetMaxConnectionAge(), // this forces clients to reconnect after 30 seconds, triggering a new DNS lookup to pick up new IPs
})
tlsConfig := &tls.Config{}
if sopts.TLSEnabled {
var cert tls.Certificate
Expand All @@ -43,9 +48,9 @@ func NewServiceWithClient(client client.Client, opts ...Option) (Service, error)
}
}
tlsConfig.Certificates = []tls.Certificate{cert}
mServer = mgrpcs.NewServer(mgrpcs.AuthTLS(tlsConfig))
mServer = mgrpcs.NewServer(mgrpcs.Options(keepaliveParams), mgrpcs.AuthTLS(tlsConfig))
} else {
mServer = mgrpcs.NewServer()
mServer = mgrpcs.NewServer(mgrpcs.Options(keepaliveParams))
}

mopts := []micro.Option{
Expand Down

0 comments on commit fa2c1ff

Please sign in to comment.