-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.go
160 lines (138 loc) · 2.86 KB
/
postgresql.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
package looper
import (
"context"
"database/sql"
"fmt"
"time"
)
const defaultTableName = "looper_lock"
// PostgresLocker provides an implementation of the Locker interface using
// a PostgreSQL table for storage.
func PostgresLocker(ctx context.Context, db *sql.DB, tableName string) (locker, error) {
if err := db.PingContext(ctx); err != nil {
return nil, fmt.Errorf("%w: %v", ErrFailedToConnectToLocker, err)
}
if tableName == "" {
tableName = defaultTableName
}
// Ensure the lock table exists, create it if necessary
if err := createLockTable(ctx, db, tableName); err != nil {
return nil, err
}
pl := &postgresLocker{
db: db,
table: tableName,
}
return pl, nil
}
// Locker
var _ locker = (*postgresLocker)(nil)
type postgresLocker struct {
db *sql.DB
table string
}
func createLockTable(ctx context.Context, db *sql.DB, table string) error {
var tableExists bool
err := db.QueryRowContext(
ctx,
fmt.Sprintf(`
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_name = '%s'
);`,
table,
),
).Scan(&tableExists)
if err != nil {
return fmt.Errorf("%w: %v", ErrFailedToCheckLockExistence, err)
}
if !tableExists {
_, err := db.ExecContext(
ctx,
fmt.Sprintf(`
CREATE TABLE %s (
job_name VARCHAR(255) PRIMARY KEY,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT (now() AT TIME ZONE 'UTC')
);`,
table,
))
if err != nil {
return fmt.Errorf("%w: %v", ErrFailedToCreateLockTable, err)
}
}
return nil
}
func (p *postgresLocker) lock(
ctx context.Context,
key string,
timeout time.Duration,
) (lock, error) {
// Create a row in the lock table to acquire the lock
_, err := p.db.ExecContext(
ctx,
fmt.Sprintf(`
INSERT INTO %s (job_name)
VALUES ('%s');`,
p.table,
key,
))
if err != nil {
var createdAt time.Time
err := p.db.QueryRowContext(
ctx,
fmt.Sprintf(`
SELECT created_at
FROM %s
WHERE job_name = '%s';`,
p.table,
key,
)).Scan(&createdAt)
if err != nil {
return nil, ErrFailedToCheckLockExistence
}
if createdAt.Before(time.Now().Add(-timeout)) {
_, err := p.db.ExecContext(
ctx,
fmt.Sprintf(`
DELETE FROM %s
WHERE job_name = '%s';`,
p.table,
key,
))
if err != nil {
return nil, ErrFailedToReleaseLock
}
return p.lock(ctx, key, timeout)
}
return nil, ErrFailedToObtainLock
}
pl := &postgresLock{
db: p.db,
table: p.table,
key: key,
}
return pl, nil
}
// Lock
var _ lock = (*postgresLock)(nil)
type postgresLock struct {
db *sql.DB
table string
key string
}
func (p *postgresLock) unlock(ctx context.Context) error {
// Release the lock by deleting the row
_, err := p.db.ExecContext(
ctx,
fmt.Sprintf(`
DELETE FROM %s
WHERE job_name = '%s';`,
p.table,
p.key,
))
if err != nil {
return ErrFailedToReleaseLock
}
return nil
}