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 committed 2025-05-22 14:14:42 +02:00
1 parent 9b9a964975
commit f5a466b051
6 files changed
+122 -52

No files matched your search

+9 -11
View File
@@ -1,10 +1,10 @@
package batch
import "github.com/lightningnetwork/lnd/kvdb"
import "context"
// Request defines an operation that can be batched into a single bbolt
// transaction.
type Request struct {
type Request[Q any] struct {
// Opts holds various configuration options for a scheduled request.
Opts *SchedulerOptions
@@ -19,7 +19,7 @@ type Request struct {
// Update is applied alongside other operations in the batch.
//
// NOTE: This method MUST NOT acquire any mutexes.
Update func(tx kvdb.RwTx) error
Update func(tx Q) error
// OnCommit is called if the batch or a subset of the batch including
// this request all succeeded without failure. The passed error should
@@ -32,16 +32,16 @@ type Request struct {
// SchedulerOptions holds various configuration options for a scheduled request.
type SchedulerOptions struct {
// lazy should be true if we don't have to immediately execute this
// Lazy should be true if we don't have to immediately execute this
// request when it comes in. This means that it can be scheduled later,
// allowing larger batches.
lazy bool
Lazy bool
}
// NewDefaultSchedulerOpts returns a new SchedulerOptions with default values.
func NewDefaultSchedulerOpts() *SchedulerOptions {
return &SchedulerOptions{
lazy: false,
Lazy: false,
}
}
@@ -62,20 +62,18 @@ type SchedulerOption func(*SchedulerOptions)
// LazyAdd will make the request be executed lazily, added to the next batch to
// reduce db contention.
//
// NOTE: This is currently a no-op for any DB backend other than bbolt.
func LazyAdd() SchedulerOption {
return func(opts *SchedulerOptions) {
opts.lazy = true
opts.Lazy = true
}
}
// Scheduler abstracts a generic batching engine that accumulates an incoming
// set of Requests, executes them, and returns the error from the operation.
type Scheduler interface {
type Scheduler[Q any] interface {
// Execute schedules a Request for execution with the next available
// batch. This method blocks until the underlying closure has been
// run against the database. The resulting error is returned to the
// caller.
Execute(req *Request) error
Execute(ctx context.Context, req *Request[Q]) error
}