batch: dont expose kvdb.RwTx in batch.SchedulerOptions

Currently, a few of the graph KVStore methods take the
`batch.SchedulerOptions` param. This is only used to set the LazyAdd
option. A SchedulerOption is a functional option that takes a
`batch.Request` which has bolt-specific fields in it. This commit
restructures things a bit such that the `batch.Request` type is no
longer part of the `batch.SchedulerOptions` - this will make it easier
to implement the graph store with a different DB backend.
This commit is contained in:
Elle Mouton
2025-03-30 11:26:02 +02:00
parent 5d7c6c4b60
commit 11b27f07da
3 changed files with 40 additions and 23 deletions

View File

@@ -5,6 +5,9 @@ import "github.com/lightningnetwork/lnd/kvdb"
// Request defines an operation that can be batched into a single bbolt
// transaction.
type Request struct {
// Opts holds various configuration options for a scheduled request.
Opts *SchedulerOptions
// Reset is called before each invocation of Update and is used to clear
// any possible modifications to local state as a result of previous
// calls to Update that were not committed due to a concurrent batch
@@ -25,22 +28,45 @@ type Request struct {
//
// NOTE: This field is optional.
OnCommit func(commitErr error) error
}
// 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
// request when it comes in. This means that it can be scheduled later,
// allowing larger batches.
lazy bool
}
// NewDefaultSchedulerOpts returns a new SchedulerOptions with default values.
func NewDefaultSchedulerOpts() *SchedulerOptions {
return &SchedulerOptions{
lazy: false,
}
}
// NewSchedulerOptions returns a new SchedulerOptions with the given options
// applied on top of the default options.
func NewSchedulerOptions(options ...SchedulerOption) *SchedulerOptions {
opts := NewDefaultSchedulerOpts()
for _, o := range options {
o(opts)
}
return opts
}
// SchedulerOption is a type that can be used to supply options to a scheduled
// request.
type SchedulerOption func(r *Request)
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(r *Request) {
r.lazy = true
return func(opts *SchedulerOptions) {
opts.lazy = true
}
}

View File

@@ -43,6 +43,10 @@ func NewTimeScheduler(db kvdb.Backend, locker sync.Locker,
//
// NOTE: Part of the Scheduler interface.
func (s *TimeScheduler) Execute(r *Request) error {
if r.Opts == nil {
r.Opts = NewDefaultSchedulerOpts()
}
req := request{
Request: r,
errChan: make(chan error, 1),
@@ -62,7 +66,7 @@ func (s *TimeScheduler) Execute(r *Request) error {
s.b.reqs = append(s.b.reqs, &req)
// If this is a non-lazy request, we'll execute the batch immediately.
if !r.lazy {
if !r.Opts.lazy {
go s.b.trigger()
}