/
locking_strategies.go
148 lines (128 loc) · 3.46 KB
/
locking_strategies.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
package orm
import (
"context"
"database/sql"
"fmt"
"path/filepath"
"sync"
"time"
"github.com/gofrs/flock"
"go.uber.org/multierr"
)
// NewLockingStrategy returns the locking strategy for a particular dialect
// to ensure exlusive access to the orm.
func NewLockingStrategy(dialect DialectName, dbpath string) (LockingStrategy, error) {
switch dialect {
case DialectPostgres:
return NewPostgresLockingStrategy(dbpath)
case DialectSqlite:
return NewFileLockingStrategy(dbpath)
}
return nil, fmt.Errorf("unable to create locking strategy for dialect %s and path %s", dialect, dbpath)
}
// LockingStrategy employs the locking and unlocking of an underlying
// resource for exclusive access, usually a file or database.
type LockingStrategy interface {
Lock(timeout time.Duration) error
Unlock() error
}
// FileLockingStrategy uses a file lock on disk to ensure exclusive access.
type FileLockingStrategy struct {
path string
fileLock *flock.Flock
m *sync.Mutex
}
// NewFileLockingStrategy creates a new instance of FileLockingStrategy
// at the passed path.
func NewFileLockingStrategy(dbpath string) (LockingStrategy, error) {
directory := filepath.Dir(dbpath)
lockPath := filepath.Join(directory, "chainlink.lock")
return &FileLockingStrategy{
path: lockPath,
fileLock: flock.New(lockPath),
m: &sync.Mutex{},
}, nil
}
// Lock returns immediately and assumes is always unlocked.
func (s *FileLockingStrategy) Lock(timeout time.Duration) error {
s.m.Lock()
defer s.m.Unlock()
var err error
locked := make(chan struct{})
go func() {
err = s.fileLock.Lock()
close(locked)
}()
select {
case <-locked:
case <-normalizedTimeout(timeout):
return fmt.Errorf("file locking strategy timed out for %s", s.path)
}
return err
}
func normalizedTimeout(timeout time.Duration) <-chan time.Time {
if timeout == 0 {
return make(chan time.Time) // never time out
}
return time.After(timeout)
}
// Unlock is a noop.
func (s *FileLockingStrategy) Unlock() error {
s.m.Lock()
defer s.m.Unlock()
return s.fileLock.Unlock()
}
// PostgresLockingStrategy uses a postgres advisory lock to ensure exclusive
// access.
type PostgresLockingStrategy struct {
db *sql.DB
path string
m *sync.Mutex
locked bool
}
// NewPostgresLockingStrategy returns a new instance of the PostgresLockingStrategy.
func NewPostgresLockingStrategy(path string) (LockingStrategy, error) {
return &PostgresLockingStrategy{
m: &sync.Mutex{},
path: path,
}, nil
}
const postgresAdvisoryLockID int64 = 1027321974924625846
// Lock uses a blocking postgres advisory lock that times out at the passed
// timeout.
func (s *PostgresLockingStrategy) Lock(timeout time.Duration) error {
s.m.Lock()
defer s.m.Unlock()
if s.locked {
return nil
}
db, err := sql.Open(string(DialectPostgres), s.path)
if err != nil {
return err
}
s.db = db
ctx := context.Background()
if timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
_, err = s.db.ExecContext(ctx, "SELECT pg_advisory_lock($1)", postgresAdvisoryLockID)
if err != nil {
return multierr.Append(
fmt.Errorf("postgres advisory locking strategy failed, timeout set to %v", displayTimeout(timeout)),
err)
}
s.locked = true
return nil
}
// Unlock unlocks the locked postgres advisory lock.
func (s *PostgresLockingStrategy) Unlock() error {
s.m.Lock()
defer s.m.Unlock()
s.locked = false
if s.db != nil {
return s.db.Close()
}
return nil
}