func ExampleMaintenance_status() { for _, ep := range endpoints { cli, err := clientv3.New(clientv3.Config{ Endpoints: []string{ep}, DialTimeout: dialTimeout, }) if err != nil { log.Fatal(err) } defer cli.Close() // resp, err := cli.Status(context.Background(), ep) // // or // mapi := clientv3.NewMaintenance(cli) resp, err := mapi.Status(context.Background(), ep) if err != nil { log.Fatal(err) } fmt.Printf("endpoint: %s / IsLeader: %v\n", ep, resp.Header.MemberId == resp.Leader) } // endpoint: localhost:2379 / IsLeader: false // endpoint: localhost:22379 / IsLeader: false // endpoint: localhost:32379 / IsLeader: true }
// GetLeader returns the index of leader and error if any. func (c *cluster) GetLeader() (int, error) { if c.v2Only { return 0, nil } for i, ep := range c.GRPCURLs { cli, err := clientv3.New(clientv3.Config{ Endpoints: []string{ep}, DialTimeout: 5 * time.Second, }) if err != nil { return 0, err } defer cli.Close() mapi := clientv3.NewMaintenance(cli) resp, err := mapi.Status(context.Background(), ep) if err != nil { return 0, err } if resp.Header.MemberId == resp.Leader { return i, nil } } return 0, fmt.Errorf("no leader found") }