distribute_lock.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package utils
  2. import (
  3. "context"
  4. "gd_adm_data/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 redisUnlock(key string) error {
  81. _, err := cache.Redis.Del(key)
  82. return err
  83. }
  84. func localLock(key string) bool {
  85. now := time.Now().Unix()
  86. for {
  87. gmutex.Lock()
  88. if _, ok := gm[key]; ok == false {
  89. gm[key] = "-"
  90. gmutex.Unlock()
  91. return true
  92. }
  93. gmutex.Unlock()
  94. if time.Now().Unix()-now >= 60 {
  95. return false
  96. }
  97. time.Sleep(50 * time.Millisecond)
  98. }
  99. }
  100. func localUnlock(key string) {
  101. gmutex.Lock()
  102. if _, ok := gm[key]; ok == true {
  103. delete(gm, key)
  104. }
  105. if len(gm) == 0 {
  106. gm = map[string]string{}
  107. }
  108. gmutex.Unlock()
  109. }
  110. func Lock(flag string) error {
  111. realLockKey := lockKey + "/" + flag
  112. if localLock(realLockKey) == false {
  113. return errors.New("local lock timeout")
  114. }
  115. count := 0
  116. for {
  117. count++
  118. if count > 1000 {
  119. localUnlock(realLockKey)
  120. return errors.New("max count")
  121. }
  122. //err := lock(realLockKey)
  123. err := redisLock(realLockKey)
  124. if err == nil {
  125. return nil
  126. }
  127. time.Sleep(400 * time.Millisecond)
  128. /*
  129. if err.Error() != "can lock" {
  130. return err
  131. }*/
  132. }
  133. return nil
  134. }
  135. func UnLock(flag string) error {
  136. //return delKey(keyApi, lockKey+"/"+flag)
  137. err := redisUnlock(lockKey + "/" + flag)
  138. localUnlock(lockKey + "/" + flag)
  139. return err
  140. }