Skip to content

Commit b19a3f4

Browse files
committed
server: enhance gRPC keepalive configuration
In this commit, we enhance the server-side gRPC keepalive configuration to work in coordination with the client-side keepalive settings added in the previous commit. This completes the fix for issue #1814 by ensuring both sides of the connection actively maintain connection health. Previously, the server only configured MaxConnectionIdle set to 2 minutes, which would aggressively close idle connections. This caused problems for price oracle connections that could be idle for extended periods between RFQ operations. We now extend MaxConnectionIdle to 24 hours and add active health checking through Time and Timeout parameters. The critical addition is the EnforcementPolicy with PermitWithoutStream set to true. This allows clients to send keepalive pings even when no RPC calls are active, which is essential for long-lived connections. Without this policy, the server would reject client keepalive pings on idle connections, defeating the purpose of the client-side keepalive configuration. These settings follow the same pattern used by lnd and are based on gRPC's official keepalive recommendations. The combination of active pinging from both client and server, along with permissive policies, ensures connections remain healthy and any network issues are detected promptly rather than discovered only when the next RPC fails. Fixes #1814
1 parent a9748a9 commit b19a3f4

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

server.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,34 @@ func (s *Server) RunUntilShutdown(mainErrChan <-chan error) error {
370370
serverOpts = append(serverOpts, rpcServerOpts...)
371371
serverOpts = append(serverOpts, ServerMaxMsgReceiveSize)
372372

373-
keepAliveParams := keepalive.ServerParameters{
374-
MaxConnectionIdle: time.Minute * 2,
375-
}
376-
377-
serverOpts = append(serverOpts, grpc.KeepaliveParams(keepAliveParams))
373+
// Configure server-side keepalive parameters. These settings allow the
374+
// server to actively probe the connection health and ensure connections
375+
// stay alive during idle periods.
376+
serverKeepalive := keepalive.ServerParameters{
377+
// Ping client after 1 minute of inactivity.
378+
Time: time.Minute,
379+
// Wait 20 seconds for ping response.
380+
Timeout: 20 * time.Second,
381+
// Allow connections to remain idle for extended periods. This
382+
// is particularly important for RFQ operations where price
383+
// oracle connections may be idle for long periods.
384+
MaxConnectionIdle: time.Hour * 24,
385+
}
386+
387+
// Configure client enforcement policy. This allows clients to send
388+
// keepalive pings even when there are no active streams, which is
389+
// crucial for long-lived connections with infrequent activity.
390+
clientKeepalive := keepalive.EnforcementPolicy{
391+
// Minimum time between client pings.
392+
MinTime: 5 * time.Second,
393+
// Allow pings without active RPCs.
394+
PermitWithoutStream: true,
395+
}
396+
397+
serverOpts = append(
398+
serverOpts, grpc.KeepaliveParams(serverKeepalive),
399+
grpc.KeepaliveEnforcementPolicy(clientKeepalive),
400+
)
378401

379402
grpcServer := grpc.NewServer(serverOpts...)
380403
defer grpcServer.Stop()

0 commit comments

Comments
 (0)