Skip to content

Commit

Permalink
clientv3: add keep-alive to connection
Browse files Browse the repository at this point in the history
this makes the grpc client connection use a keep-alive.
  • Loading branch information
sakshamsharma committed Jun 30, 2017
1 parent b7cf080 commit 2e0ef6f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clientv3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions clientv3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 2e0ef6f

Please sign in to comment.