etcd: optionally reduce concurrency to single writer for legacy code
This commit is contained in:
5 files changed
+70
-17
No files matched your search
@@ -3,6 +3,8 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/btcsuite/btcwallet/walletdb"
|
||||
)
|
||||
|
||||
@@ -17,13 +19,18 @@ type readWriteTx struct {
|
||||
|
||||
// active is true if the transaction hasn't been committed yet.
|
||||
active bool
|
||||
|
||||
// lock is passed on for manual txns when the backend is instantiated
|
||||
// such that we read/write lock transactions to ensure a single writer.
|
||||
lock sync.Locker
|
||||
}
|
||||
|
||||
// newReadWriteTx creates an rw transaction with the passed STM.
|
||||
func newReadWriteTx(stm STM, prefix string) *readWriteTx {
|
||||
func newReadWriteTx(stm STM, prefix string, lock sync.Locker) *readWriteTx {
|
||||
return &readWriteTx{
|
||||
stm: stm,
|
||||
active: true,
|
||||
lock: lock,
|
||||
rootBucketID: makeBucketID([]byte(prefix)),
|
||||
}
|
||||
}
|
||||
@@ -65,6 +72,10 @@ func (tx *readWriteTx) Rollback() error {
|
||||
return walletdb.ErrTxClosed
|
||||
}
|
||||
|
||||
if tx.lock != nil {
|
||||
defer tx.lock.Unlock()
|
||||
}
|
||||
|
||||
// Rollback the STM and set the tx to inactive.
|
||||
tx.stm.Rollback()
|
||||
tx.active = false
|
||||
@@ -99,6 +110,10 @@ func (tx *readWriteTx) Commit() error {
|
||||
return walletdb.ErrTxClosed
|
||||
}
|
||||
|
||||
if tx.lock != nil {
|
||||
defer tx.lock.Unlock()
|
||||
}
|
||||
|
||||
// Try committing the transaction.
|
||||
if err := tx.stm.Commit(); err != nil {
|
||||
return err
|
||||
|
||||
Reference in new issue
Block a user