config+lnd: allow configuring gRPC keepalive settings

This change allows users to customize the gRPC keepalive settings. The
server ping values configure when the server sends out a ping by itself
and how quickly the client is expected to respond.
The client ping min wait configures the minimum time a client has to
wait before sending another ping. A connection might be closed by the
server if a client pings more frequently than the allowed min wait time.

We choose lower default values than the gRPC library used before:
 - ServerPingTime: 1 minute instead of 2 hours
 - ClientPingMinWait: 5 seconds instead of 5 minutes

This should by itself already make long-standing gRPC streams more
stable but also allow clients to set a custom client ping time of 5
seconds or more (a value of slightly more than 5000 milliseconds should
be chosen to avoid the server disconnecting due to slight timing skews).
This commit is contained in:
Oliver Gugger
2023-05-25 14:09:14 +02:00
parent 0005adaac8
commit f9f8f6406b
2 changed files with 65 additions and 0 deletions

14
lnd.go
View File

@@ -37,6 +37,7 @@ import (
"github.com/lightningnetwork/lnd/watchtower"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon.v2"
)
@@ -303,10 +304,23 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
}
}()
// Allow the user to overwrite some defaults of the gRPC library related
// to connection keepalive (server side and client side pings).
serverKeepalive := keepalive.ServerParameters{
Time: cfg.GRPC.ServerPingTime,
Timeout: cfg.GRPC.ServerPingTimeout,
}
clientKeepalive := keepalive.EnforcementPolicy{
MinTime: cfg.GRPC.ClientPingMinWait,
PermitWithoutStream: cfg.GRPC.ClientAllowPingWithoutStream,
}
rpcServerOpts := interceptorChain.CreateServerOpts()
serverOpts = append(serverOpts, rpcServerOpts...)
serverOpts = append(
serverOpts, grpc.MaxRecvMsgSize(lnrpc.MaxGrpcMsgSize),
grpc.KeepaliveParams(serverKeepalive),
grpc.KeepaliveEnforcementPolicy(clientKeepalive),
)
grpcServer := grpc.NewServer(serverOpts...)