Skip to content

Commit

Permalink
Merge pull request #8663 from YuleiXiao/add_keepalive_for_ctlv3
Browse files Browse the repository at this point in the history
etcdctl/v3: add keep alive time/timeout
  • Loading branch information
xiang90 authored Oct 9, 2017
2 parents 71197ab + 04940ef commit 6571829
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
4 changes: 3 additions & 1 deletion etcdctl/ctlv3/command/ep_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) {

sec := secureCfgFromCmd(cmd)
dt := dialTimeoutFromCmd(cmd)
ka := keepAliveTimeFromCmd(cmd)
kat := keepAliveTimeoutFromCmd(cmd)
auth := authCfgFromCmd(cmd)
cfgs := []*v3.Config{}
for _, ep := range endpointsFromCluster(cmd) {
cfg, err := newClientCfg([]string{ep}, dt, sec, auth)
cfg, err := newClientCfg([]string{ep}, dt, ka, kat, sec, auth)
if err != nil {
ExitWithError(ExitBadArgs, err)
}
Expand Down
38 changes: 32 additions & 6 deletions etcdctl/ctlv3/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type GlobalFlags struct {
Endpoints []string
DialTimeout time.Duration
CommandTimeOut time.Duration
KeepAliveTime time.Duration
KeepAliveTimeout time.Duration

TLS transport.TLSInfo

Expand Down Expand Up @@ -109,17 +111,21 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
if err != nil {
ExitWithError(ExitError, err)
}

dialTimeout := dialTimeoutFromCmd(cmd)
keepAliveTime := keepAliveTimeFromCmd(cmd)
keepAliveTimeout := keepAliveTimeoutFromCmd(cmd)

sec := secureCfgFromCmd(cmd)
auth := authCfgFromCmd(cmd)

initDisplayFromCmd(cmd)

return mustClient(endpoints, dialTimeout, sec, auth)
return mustClient(endpoints, dialTimeout, keepAliveTime, keepAliveTimeout, sec, auth)
}

func mustClient(endpoints []string, dialTimeout time.Duration, scfg *secureCfg, acfg *authCfg) *clientv3.Client {
cfg, err := newClientCfg(endpoints, dialTimeout, scfg, acfg)
func mustClient(endpoints []string, dialTimeout, keepAliveTime, keepAliveTimeout time.Duration, scfg *secureCfg, acfg *authCfg) *clientv3.Client {
cfg, err := newClientCfg(endpoints, dialTimeout, keepAliveTime, keepAliveTimeout, scfg, acfg)
if err != nil {
ExitWithError(ExitBadArgs, err)
}
Expand All @@ -132,7 +138,7 @@ func mustClient(endpoints []string, dialTimeout time.Duration, scfg *secureCfg,
return client
}

func newClientCfg(endpoints []string, dialTimeout time.Duration, scfg *secureCfg, acfg *authCfg) (*clientv3.Config, error) {
func newClientCfg(endpoints []string, dialTimeout, keepAliveTime, keepAliveTimeout time.Duration, scfg *secureCfg, acfg *authCfg) (*clientv3.Config, error) {
// set tls if any one tls option set
var cfgtls *transport.TLSInfo
tlsinfo := transport.TLSInfo{}
Expand All @@ -157,16 +163,20 @@ func newClientCfg(endpoints []string, dialTimeout time.Duration, scfg *secureCfg
}

cfg := &clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
Endpoints: endpoints,
DialTimeout: dialTimeout,
DialKeepAliveTime: keepAliveTime,
DialKeepAliveTimeout: keepAliveTimeout,
}

if cfgtls != nil {
clientTLS, err := cfgtls.ClientConfig()
if err != nil {
return nil, err
}
cfg.TLS = clientTLS
}

// if key/cert is not given but user wants secure connection, we
// should still setup an empty tls configuration for gRPC to setup
// secure connection.
Expand Down Expand Up @@ -207,6 +217,22 @@ func dialTimeoutFromCmd(cmd *cobra.Command) time.Duration {
return dialTimeout
}

func keepAliveTimeFromCmd(cmd *cobra.Command) time.Duration {
keepAliveTime, err := cmd.Flags().GetDuration("keepalive-time")
if err != nil {
ExitWithError(ExitError, err)
}
return keepAliveTime
}

func keepAliveTimeoutFromCmd(cmd *cobra.Command) time.Duration {
keepAliveTimeout, err := cmd.Flags().GetDuration("keepalive-timeout")
if err != nil {
ExitWithError(ExitError, err)
}
return keepAliveTimeout
}

func secureCfgFromCmd(cmd *cobra.Command) *secureCfg {
cert, key, cacert := keyAndCertFromCmd(cmd)
insecureTr := insecureTransportFromCmd(cmd)
Expand Down
4 changes: 3 additions & 1 deletion etcdctl/ctlv3/command/make_mirror_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ func makeMirrorCommandFunc(cmd *cobra.Command, args []string) {
}

dialTimeout := dialTimeoutFromCmd(cmd)
keepAliveTime := keepAliveTimeFromCmd(cmd)
keepAliveTimeout := keepAliveTimeoutFromCmd(cmd)
sec := &secureCfg{
cert: mmcert,
key: mmkey,
cacert: mmcacert,
insecureTransport: mminsecureTr,
}

dc := mustClient([]string{args[0]}, dialTimeout, sec, nil)
dc := mustClient([]string{args[0]}, dialTimeout, keepAliveTime, keepAliveTimeout, sec, nil)
c := mustClientFromCmd(cmd)

err := makeMirror(context.TODO(), c, dc)
Expand Down
8 changes: 6 additions & 2 deletions etcdctl/ctlv3/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const (
cliName = "etcdctl"
cliDescription = "A simple command line client for etcd3."

defaultDialTimeout = 2 * time.Second
defaultCommandTimeOut = 5 * time.Second
defaultDialTimeout = 2 * time.Second
defaultCommandTimeOut = 5 * time.Second
defaultKeepAliveTime = 2 * time.Second
defaultKeepAliveTimeOut = 6 * time.Second
)

var (
Expand All @@ -51,6 +53,8 @@ func init() {

rootCmd.PersistentFlags().DurationVar(&globalFlags.DialTimeout, "dial-timeout", defaultDialTimeout, "dial timeout for client connections")
rootCmd.PersistentFlags().DurationVar(&globalFlags.CommandTimeOut, "command-timeout", defaultCommandTimeOut, "timeout for short running command (excluding dial timeout)")
rootCmd.PersistentFlags().DurationVar(&globalFlags.KeepAliveTime, "keepalive-time", defaultKeepAliveTime, "keepalive time for client connections")
rootCmd.PersistentFlags().DurationVar(&globalFlags.KeepAliveTimeout, "keepalive-timeout", defaultKeepAliveTimeOut, "keepalive timeout for client connections")

// TODO: secure by default when etcd enables secure gRPC by default.
rootCmd.PersistentFlags().BoolVar(&globalFlags.Insecure, "insecure-transport", true, "disable transport security for client connections")
Expand Down

0 comments on commit 6571829

Please sign in to comment.