This repository was archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.go
102 lines (86 loc) · 2.36 KB
/
mutex.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package redqueue
import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
"time"
"github.com/redis/go-redis/v9"
)
var ErrFailed = errors.New("redsync: failed to acquire lock")
var ErrExtendFailed = errors.New("redsync: failed to extend lock")
// A Lease is a distributed mutual exclusion lock.
type Lease struct {
item string
key string
driftFactor float64
token string
until time.Time
client *redis.Client
}
// Item returns mutex name (i.e. the Redis key).
func (m *Lease) Item() string {
return m.item
}
// Until returns the time of validity of acquired lock. The value will be zero value until a lock is acquired.
func (m *Lease) Until() time.Time {
return m.until
}
// Release unlocks m and returns the status of unlock.
func (m *Lease) Release(ctx context.Context) (bool, error) {
return m.release(ctx, m.client, m.token)
}
// Extend resets the mutex's expiry and returns the status of expiry extension.
func (m *Lease) Extend(ctx context.Context, duration time.Duration) (bool, error) {
start := time.Now()
held, err := m.touch(ctx, m.client, m.token, duration)
if err != nil {
return false, err
}
if !held {
return false, nil
}
now := time.Now()
until := now.Add(duration - now.Sub(start) - time.Duration(int64(float64(duration)*m.driftFactor)))
if now.Before(until) {
m.until = until
return true, nil
}
return false, ErrExtendFailed
}
func genToken() (string, error) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}
var deleteScript = redis.NewScript(`
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("DEL", KEYS[1])
else
return 0
end
`)
func (m *Lease) release(ctx context.Context, client *redis.Client, token string) (bool, error) {
status, err := deleteScript.Run(ctx, client, []string{m.key}, token).Result()
if err != nil {
return false, err
}
return status != int64(0), nil
}
var touchScript = redis.NewScript(`
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("PEXPIRE", KEYS[1], ARGV[2])
else
return 0
end
`)
func (m *Lease) touch(ctx context.Context, client *redis.Client, token string, expiry time.Duration) (bool, error) {
status, err := touchScript.Run(ctx, client, []string{m.key}, token, int(expiry/time.Millisecond)).Result()
if err != nil {
return false, err
}
return status != int64(0), nil
}