fix dial-timeout not affected for client watch command

Signed-off-by: joey <zchengjoey@gmail.com>
This commit is contained in:
joey 2024-07-16 13:52:55 +08:00
parent c86c93ca29
commit 3d8fe917f9
No known key found for this signature in database
GPG Key ID: B5AE69C27B241A78
2 changed files with 24 additions and 1 deletions

View File

@ -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

View File

@ -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)