-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributed_locker.go
66 lines (61 loc) · 1.47 KB
/
distributed_locker.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
package request_locker
import (
"context"
"time"
"github.com/bsm/redislock"
"github.com/redis/go-redis/v9"
)
type DistributedLocker struct {
redisClient *redis.Client
redisLock *redislock.Client
ttl time.Duration
tickerDuration time.Duration
}
func NewDistributedLocker(rsClient *redis.Client, rsLock *redislock.Client, ttl, ticketDuration time.Duration) *DistributedLocker {
distributedLocker := &DistributedLocker{
redisClient: rsClient,
redisLock: rsLock,
ttl: ttl,
tickerDuration: ticketDuration,
}
if distributedLocker.ttl == 0 {
distributedLocker.ttl = DefaultTTL
}
if distributedLocker.tickerDuration == 0 {
distributedLocker.tickerDuration = DefaultTicker
}
return distributedLocker
}
func (l *DistributedLocker) WaitSignal(ctx context.Context, abortChan SyncChannel[bool], key, value string) {
lock, err := l.redisLock.Obtain(ctx, key, l.ttl, nil)
if err != nil {
abortChan.Write(false)
return
}
err = l.redisClient.Set(ctx, key, value, l.ttl).Err()
if err != nil {
abortChan.Write(false)
return
}
lock.Release(context.Background())
ticker := time.NewTicker(l.tickerDuration)
go func() {
for {
select {
case <-ctx.Done():
abortChan.Write(false)
return
case <-ticker.C:
result, err := l.redisClient.Get(ctx, key).Result()
if err != nil || result == "" {
abortChan.Write(false)
return
}
if result != value {
abortChan.Write(true)
return
}
}
}
}()
}