distribute_lock.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package utils
  2. import (
  3. "context"
  4. "gd_access_log/common.in/cache"
  5. "errors"
  6. "fmt"
  7. "go.etcd.io/etcd/client"
  8. "sync"
  9. "time"
  10. )
  11. var lockKey = "/gd/lock"
  12. var lockValue = "locked"
  13. var etcdClient client.Client
  14. var keyApi client.KeysAPI
  15. var gmutex sync.Mutex
  16. var gm = map[string]string{}
  17. func SetEtcdKeyApi(c client.Client) {
  18. if keyApi == nil {
  19. keyApi = client.NewKeysAPI(c)
  20. }
  21. }
  22. func setKey(keyApi client.KeysAPI, key string, value string, prevExist client.PrevExistType) error {
  23. opts := &client.SetOptions{
  24. PrevExist: prevExist,
  25. TTL: 10 * time.Second,
  26. }
  27. resp, err := keyApi.Set(context.Background(), key, value, opts)
  28. fmt.Printf("set key:%v,%v\n", resp, err)
  29. return err
  30. }
  31. func delKey(keyApi client.KeysAPI, key string) error {
  32. resp, err := keyApi.Delete(context.Background(), key, nil)
  33. fmt.Printf("del key:%v,%v\n", resp, err)
  34. e, _ := err.(client.Error)
  35. if e.Code == client.ErrorCodeKeyNotFound {
  36. return nil
  37. }
  38. return err
  39. }
  40. func lock(realLockKey string) error {
  41. var err error
  42. err = setKey(keyApi, realLockKey, lockValue, client.PrevNoExist)
  43. if err == nil {
  44. return nil
  45. }
  46. return err
  47. /*
  48. e, _ := err.(client.Error)
  49. if e.Code != client.ErrorCodeNodeExist {
  50. return err
  51. }
  52. resp, err := keyApi.Get(context.Background(), realLockKey, nil)
  53. if err != nil {
  54. return err
  55. }
  56. watcherOptions := &client.WatcherOptions{
  57. AfterIndex: resp.Index,
  58. Recursive: false,
  59. }
  60. fmt.Printf("wait wait\n")
  61. watcher := keyApi.Watcher(realLockKey, watcherOptions)
  62. for {
  63. resp, err := watcher.Next(context.Background())
  64. if err != nil {
  65. return err
  66. }
  67. if resp.Action == "delete" || resp.Action == "expired" {
  68. return errors.New("can lock")
  69. }
  70. }*/
  71. return nil
  72. }
  73. func redisLock(key string) error {
  74. exist, err := cache.Redis.SetNxEx(key, "-", 10)
  75. if exist == false {
  76. return errors.New("false")
  77. }
  78. return err
  79. }
  80. func redisLockWithTimeout(key string, timeout int) error {
  81. if timeout == 0 {
  82. timeout = 10
  83. }
  84. exist, err := cache.Redis.SetNxEx(key, "-", int64(timeout))
  85. if exist == false {
  86. return errors.New("false")
  87. }
  88. return err
  89. }
  90. func redisUnlock(key string) error {
  91. _, err := cache.Redis.Del(key)
  92. return err
  93. }
  94. func localLock(key string) bool {
  95. now := time.Now().Unix()
  96. for {
  97. gmutex.Lock()
  98. if _, ok := gm[key]; ok == false {
  99. gm[key] = "-"
  100. gmutex.Unlock()
  101. return true
  102. }
  103. gmutex.Unlock()
  104. if time.Now().Unix()-now >= 60 {
  105. return false
  106. }
  107. time.Sleep(50 * time.Millisecond)
  108. }
  109. }
  110. func localLockWithTimeout(key string, timeout int) bool {
  111. now := time.Now().Unix()
  112. for {
  113. gmutex.Lock()
  114. if _, ok := gm[key]; ok == false {
  115. gm[key] = "-"
  116. gmutex.Unlock()
  117. return true
  118. }
  119. gmutex.Unlock()
  120. if time.Now().Unix()-now >= int64(timeout) {
  121. return false
  122. }
  123. time.Sleep(50 * time.Millisecond)
  124. }
  125. }
  126. func localUnlock(key string) {
  127. gmutex.Lock()
  128. if _, ok := gm[key]; ok == true {
  129. delete(gm, key)
  130. }
  131. if len(gm) == 0 {
  132. gm = map[string]string{}
  133. }
  134. gmutex.Unlock()
  135. }
  136. func Lock(flag string) error {
  137. realLockKey := lockKey + "/" + flag
  138. if localLock(realLockKey) == false {
  139. return errors.New("local lock timeout")
  140. }
  141. count := 0
  142. for {
  143. count++
  144. if count > 1000 {
  145. localUnlock(realLockKey)
  146. return errors.New("max count")
  147. }
  148. //err := lock(realLockKey)
  149. err := redisLock(realLockKey)
  150. if err == nil {
  151. return nil
  152. }
  153. time.Sleep(400 * time.Millisecond)
  154. /*
  155. if err.Error() != "can lock" {
  156. return err
  157. }*/
  158. }
  159. return nil
  160. }
  161. func LockWithTimeout(flag string, timeout int) error {
  162. now := time.Now().Unix()
  163. realLockKey := lockKey + "/" + flag
  164. if localLockWithTimeout(realLockKey, timeout) == false {
  165. return errors.New("local lock timeout")
  166. }
  167. for {
  168. loopNow := time.Now().Unix()
  169. //err := lock(realLockKey)
  170. err := redisLockWithTimeout(realLockKey, timeout)
  171. if err == nil {
  172. return nil
  173. }
  174. if loopNow-now >= int64(timeout) {
  175. localUnlock(realLockKey)
  176. return errors.New("max count")
  177. }
  178. time.Sleep(500 * time.Millisecond)
  179. /*
  180. if err.Error() != "can lock" {
  181. return err
  182. }*/
  183. }
  184. return nil
  185. }
  186. func UnLock(flag string) error {
  187. //return delKey(keyApi, lockKey+"/"+flag)
  188. err := redisUnlock(lockKey + "/" + flag)
  189. localUnlock(lockKey + "/" + flag)
  190. return err
  191. }