package main import ( "context" "fmt" clientv3 "go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/client/v3/concurrency" "time" ) func main() { cli, err := clientv3.New(clientv3.Config{ Endpoints: []string{"106.54.33.152:2379"}, DialTimeout: 5 * time.Second, }) if err != nil { panic(err) } defer cli.Close() for i := 0; i < 10; i++ { go func(i int) { m, err := NewPciLock(cli) if err != nil { fmt.Printf("Error creating") } m.IsOwner() err = m.Lock(context.Background()) if err != nil { fmt.Printf("Failed to lock: %v\n", err) return } if i == 1 { time.Sleep(time.Second * 10) } fmt.Println(i) err = m.Unlock(context.Background()) if err != nil { fmt.Printf("Failed to Unlock: %v\n", err) return } }(i) } time.Sleep(time.Second * 15) } func NewPciLock(etcd *clientv3.Client) (*concurrency.Mutex, error) { session, err := concurrency.NewSession(etcd, concurrency.WithTTL(5)) if err != nil { return nil, err } m := concurrency.NewMutex(session, "hstack/server/pciLock") return m, nil }