mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-31 17:51:33 +02:00
multi: add a re-usable TxOptions type
Add a re-usable implementation of the sqldb.TxOptions interface and make use of this in the various spots (invoices, batch and graph/db) where we had previously defined individual implementations that were all doing the same thing.
This commit is contained in:
@@ -14,19 +14,6 @@ var errSolo = errors.New(
|
||||
"batch function returned an error and should be re-run solo",
|
||||
)
|
||||
|
||||
// txOpts implements the sqldb.TxOptions interface. It is used to indicate that
|
||||
// the transaction can be read-only or not transaction.
|
||||
type txOpts struct {
|
||||
readOnly bool
|
||||
}
|
||||
|
||||
// ReadOnly returns true if the transaction should be read only.
|
||||
//
|
||||
// NOTE: This is part of the sqldb.TxOptions interface.
|
||||
func (t *txOpts) ReadOnly() bool {
|
||||
return t.readOnly
|
||||
}
|
||||
|
||||
type request[Q any] struct {
|
||||
*Request[Q]
|
||||
errChan chan error
|
||||
@@ -38,7 +25,7 @@ type batch[Q any] struct {
|
||||
reqs []*request[Q]
|
||||
clear func(b *batch[Q])
|
||||
locker sync.Locker
|
||||
txOpts txOpts
|
||||
txOpts sqldb.TxOptions
|
||||
}
|
||||
|
||||
// trigger is the entry point for the batch and ensures that run is started at
|
||||
@@ -68,7 +55,7 @@ func (b *batch[Q]) run(ctx context.Context) {
|
||||
// that fail will be retried individually.
|
||||
for len(b.reqs) > 0 {
|
||||
var failIdx = -1
|
||||
err := b.db.ExecTx(ctx, &b.txOpts, func(tx Q) error {
|
||||
err := b.db.ExecTx(ctx, b.txOpts, func(tx Q) error {
|
||||
for i, req := range b.reqs {
|
||||
err := req.Do(tx)
|
||||
if err != nil {
|
||||
|
@@ -550,7 +550,7 @@ func benchmarkSQLBatching(b *testing.B, sqlite bool) {
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
var opts txOpts
|
||||
opts := sqldb.WriteTxOpt()
|
||||
|
||||
// writeRecord is a helper that adds a single new invoice to the
|
||||
// database. It uses the 'i' argument to create a unique hash for the
|
||||
@@ -578,13 +578,12 @@ func benchmarkSQLBatching(b *testing.B, sqlite bool) {
|
||||
var hash [8]byte
|
||||
binary.BigEndian.PutUint64(hash[:], uint64(N-1))
|
||||
|
||||
err := tx.ExecTx(
|
||||
ctx, &txOpts{}, func(queries *sqlc.Queries) error {
|
||||
_, err := queries.GetInvoiceByHash(ctx, hash[:])
|
||||
require.NoError(b, err)
|
||||
err := tx.ExecTx(ctx, opts, func(queries *sqlc.Queries) error {
|
||||
_, err := queries.GetInvoiceByHash(ctx, hash[:])
|
||||
require.NoError(b, err)
|
||||
|
||||
return nil
|
||||
}, func() {},
|
||||
return nil
|
||||
}, func() {},
|
||||
)
|
||||
require.NoError(b, err)
|
||||
}
|
||||
@@ -602,7 +601,7 @@ func benchmarkSQLBatching(b *testing.B, sqlite bool) {
|
||||
defer wg.Done()
|
||||
|
||||
err := db.ExecTx(
|
||||
ctx, &opts,
|
||||
ctx, opts,
|
||||
func(tx *sqlc.Queries) error {
|
||||
writeRecord(b, tx, int64(j))
|
||||
return nil
|
||||
@@ -624,7 +623,7 @@ func benchmarkSQLBatching(b *testing.B, sqlite bool) {
|
||||
b.ResetTimer()
|
||||
|
||||
err := db.ExecTx(
|
||||
ctx, &opts,
|
||||
ctx, opts,
|
||||
func(tx *sqlc.Queries) error {
|
||||
for i := 0; i < b.N; i++ {
|
||||
writeRecord(b, tx, int64(i))
|
||||
|
@@ -65,9 +65,7 @@ func (s *TimeScheduler[Q]) Execute(ctx context.Context, r *Request[Q]) error {
|
||||
// By default, we assume that the batch is read-only,
|
||||
// and we only upgrade it to read-write if a request
|
||||
// is added that is not read-only.
|
||||
txOpts: txOpts{
|
||||
readOnly: true,
|
||||
},
|
||||
txOpts: sqldb.ReadTxOpt(),
|
||||
}
|
||||
trigger := s.b.trigger
|
||||
time.AfterFunc(s.duration, func() {
|
||||
@@ -78,8 +76,8 @@ func (s *TimeScheduler[Q]) Execute(ctx context.Context, r *Request[Q]) error {
|
||||
|
||||
// We only upgrade the batch to read-write if the new request is not
|
||||
// read-only. If it is already read-write, we don't need to do anything.
|
||||
if s.b.txOpts.readOnly && !r.Opts.ReadOnly {
|
||||
s.b.txOpts.readOnly = false
|
||||
if s.b.txOpts.ReadOnly() && !r.Opts.ReadOnly {
|
||||
s.b.txOpts = sqldb.WriteTxOpt()
|
||||
}
|
||||
|
||||
// If this is a non-lazy request, we'll execute the batch immediately.
|
||||
@@ -109,7 +107,7 @@ func (s *TimeScheduler[Q]) Execute(ctx context.Context, r *Request[Q]) error {
|
||||
}
|
||||
|
||||
// Otherwise, run the request on its own.
|
||||
commitErr := s.db.ExecTx(ctx, &txOpts, func(tx Q) error {
|
||||
commitErr := s.db.ExecTx(ctx, txOpts, func(tx Q) error {
|
||||
return req.Do(tx)
|
||||
}, func() {
|
||||
if req.Reset != nil {
|
||||
|
Reference in New Issue
Block a user