Skip to content

Commit 67419a7

Browse files
committed
Revert "kvdb/postgres: remove global application level lock"
This reverts commit 43a1ca4.
1 parent a03d5fc commit 67419a7

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

kvdb/postgres/db.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func newPostgresBackend(ctx context.Context, config *Config, prefix string) (
2828
Schema: "public",
2929
TableNamePrefix: prefix,
3030
SQLiteCmdReplacements: sqliteCmdReplacements,
31+
WithTxLevelLock: true,
3132
}
3233

3334
return sqlbase.NewSqlBackend(ctx, cfg)

kvdb/sqlbase/db.go

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ type Config struct {
5555
// commands. Note that the sqlite keywords to be replaced are
5656
// case-sensitive.
5757
SQLiteCmdReplacements SQLiteCmdReplacements
58+
59+
// WithTxLevelLock when set will ensure that there is a transaction
60+
// level lock.
61+
WithTxLevelLock bool
5862
}
5963

6064
// db holds a reference to the sql db connection.
@@ -75,6 +79,10 @@ type db struct {
7579
// db is the underlying database connection instance.
7680
db *sql.DB
7781

82+
// lock is the global write lock that ensures single writer. This is
83+
// only used if cfg.WithTxLevelLock is set.
84+
lock sync.RWMutex
85+
7886
// table is the name of the table that contains the data for all
7987
// top-level buckets that have keys that cannot be mapped to a distinct
8088
// sql table.

kvdb/sqlbase/readwrite_tx.go

+44
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package sqlbase
55
import (
66
"context"
77
"database/sql"
8+
"sync"
89

910
"github.com/btcsuite/btcwallet/walletdb"
1011
)
@@ -19,11 +20,28 @@ type readWriteTx struct {
1920

2021
// active is true if the transaction hasn't been committed yet.
2122
active bool
23+
24+
// locker is a pointer to the global db lock.
25+
locker sync.Locker
2226
}
2327

2428
// newReadWriteTx creates an rw transaction using a connection from the
2529
// specified pool.
2630
func newReadWriteTx(db *db, readOnly bool) (*readWriteTx, error) {
31+
locker := newNoopLocker()
32+
if db.cfg.WithTxLevelLock {
33+
// Obtain the global lock instance. An alternative here is to
34+
// obtain a database lock from Postgres. Unfortunately there is
35+
// no database-level lock in Postgres, meaning that each table
36+
// would need to be locked individually. Perhaps an advisory
37+
// lock could perform this function too.
38+
locker = &db.lock
39+
if readOnly {
40+
locker = db.lock.RLocker()
41+
}
42+
}
43+
locker.Lock()
44+
2745
// Start the transaction. Don't use the timeout context because it would
2846
// be applied to the transaction as a whole. If possible, mark the
2947
// transaction as read-only to make sure that potential programming
@@ -36,13 +54,15 @@ func newReadWriteTx(db *db, readOnly bool) (*readWriteTx, error) {
3654
},
3755
)
3856
if err != nil {
57+
locker.Unlock()
3958
return nil, err
4059
}
4160

4261
return &readWriteTx{
4362
db: db,
4463
tx: tx,
4564
active: true,
65+
locker: locker,
4666
}, nil
4767
}
4868

@@ -74,6 +94,7 @@ func (tx *readWriteTx) Rollback() error {
7494

7595
// Unlock the transaction regardless of the error result.
7696
tx.active = false
97+
tx.locker.Unlock()
7798
return err
7899
}
79100

@@ -141,6 +162,7 @@ func (tx *readWriteTx) Commit() error {
141162

142163
// Unlock the transaction regardless of the error result.
143164
tx.active = false
165+
tx.locker.Unlock()
144166

145167
return err
146168
}
@@ -182,3 +204,25 @@ func (tx *readWriteTx) Exec(query string, args ...interface{}) (sql.Result,
182204

183205
return tx.tx.ExecContext(ctx, query, args...)
184206
}
207+
208+
// noopLocker is an implementation of a no-op sync.Locker.
209+
type noopLocker struct{}
210+
211+
// newNoopLocker creates a new noopLocker.
212+
func newNoopLocker() sync.Locker {
213+
return &noopLocker{}
214+
}
215+
216+
// Lock is a noop.
217+
//
218+
// NOTE: this is part of the sync.Locker interface.
219+
func (n *noopLocker) Lock() {
220+
}
221+
222+
// Unlock is a noop.
223+
//
224+
// NOTE: this is part of the sync.Locker interface.
225+
func (n *noopLocker) Unlock() {
226+
}
227+
228+
var _ sync.Locker = (*noopLocker)(nil)

0 commit comments

Comments
 (0)