/
dmutex.go
176 lines (142 loc) · 4.12 KB
/
dmutex.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
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
172
173
174
175
176
package etcd_dsync
import (
"context"
"errors"
"fmt"
"git.golaxy.org/framework/plugins/dsync"
"git.golaxy.org/framework/plugins/log"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
etcd_concurrency "go.etcd.io/etcd/client/v3/concurrency"
"math"
"strconv"
"strings"
"time"
)
func (s *_DistSync) newMutex(name string, options dsync.DistMutexOptions) *_DistMutex {
if s.options.KeyPrefix != "" {
name = s.options.KeyPrefix + name
}
log.Debugf(s.servCtx, "new dist mutex %q", name)
return &_DistMutex{
dsync: s,
name: name,
expiry: options.Expiry,
driftFactor: options.DriftFactor,
timeoutFactor: options.TimeoutFactor,
}
}
type _DistMutex struct {
dsync *_DistSync
name string
expiry time.Duration
driftFactor float64
timeoutFactor float64
session *etcd_concurrency.Session
mutex *etcd_concurrency.Mutex
until time.Time
}
// Name returns mutex name.
func (m *_DistMutex) Name() string {
return strings.TrimPrefix(m.name, m.dsync.options.KeyPrefix)
}
// Value returns the current random value. The value will be empty until a lock is acquired (or Value option is used).
func (m *_DistMutex) Value() string {
if m.session == nil {
return ""
}
return strconv.Itoa(int(m.session.Lease()))
}
// Until returns the time of validity of acquired lock. The value will be zero value until a lock is acquired.
func (m *_DistMutex) Until() time.Time {
return m.until
}
// Lock locks m. In case it returns an error on failure, you may retry to acquire the lock by calling this method again.
func (m *_DistMutex) Lock(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
}
expirySec := m.expiry.Seconds()
if expirySec <= 0 {
expirySec = 1
}
session, err := etcd_concurrency.NewSession(m.dsync.client, etcd_concurrency.WithTTL(int(math.Ceil(expirySec))))
if err != nil {
return fmt.Errorf("dsync: %w", err)
}
mutex := etcd_concurrency.NewMutex(session, m.name)
start := time.Now()
ctx, _ = context.WithTimeout(ctx, time.Duration((expirySec*m.timeoutFactor)*float64(time.Second)))
if err = mutex.Lock(ctx); err != nil {
session.Close()
return fmt.Errorf("dsync: %w", err)
}
if _, err = m.dsync.client.KeepAlive(ctx, session.Lease()); err != nil {
mutex.Unlock(context.Background())
session.Close()
return fmt.Errorf("dsync: %w", err)
}
m.clean()
m.session = session
m.mutex = mutex
now := time.Now()
m.until = now.Add(m.expiry - now.Sub(start) - time.Duration(int64(expirySec*m.driftFactor)))
log.Debugf(m.dsync.servCtx, "dist mutex %q is locked", m.name)
return nil
}
// Unlock unlocks m and returns the status of unlock.
func (m *_DistMutex) Unlock(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
}
if m.mutex == nil {
return dsync.ErrNotAcquired
}
defer m.clean()
if err := m.mutex.Unlock(ctx); err != nil {
if errors.Is(err, rpctypes.ErrKeyNotFound) {
return dsync.ErrNotAcquired
}
return fmt.Errorf("dsync: %w", err)
}
log.Debugf(m.dsync.servCtx, "dist mutex %q is unlocked", m.name)
return nil
}
// Extend resets the mutex's expiry and returns the status of expiry extension.
func (m *_DistMutex) Extend(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
}
if m.session == nil {
return dsync.ErrNotAcquired
}
if _, err := m.dsync.client.KeepAlive(ctx, m.session.Lease()); err != nil {
if errors.Is(err, rpctypes.ErrLeaseNotFound) {
return dsync.ErrNotAcquired
}
return fmt.Errorf("dsync: %w", err)
}
log.Debugf(m.dsync.servCtx, "dist mutex %q is extended", m.name)
return nil
}
// Valid returns true if the lock acquired through m is still valid. It may
// also return true erroneously if quorum is achieved during the call and at
// least one node then takes long enough to respond for the lock to expire.
func (m *_DistMutex) Valid(ctx context.Context) (bool, error) {
if m.session == nil {
return false, nil
}
select {
case <-m.session.Done():
return false, nil
default:
return true, nil
}
}
func (m *_DistMutex) clean() {
if m.session != nil {
m.session.Close()
}
m.session = nil
m.mutex = nil
m.until = time.Time{}
}