From 3d8fe917f9c36ae4cc4d02f534706b106f02e8b6 Mon Sep 17 00:00:00 2001 From: joey Date: Tue, 16 Jul 2024 13:52:55 +0800 Subject: [PATCH] fix dial-timeout not affected for client watch command Signed-off-by: joey --- etcdctl/ctlv3/command/global.go | 23 +++++++++++++++++++++++ etcdctl/ctlv3/command/watch_command.go | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/etcdctl/ctlv3/command/global.go b/etcdctl/ctlv3/command/global.go index 93b62068af6..993522a607f 100644 --- a/etcdctl/ctlv3/command/global.go +++ b/etcdctl/ctlv3/command/global.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "go.uber.org/zap" + "google.golang.org/grpc" "google.golang.org/grpc/grpclog" "go.etcd.io/etcd/client/pkg/v3/logutil" @@ -151,6 +152,11 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client { return mustClient(cfg) } +func mustBlockClientFromCmd(cmd *cobra.Command) *clientv3.Client { + cfg := clientConfigFromCmd(cmd) + return mustBlockClient(cfg) +} + func mustClient(cc *clientv3.ConfigSpec) *clientv3.Client { lg, _ := logutil.CreateDefaultZapLogger(zap.InfoLevel) cfg, err := clientv3.NewClientConfig(cc, lg) @@ -166,6 +172,23 @@ func mustClient(cc *clientv3.ConfigSpec) *clientv3.Client { return client } +// mustBlockClient same as mustClient but with grpc.WithBlock dial option, detail see #18335 +func mustBlockClient(cc *clientv3.ConfigSpec) *clientv3.Client { + lg, _ := logutil.CreateDefaultZapLogger(zap.InfoLevel) + cfg, err := clientv3.NewClientConfig(cc, lg) + if err != nil { + cobrautl.ExitWithError(cobrautl.ExitBadArgs, err) + } + + cfg.DialOptions = append(cfg.DialOptions, grpc.WithBlock()) + client, err := clientv3.New(*cfg) + if err != nil { + cobrautl.ExitWithError(cobrautl.ExitBadConnection, err) + } + + return client +} + func argOrStdin(args []string, stdin io.Reader, i int) (string, error) { if i < len(args) { return args[i], nil diff --git a/etcdctl/ctlv3/command/watch_command.go b/etcdctl/ctlv3/command/watch_command.go index 04313ce543d..efdd147d5e8 100644 --- a/etcdctl/ctlv3/command/watch_command.go +++ b/etcdctl/ctlv3/command/watch_command.go @@ -78,7 +78,7 @@ func watchCommandFunc(cmd *cobra.Command, args []string) { cobrautl.ExitWithError(cobrautl.ExitBadArgs, err) } - c := mustClientFromCmd(cmd) + c := mustBlockClientFromCmd(cmd) wc, err := getWatchChan(c, watchArgs) if err != nil { cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)