diff --git a/clientv3/client.go b/clientv3/client.go index a0e059d5e35c..e2aadbf2b0d3 100644 --- a/clientv3/client.go +++ b/clientv3/client.go @@ -31,6 +31,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" + "google.golang.org/grpc/keepalive" "google.golang.org/grpc/metadata" ) @@ -215,6 +216,13 @@ func (c *Client) dialSetupOpts(endpoint string, dopts ...grpc.DialOption) (opts if c.cfg.DialTimeout > 0 { opts = []grpc.DialOption{grpc.WithTimeout(c.cfg.DialTimeout)} } + if c.cfg.DialKeepAlive > 0 { + opts = append(opts, grpc.DialOption{ + grpc.WithKeepaliveParams(keepalive.ClientParameters{ + Time: c.cfg.DialKeepAlive * time.Second, + Timeout: c.cfg.DialKeepAlive * time.Second, + })}) + } opts = append(opts, dopts...) f := func(host string, t time.Duration) (net.Conn, error) { diff --git a/clientv3/config.go b/clientv3/config.go index dda72a748e61..8f5b9a2d0e10 100644 --- a/clientv3/config.go +++ b/clientv3/config.go @@ -33,6 +33,10 @@ type Config struct { // DialTimeout is the timeout for failing to establish a connection. DialTimeout time.Duration `json:"dial-timeout"` + // DialKeepAlive is the time after which client pings the server to see if transport is + // alive. It waits for this duration for a response before closing the connection. + DialKeepAlive time.Duration `json:"dial-keep-alive"` + // TLS holds the client secure credentials, if any. TLS *tls.Config