-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_distr_state_lock.go
80 lines (66 loc) · 2.24 KB
/
redis_distr_state_lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package distrlock
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"github.com/furyaxyz/fuxchain/libs/tendermint/libs/log"
)
var unlockScript = redis.NewScript(`
if redis.call("get", KEYS[1]) == ARGV[1]
then
return redis.call("del", KEYS[1])
else
return 0
end
`)
var unlockScriptWithState = redis.NewScript(`
if redis.call("get", KEYS[1]) == ARGV[1]
then
redis.call("set", KEYS[2], ARGV[2])
return redis.call("del", KEYS[1])
else
return 0
end
`)
type RedisDistributeStateService struct {
client *redis.Client
logger log.Logger
lockerID string // unique identifier of locker
}
func NewRedisDistributeStateService(url string, pass string, db int, logger log.Logger, lockerID string) (*RedisDistributeStateService, error) {
client := redis.NewClient(&redis.Options{
Addr: url,
Password: pass, // no password set
DB: db, // use select DB
})
s := &RedisDistributeStateService{
client: client,
logger: logger,
lockerID: lockerID,
}
return s, nil
}
func (s *RedisDistributeStateService) GetLockerID() string {
return s.lockerID
}
func (s *RedisDistributeStateService) GetDistState(stateKey string) string {
state, _ := s.client.Get(context.Background(), stateKey).Result()
return state
}
func (s *RedisDistributeStateService) SetDistState(stateKey string, stateValue string) error {
err := s.client.Set(context.Background(), stateKey, stateValue, 0).Err()
return err
}
func (s *RedisDistributeStateService) FetchDistLock(lockKey string, locker string, expiredInMS int) (bool, error) {
success, err := s.client.SetNX(context.Background(), lockKey, locker,
time.Duration(expiredInMS)*time.Millisecond).Result()
return success, err
}
func (s *RedisDistributeStateService) ReleaseDistLock(lockKey string, locker string) (bool, error) {
replyStatus, err := unlockScript.Run(context.Background(), s.client, []string{lockKey}, locker).Int()
return err == nil && replyStatus == 1, err
}
func (s *RedisDistributeStateService) UnlockDistLockWithState(lockKey string, locker string, stateKey string, stateValue string) (bool, error) {
replyStatus, err := unlockScriptWithState.Run(context.Background(), s.client, []string{lockKey, stateKey}, locker, stateValue).Int()
return err == nil && replyStatus == 1, err
}