go.etcd.io/etcd@v3.3.27+incompatible/clientv3/concurrency/example_election_test.go (about)

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package concurrency_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"sync"
    22  	"time"
    23  
    24  	"github.com/coreos/etcd/clientv3"
    25  	"github.com/coreos/etcd/clientv3/concurrency"
    26  )
    27  
    28  func ExampleElection_Campaign() {
    29  	cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
    30  	if err != nil {
    31  		log.Fatal(err)
    32  	}
    33  	defer cli.Close()
    34  
    35  	// create two separate sessions for election competition
    36  	s1, err := concurrency.NewSession(cli)
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  	defer s1.Close()
    41  	e1 := concurrency.NewElection(s1, "/my-election/")
    42  
    43  	s2, err := concurrency.NewSession(cli)
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  	defer s2.Close()
    48  	e2 := concurrency.NewElection(s2, "/my-election/")
    49  
    50  	// create competing candidates, with e1 initially losing to e2
    51  	var wg sync.WaitGroup
    52  	wg.Add(2)
    53  	electc := make(chan *concurrency.Election, 2)
    54  	go func() {
    55  		defer wg.Done()
    56  		// delay candidacy so e2 wins first
    57  		time.Sleep(3 * time.Second)
    58  		if err := e1.Campaign(context.Background(), "e1"); err != nil {
    59  			log.Fatal(err)
    60  		}
    61  		electc <- e1
    62  	}()
    63  	go func() {
    64  		defer wg.Done()
    65  		if err := e2.Campaign(context.Background(), "e2"); err != nil {
    66  			log.Fatal(err)
    67  		}
    68  		electc <- e2
    69  	}()
    70  
    71  	cctx, cancel := context.WithCancel(context.TODO())
    72  	defer cancel()
    73  
    74  	e := <-electc
    75  	fmt.Println("completed first election with", string((<-e.Observe(cctx)).Kvs[0].Value))
    76  
    77  	// resign so next candidate can be elected
    78  	if err := e.Resign(context.TODO()); err != nil {
    79  		log.Fatal(err)
    80  	}
    81  
    82  	e = <-electc
    83  	fmt.Println("completed second election with", string((<-e.Observe(cctx)).Kvs[0].Value))
    84  
    85  	wg.Wait()
    86  
    87  	// Output:
    88  	// completed first election with e2
    89  	// completed second election with e1
    90  }