/
redis.go
136 lines (116 loc) · 3.19 KB
/
redis.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
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
package store
import (
"context"
"errors"
"sync"
"time"
"github.com/bsm/redislock"
"github.com/git-hulk/go-elect/elector/engine"
)
// RedisMutex implements engine.Mutex, it uses redislock to implement distributed mutex.
type RedisMutex struct {
client *redislock.Client
// rwmu is used to protect lock field
rwmu sync.RWMutex
lock *redislock.Lock
id string
key string
timeout time.Duration
}
// NewRedisMutex creates a new RedisMutex instance.
func NewRedisMutex(client *redislock.Client, id, key string, timeout time.Duration) *RedisMutex {
return &RedisMutex{
client: client,
id: id,
key: key,
timeout: timeout,
}
}
// ID returns the id of the Redis mutex.
func (mu *RedisMutex) ID() string {
return mu.id
}
// Key returns the key of the Redis mutex.
func (mu *RedisMutex) Key() string {
return mu.key
}
// Timeout returns the timeout of the Redis mutex.
// It's used to set the timeout of the Redis lock.
func (mu *RedisMutex) Timeout() time.Duration {
return mu.timeout
}
// IsLocked returns true if the Redis mutex is locked.
func (mu *RedisMutex) IsLocked() bool {
mu.rwmu.RLock()
defer mu.rwmu.RUnlock()
return mu.lock != nil
}
// TryLock tries to obtain the Redis lock, it will return ErrLeaderElected
// if the Redis lock is already held.
func (mu *RedisMutex) TryLock(ctx context.Context) error {
lock, err := mu.client.Obtain(ctx, mu.key, mu.timeout, &redislock.Options{
Token: mu.id,
// No retry strategy to achieve a non-blocking mu
RetryStrategy: redislock.NoRetry(),
})
if err != nil {
if errors.Is(err, redislock.ErrNotObtained) {
return engine.ErrLeaderElected
}
return err
}
mu.rwmu.Lock()
defer mu.rwmu.Unlock()
mu.lock = lock
return nil
}
// Release releases the Redis lock. It will return ErrNotLockHolder if the Redis lock
// is not held.
func (mu *RedisMutex) Release(ctx context.Context) error {
mu.rwmu.Lock()
if mu.lock == nil {
mu.rwmu.Unlock()
return engine.ErrNotLockHolder
}
lock := mu.lock
mu.lock = nil
mu.rwmu.Unlock()
if err := lock.Release(ctx); err != nil {
if errors.Is(err, redislock.ErrLockNotHeld) {
return engine.ErrNotLockHolder
}
return err
}
return nil
}
// Refresh refreshes the Redis lock. It will return ErrNotLockHolder if the Redis lock
// is not held.
func (mu *RedisMutex) Refresh(ctx context.Context, lease time.Duration) error {
mu.rwmu.RLock()
lock := mu.lock
mu.rwmu.RUnlock()
if lock == nil {
return engine.ErrNotLockHolder
}
if err := lock.Refresh(ctx, lease, nil); err != nil {
if errors.Is(err, redislock.ErrLockNotHeld) {
return engine.ErrNotLockHolder
}
}
return nil
}
// RedisEngine implements engine.SessionClient, it's used to create a new Redis session.
type RedisEngine struct {
client *redislock.Client
}
// Create creates a new Redis session.
func (r *RedisEngine) Create(ctx context.Context, id, key string, timeout time.Duration) (engine.Session, error) {
return engine.NewLockSession(ctx, NewRedisMutex(r.client, id, key, timeout))
}
// NewRedisStore creates a new Redis session store.
func NewRedisStore(c redislock.RedisClient) *engine.SessionStore {
redisEngine := &RedisEngine{
client: redislock.New(c),
}
return engine.NewSessionStore(redisEngine)
}