-
Notifications
You must be signed in to change notification settings - Fork 373
/
dlock.go
198 lines (173 loc) · 4.52 KB
/
dlock.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// distributed-lock base etcd
// usage:
// lock, _ := dlock.New(dlock.WithTTL(5), func(){})
// lock.Lock(ctx)
// // do something...
// lock.Unlock()
// // release resource
// lock.Close() // see also lock.UnlockAndClose()
// //see also dlock_test.go
package dlock
import (
"context"
"crypto/tls"
"net/url"
"os"
"strings"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/clientv3/concurrency"
"github.com/coreos/etcd/pkg/transport"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
// The short keepalive timeout and interval have been chosen to aggressively
// detect a failed etcd server without introducing much overhead.
keepaliveTime = 30 * time.Second
keepaliveTimeout = 10 * time.Second
// default ttl, time to live when current process exit without unlock (eg. panic)
defaultTTL = 5
)
type DLock struct {
client *clientv3.Client
session *concurrency.Session
mutex *concurrency.Mutex
option *option
lockKey string
normalClose bool
}
type option struct {
ttl int
}
type OpOption func(*option)
func WithTTL(ttl int) OpOption {
return func(op *option) {
op.ttl = ttl
}
}
func applyOptions(ops []OpOption, option *option) error {
for _, op := range ops {
op(option)
}
if option.ttl <= 0 {
return errors.New("illegal ttl value, must greater than 0")
}
return nil
}
func New(lockKey string, locklostCallback func(), ops ...OpOption) (*DLock, error) {
option := &option{ttl: defaultTTL}
if err := applyOptions(ops, option); err != nil {
return nil, err
}
var endpoints []string
env := os.Getenv("ETCD_ENDPOINTS")
if env == "" {
endpoints = []string{"http://127.0.0.1:2379"}
} else {
endpoints = strings.Split(env, ",")
}
var tlsConfig *tls.Config
if len(endpoints) < 1 {
return nil, errors.New("Invalid Etcd endpoints")
}
url, err := url.Parse(endpoints[0])
if err != nil {
return nil, errors.Wrap(err, "Invalid Etcd endpoints")
}
if url.Scheme == "https" {
tlsInfo := transport.TLSInfo{
CertFile: "/certs/etcd-client.pem",
KeyFile: "/certs/etcd-client-key.pem",
TrustedCAFile: "/certs/etcd-ca.pem",
}
tlsConfig, err = tlsInfo.ClientConfig()
if err != nil {
return nil, errors.Wrap(err, "Invalid Etcd TLS config")
}
}
cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialKeepAliveTime: keepaliveTime,
DialKeepAliveTimeout: keepaliveTimeout,
TLS: tlsConfig,
})
if err != nil {
return nil, err
}
session, err := concurrency.NewSession(cli, concurrency.WithTTL(option.ttl))
if err != nil {
return nil, err
}
mutex := concurrency.NewMutex(session, lockKey)
l := DLock{
client: cli,
session: session,
mutex: mutex,
lockKey: lockKey,
option: option,
}
go func() {
select {
case <-l.session.Done():
// invoke l.Close() or l.UnlockAndClose()
if l.normalClose {
return
}
if locklostCallback != nil {
locklostCallback()
}
}
}()
return &l, nil
}
// it's cancelable
func (l *DLock) Lock(ctx context.Context) error {
return l.mutex.Lock(ctx)
}
func (l *DLock) Unlock() error {
return l.mutex.Unlock(context.Background())
}
func (l *DLock) Close() error {
l.normalClose = true
var errs []string
if err := l.session.Close(); err != nil {
logrus.Errorf("dlock: failed to close concurrency session, err: %v", err)
errs = append(errs, err.Error())
}
if err := l.client.Close(); err != nil {
logrus.Errorf("dlock: failed to close etcd client, err: %v", err)
errs = append(errs, err.Error())
}
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}
func (l *DLock) UnlockAndClose() error {
defer l.Close()
return l.Unlock()
}
// return locked key belong to this locker: <lockKey>/<lease-ID>
func (l *DLock) Key() string {
return l.mutex.Key()
}
func (l *DLock) IsOwner() (bool, error) {
r, err := l.client.Txn(context.Background()).If(l.mutex.IsOwner()).Commit()
if err != nil {
return false, err
}
return r.Succeeded, nil
}