test.go 589 B

12345678910111213141516171819202122232425262728293031
  1. package main
  2. import (
  3. "context"
  4. clientv3 "go.etcd.io/etcd/client/v3"
  5. "go.etcd.io/etcd/client/v3/concurrency"
  6. "time"
  7. )
  8. func main() {
  9. cli, err := clientv3.New(clientv3.Config{
  10. Endpoints: []string{"106.54.33.152:2379"},
  11. DialTimeout: 5 * time.Second,
  12. })
  13. if err != nil {
  14. panic(err)
  15. }
  16. defer cli.Close()
  17. grant, _ := cli.Grant(context.Background(), 15)
  18. session, err := concurrency.NewSession(cli, concurrency.WithLease(grant.ID))
  19. if err != nil {
  20. panic(err)
  21. }
  22. m := concurrency.NewLocker(session, "lock/test")
  23. m.Lock()
  24. defer m.Unlock()
  25. time.Sleep(20 * time.Second)
  26. }