@@ -29,6 +29,7 @@ import (
2929 "github.com/sirupsen/logrus"
3030 "google.golang.org/grpc"
3131 "google.golang.org/grpc/credentials"
32+ "google.golang.org/grpc/keepalive"
3233)
3334
3435const (
@@ -455,11 +456,44 @@ func main() {
455456 log .Fatalf ("Failed to generate TLS certificate: %v" , err )
456457 }
457458
458- // Create the gRPC server with TLS
459+ // Configure server-side keepalive parameters. These settings ensure the
460+ // server actively probes client connection health and allows long-lived
461+ // idle connections.
462+ serverKeepalive := keepalive.ServerParameters {
463+ // Ping clients after 1 minute of inactivity.
464+ Time : time .Minute ,
465+
466+ // Wait 20 seconds for ping response.
467+ Timeout : 20 * time .Second ,
468+
469+ // Allow connections to stay idle for 24 hours. The active
470+ // pinging mechanism (via Time parameter) handles health
471+ // checking, so we don't need aggressive idle timeouts.
472+ MaxConnectionIdle : time .Hour * 24 ,
473+ }
474+
475+ // Configure client keepalive enforcement policy. This tells the server
476+ // how to handle client keepalive pings.
477+ clientKeepalive := keepalive.EnforcementPolicy {
478+ // Allow client to ping even when there are no active RPCs.
479+ // This is critical for long-lived connections with infrequent
480+ // price queries.
481+ PermitWithoutStream : true ,
482+
483+ // Prevent abusive clients from pinging too frequently (DoS
484+ // protection).
485+ MinTime : 5 * time .Second ,
486+ }
487+
488+ // Create the gRPC server with TLS and keepalive configuration.
459489 transportCredentials := credentials .NewTLS (& tls.Config {
460490 Certificates : []tls.Certificate {tlsCert },
461491 })
462- backendService := grpc .NewServer (grpc .Creds (transportCredentials ))
492+ backendService := grpc .NewServer (
493+ grpc .Creds (transportCredentials ),
494+ grpc .KeepaliveParams (serverKeepalive ),
495+ grpc .KeepaliveEnforcementPolicy (clientKeepalive ),
496+ )
463497
464498 err = startService (backendService )
465499 if err != nil {
0 commit comments