example_mutex_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. package concurrency_test
  15. import (
  16. "context"
  17. "fmt"
  18. "log"
  19. "go.etcd.io/etcd/clientv3"
  20. "go.etcd.io/etcd/clientv3/concurrency"
  21. )
  22. func ExampleMutex_TryLock() {
  23. cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. defer cli.Close()
  28. // create two separate sessions for lock competition
  29. s1, err := concurrency.NewSession(cli)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. defer s1.Close()
  34. m1 := concurrency.NewMutex(s1, "/my-lock")
  35. s2, err := concurrency.NewSession(cli)
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. defer s2.Close()
  40. m2 := concurrency.NewMutex(s2, "/my-lock")
  41. // acquire lock for s1
  42. if err = m1.Lock(context.TODO()); err != nil {
  43. log.Fatal(err)
  44. }
  45. fmt.Println("acquired lock for s1")
  46. if err = m2.TryLock(context.TODO()); err == nil {
  47. log.Fatal("should not acquire lock")
  48. }
  49. if err == concurrency.ErrLocked {
  50. fmt.Println("cannot acquire lock for s2, as already locked in another session")
  51. }
  52. if err = m1.Unlock(context.TODO()); err != nil {
  53. log.Fatal(err)
  54. }
  55. fmt.Println("released lock for s1")
  56. if err = m2.TryLock(context.TODO()); err != nil {
  57. log.Fatal(err)
  58. }
  59. fmt.Println("acquired lock for s2")
  60. // Output:
  61. // acquired lock for s1
  62. // cannot acquire lock for s2, as already locked in another session
  63. // released lock for s1
  64. // acquired lock for s2
  65. }
  66. func ExampleMutex_Lock() {
  67. cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. defer cli.Close()
  72. // create two separate sessions for lock competition
  73. s1, err := concurrency.NewSession(cli)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. defer s1.Close()
  78. m1 := concurrency.NewMutex(s1, "/my-lock/")
  79. s2, err := concurrency.NewSession(cli)
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. defer s2.Close()
  84. m2 := concurrency.NewMutex(s2, "/my-lock/")
  85. // acquire lock for s1
  86. if err := m1.Lock(context.TODO()); err != nil {
  87. log.Fatal(err)
  88. }
  89. fmt.Println("acquired lock for s1")
  90. m2Locked := make(chan struct{})
  91. go func() {
  92. defer close(m2Locked)
  93. // wait until s1 is locks /my-lock/
  94. if err := m2.Lock(context.TODO()); err != nil {
  95. log.Fatal(err)
  96. }
  97. }()
  98. if err := m1.Unlock(context.TODO()); err != nil {
  99. log.Fatal(err)
  100. }
  101. fmt.Println("released lock for s1")
  102. <-m2Locked
  103. fmt.Println("acquired lock for s2")
  104. // Output:
  105. // acquired lock for s1
  106. // released lock for s1
  107. // acquired lock for s2
  108. }