/
locks.go
208 lines (144 loc) · 3.37 KB
/
locks.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
199
200
201
202
203
204
205
206
207
208
package flow
import (
"context"
"database/sql"
"fmt"
"runtime/debug"
"time"
"github.com/lib/pq"
"github.com/mitchellh/hashstructure/v2"
derrors "github.com/direktiv/direktiv/pkg/flow/errors"
)
const defaultLockWait = time.Second * 10
type locks struct {
db *sql.DB
}
func initLocks(conn string) (*locks, error) {
var err error
locks := new(locks)
locks.db, err = sql.Open("postgres", conn)
if err != nil {
return nil, err
}
locks.db.SetConnMaxIdleTime(-1)
locks.db.SetConnMaxLifetime(-1)
locks.db.SetMaxOpenConns(10)
locks.db.SetMaxIdleConns(10)
return locks, nil
}
func (locks *locks) Close() error {
if locks.db != nil {
err := locks.db.Close()
if err != nil {
return err
}
locks.db = nil
return nil
}
return nil
}
func (locks *locks) lockDB(id uint64, wait int) (*sql.Conn, error) {
var (
err error
conn *sql.Conn
)
defer func() {
if err != nil && conn != nil {
conn.Close()
}
}()
ctx, cancel := context.WithTimeout(context.Background(),
time.Duration(wait)*time.Second)
defer cancel()
conn, err = locks.db.Conn(ctx)
if err != nil {
return nil, err
}
_, err = conn.ExecContext(ctx, "SELECT pg_advisory_lock($1)", int64(id))
if err, ok := err.(*pq.Error); ok {
if err.Code == "57014" {
return conn, fmt.Errorf("canceled query")
}
return conn, err
}
return conn, err
}
func (locks *locks) unlockDB(id uint64, conn *sql.Conn) (err error) {
defer func() {
err = conn.Close()
if err != nil {
err = fmt.Errorf("can not close database connection %d: %v", id, err)
}
}()
_, err = conn.ExecContext(context.Background(),
"SELECT pg_advisory_unlock($1)", int64(id))
if err != nil {
err = fmt.Errorf("can not unlock lock %d: %v", id, err)
}
return err
}
func (engine *engine) lock(key string, timeout time.Duration) (context.Context, *sql.Conn, error) {
hash, err := hashstructure.Hash(key, hashstructure.FormatV2, nil)
if err != nil {
return nil, nil, derrors.NewInternalError(err)
}
wait := int(timeout.Seconds())
conn, err := engine.locks.lockDB(hash, wait)
if err != nil {
return nil, nil, derrors.NewInternalError(err)
}
st := debug.Stack()
ch := make(chan bool, 1)
ctx, cancel := context.WithCancel(context.Background())
engine.cancellersLock.Lock()
go func() {
select {
case <-time.After(time.Second * 30):
fmt.Println("---- POTENTIAL LOCK LEAK ")
fmt.Println(string(st))
fmt.Println("----")
case <-ch:
}
}()
engine.cancellers[key] = func() {
defer func() {
recover()
}()
cancel()
close(ch)
}
engine.cancellersLock.Unlock()
return ctx, conn, nil
}
func (engine *engine) InstanceLock(im *instanceMemory, timeout time.Duration) (context.Context, error) {
key := im.ID().String()
ctx, conn, err := engine.lock(key, timeout)
if err != nil {
return nil, err
}
im.lock = conn
return ctx, nil
}
func (engine *engine) unlock(key string, conn *sql.Conn) {
hash, err := hashstructure.Hash(key, hashstructure.FormatV2, nil)
if err != nil {
panic(err)
}
engine.cancellersLock.Lock()
defer engine.cancellersLock.Unlock()
cancel := engine.cancellers[key]
delete(engine.cancellers, key)
cancel()
err = engine.locks.unlockDB(hash, conn)
if err != nil {
engine.sugar.Error(err)
return
}
}
func (engine *engine) InstanceUnlock(im *instanceMemory) {
if im.lock == nil {
return
}
engine.unlock(im.ID().String(), im.lock)
im.lock = nil
}