[ccp] set keepalive on GRPC connection
This will stop the connection being silently dropped. GCP load balancers
will drop connections that have been inactive for around 10 minutes.
[fixes
@@ -5,7 +5,9 @@ import (
"fmt"
"html/template"
"log"
+ "net"
"os"
+ "time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
@@ -97,8 +99,11 @@ func main() {
RootCAs: rootCertPool,
})
- dialOption := grpc.WithTransportCredentials(transportCreds)
- conn, err := grpc.Dial(serverAddr, dialOption)
+ conn, err := grpc.Dial(
+ serverAddr,
+ grpc.WithTransportCredentials(transportCreds),
+ grpc.WithDialer(keepAliveDial),
+ )
if err != nil {
log.Fatalf("failed to create handler: %s", err.Error())
}
@@ -123,3 +128,11 @@ func main() {
logger.Error("running-server-failed", err)
}
}
+
+func keepAliveDial(addr string, timeout time.Duration) (net.Conn, error) {
+ d := net.Dialer{
+ Timeout: timeout,
+ KeepAlive: 60 * time.Second,
+ }
+ return d.Dial("tcp", addr)
+}
