Skip to content

Commit

Permalink
Ping etcd before go into etcd client state (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: Congqi Xia <[email protected]>

Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia authored Oct 11, 2022
1 parent 5e9fbad commit 2dddade
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions states/etcd_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import (

"github.com/spf13/cobra"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
)

const (
metaPath = `meta`
)

func pingEtcd(ctx context.Context, cli *clientv3.Client) error {
_, err := cli.Get(ctx, "ping")
return err
}

// getConnectCommand returns the command for connect etcd.
// usage: connect --etcd [address] --rootPath [rootPath]
func getConnectCommand(state State) *cobra.Command {
Expand Down Expand Up @@ -41,11 +47,23 @@ func getConnectCommand(state State) *cobra.Command {
etcdCli, err := clientv3.New(clientv3.Config{
Endpoints: []string{etcdAddr},
DialTimeout: time.Second * 2,

// disable grpc logging
Logger: zap.NewNop(),
})
if err != nil {
return err
}

// ping etcd
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
err = pingEtcd(ctx, etcdCli)
if err != nil {
fmt.Println("cannot connect to etcd with addr:", etcdAddr, err.Error())
return nil
}

fmt.Println("Using meta path:", fmt.Sprintf("%s/%s/", rootPath, metaPath))

etcdState := getEtcdConnectedState(etcdCli, etcdAddr)
Expand Down

0 comments on commit 2dddade

Please sign in to comment.