Repository navigation
Expand file tree
/
Copy pathimpl.go
More file actions
171 lines (142 loc) · 3.56 KB
/
Copy pathimpl.go
File metadata and controls
171 lines (142 loc) · 3.56 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package arbiter
import (
"context"
"errors"
"sync"
"time"
"github.com/redis/go-redis/v9"
"github.com/huimingz/arbiter/internal/lua"
)
var (
ErrLockNotHeld = errors.New("lock not held")
ErrLockTimeout = errors.New("lock timeout")
)
type lockImpl struct {
redis *redis.Client
name string
value string
options *LockOptions
logger Logger
watchDogCtx context.Context
watchDogCancel context.CancelFunc
watchDogOnce sync.Once
watchDogDone chan struct{}
mu sync.Mutex
}
func newLock(redis *redis.Client, name string, options *LockOptions, logger Logger) Lock {
return &lockImpl{
redis: redis,
name: name,
value: generateValue(),
options: options,
logger: logger,
watchDogDone: make(chan struct{}),
}
}
func (l *lockImpl) Lock(ctx context.Context) error {
deadline := time.Now().Add(l.options.WaitTimeout)
l.logger.Debug(ctx, "Attempting to acquire lock: %s", l.name)
attempt := 0
for {
attempt++
acquired, err := l.TryLock(ctx)
if err != nil {
l.logger.Error(ctx, "Failed to acquire lock: %s, error: %v", l.name, err)
return err
}
if acquired {
l.logger.Info(ctx, "Successfully acquired lock: %s", l.name)
return nil
}
if l.options.WaitTimeout > 0 && time.Now().After(deadline) {
l.logger.Warn(ctx, "Timeout waiting for lock: %s", l.name)
return ErrLockTimeout
}
select {
case <-ctx.Done():
l.logger.Debug(ctx, "Context cancelled while waiting for lock: %s", l.name)
return ctx.Err()
case <-time.After(100 * time.Millisecond): // retry delay
continue
}
}
}
func (l *lockImpl) TryLock(ctx context.Context) (bool, error) {
l.mu.Lock()
defer l.mu.Unlock()
8566
leaseTime := l.options.LeaseTime
if l.options.EnableWatchDog {
leaseTime = l.options.WatchDogTimeout
}
ok, err := l.redis.Eval(ctx, lua.TryLock, []string{l.name}, l.value, leaseTime.Milliseconds()).Bool()
if err != nil {
l.logger.Error(ctx, "Error trying to acquire lock: %s", l.name)
return false, err
}
if !ok {
return false, nil
}
if l.options.EnableWatchDog {
l.logger.Debug(ctx, "Starting watchdog for lock: %s", l.name)
l.startWatchDog(ctx)
}
return true, nil
}
func (l *lockImpl) Unlock(ctx context.Context) error {
l.mu.Lock()
defer l.mu.Unlock()
if l.watchDogCancel != nil {
l.watchDogCancel()
<-l.watchDogDone
}
ok, err := l.redis.Eval(ctx, lua.Unlock, []string{l.name}, l.value).Bool()
if err != nil {
l.logger.Error(ctx, "Error releasing lock: %s", l.name)
return err
}
if !ok {
return ErrLockNotHeld
}
l.logger.Info(ctx, "Released lock: %s", l.name)
return nil
}
func (l *lockImpl) Refresh(ctx context.Context) error {
l.mu.Lock()
defer l.mu.Unlock()
leaseTime := l.options.LeaseTime
if l.options.EnableWatchDog {
leaseTime = l.options.WatchDogTimeout
}
ok, err := l.redis.Eval(ctx, lua.Refresh, []string{l.name}, l.value, leaseTime.Milliseconds()).Bool()
if err != nil {
l.logger.Error(ctx, "Error refreshing lock: %s", l.name)
return err
}
if !ok {
return ErrLockNotHeld
}
return nil
}
func (l *lockImpl) startWatchDog(ctx context.Context) {
l.watchDogOnce.Do(func() {
l.watchDogCtx, l.watchDogCancel = context.WithCancel(context.Background())
go func() {
defer close(l.watchDogDone)
ticker := time.NewTicker(l.options.WatchDogTimeout / 3)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := l.Refresh(ctx); err != nil {
l.logger.Error(ctx, "Watchdog failed to refresh lock: %s", l.name)
return
}
case <-l.watchDogCtx.Done():
return
case <-ctx.Done():
return
}
}
}()
})
}