distribute_lock.go 3.0 KB

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