batch+graph: update batch.Schedular to be generic

In preparation for using the same logic for non-bbolt backends, we adapt
the batch.Schedular to be more generic.

The only user of the scheduler at the moment is the KVStore in the
`graph.db` package. This store instantiates the bbolt implementation of
the scheduler.
This commit is contained in:
Elle Mouton
2025-05-21 08:07:40 +02:00
parent 9b9a964975
commit f5a466b051
6 changed files with 122 additions and 52 deletions

View File

@@ -1,10 +1,10 @@
package batch
import (
"context"
"errors"
"sync"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/sqldb"
)
@@ -14,28 +14,30 @@ var errSolo = errors.New(
"batch function returned an error and should be re-run solo",
)
type request struct {
*Request
type request[Q any] struct {
*Request[Q]
errChan chan error
}
type batch struct {
db kvdb.Backend
type batch[Q any] struct {
db sqldb.BatchedTx[Q]
start sync.Once
reqs []*request
clear func(b *batch)
reqs []*request[Q]
clear func(b *batch[Q])
locker sync.Locker
}
// trigger is the entry point for the batch and ensures that run is started at
// most once.
func (b *batch) trigger() {
b.start.Do(b.run)
func (b *batch[Q]) trigger(ctx context.Context) {
b.start.Do(func() {
b.run(ctx)
})
}
// run executes the current batch of requests. If any individual requests fail
// alongside others they will be retried by the caller.
func (b *batch) run() {
func (b *batch[Q]) run(ctx context.Context) {
// Clear the batch from its scheduler, ensuring that no new requests are
// added to this batch.
b.clear(b)
@@ -50,9 +52,10 @@ func (b *batch) run() {
// Apply the batch until a subset succeeds or all of them fail. Requests
// that fail will be retried individually.
var writeTx writeOpts
for len(b.reqs) > 0 {
var failIdx = -1
err := kvdb.Update(b.db, func(tx kvdb.RwTx) error {
err := b.db.ExecTx(ctx, &writeTx, func(tx Q) error {
for i, req := range b.reqs {
err := req.Update(tx)
if err != nil {