Skip to content

Commit 566bd84

Browse files
authored
Merge pull request #9790 from ellemouton/graphSQL2-hideKVDBOpts
batch: dont expose kvdb.RwTx in batch.SchedulerOptions
2 parents 695cf7c + 2dd50de commit 566bd84

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

batch/interface.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import "github.com/lightningnetwork/lnd/kvdb"
55
// Request defines an operation that can be batched into a single bbolt
66
// transaction.
77
type Request struct {
8+
// Opts holds various configuration options for a scheduled request.
9+
Opts *SchedulerOptions
10+
811
// Reset is called before each invocation of Update and is used to clear
912
// any possible modifications to local state as a result of previous
1013
// calls to Update that were not committed due to a concurrent batch
@@ -25,22 +28,45 @@ type Request struct {
2528
//
2629
// NOTE: This field is optional.
2730
OnCommit func(commitErr error) error
31+
}
2832

33+
// SchedulerOptions holds various configuration options for a scheduled request.
34+
type SchedulerOptions struct {
2935
// lazy should be true if we don't have to immediately execute this
3036
// request when it comes in. This means that it can be scheduled later,
3137
// allowing larger batches.
3238
lazy bool
3339
}
3440

41+
// NewDefaultSchedulerOpts returns a new SchedulerOptions with default values.
42+
func NewDefaultSchedulerOpts() *SchedulerOptions {
43+
return &SchedulerOptions{
44+
lazy: false,
45+
}
46+
}
47+
48+
// NewSchedulerOptions returns a new SchedulerOptions with the given options
49+
// applied on top of the default options.
50+
func NewSchedulerOptions(options ...SchedulerOption) *SchedulerOptions {
51+
opts := NewDefaultSchedulerOpts()
52+
for _, o := range options {
53+
o(opts)
54+
}
55+
56+
return opts
57+
}
58+
3559
// SchedulerOption is a type that can be used to supply options to a scheduled
3660
// request.
37-
type SchedulerOption func(r *Request)
61+
type SchedulerOption func(*SchedulerOptions)
3862

3963
// LazyAdd will make the request be executed lazily, added to the next batch to
4064
// reduce db contention.
65+
//
66+
// NOTE: This is currently a no-op for any DB backend other than bbolt.
4167
func LazyAdd() SchedulerOption {
42-
return func(r *Request) {
43-
r.lazy = true
68+
return func(opts *SchedulerOptions) {
69+
opts.lazy = true
4470
}
4571
}
4672

batch/scheduler.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func NewTimeScheduler(db kvdb.Backend, locker sync.Locker,
4343
//
4444
// NOTE: Part of the Scheduler interface.
4545
func (s *TimeScheduler) Execute(r *Request) error {
46+
if r.Opts == nil {
47+
r.Opts = NewDefaultSchedulerOpts()
48+
}
49+
4650
req := request{
4751
Request: r,
4852
errChan: make(chan error, 1),
@@ -62,7 +66,7 @@ func (s *TimeScheduler) Execute(r *Request) error {
6266
s.b.reqs = append(s.b.reqs, &req)
6367

6468
// If this is a non-lazy request, we'll execute the batch immediately.
65-
if !r.lazy {
69+
if !r.Opts.lazy {
6670
go s.b.trigger()
6771
}
6872

graph/db/kv_store.go

+6-19
Original file line numberDiff line numberDiff line change
@@ -848,18 +848,15 @@ func (c *KVStore) SetSourceNode(node *models.LightningNode) error {
848848
//
849849
// TODO(roasbeef): also need sig of announcement.
850850
func (c *KVStore) AddLightningNode(node *models.LightningNode,
851-
op ...batch.SchedulerOption) error {
851+
opts ...batch.SchedulerOption) error {
852852

853853
r := &batch.Request{
854+
Opts: batch.NewSchedulerOptions(opts...),
854855
Update: func(tx kvdb.RwTx) error {
855856
return addLightningNode(tx, node)
856857
},
857858
}
858859

859-
for _, f := range op {
860-
f(r)
861-
}
862-
863860
return c.nodeScheduler.Execute(r)
864861
}
865862

@@ -986,10 +983,11 @@ func (c *KVStore) deleteLightningNode(nodes kvdb.RwBucket,
986983
// supports. The chanPoint and chanID are used to uniquely identify the edge
987984
// globally within the database.
988985
func (c *KVStore) AddChannelEdge(edge *models.ChannelEdgeInfo,
989-
op ...batch.SchedulerOption) error {
986+
opts ...batch.SchedulerOption) error {
990987

991988
var alreadyExists bool
992989
r := &batch.Request{
990+
Opts: batch.NewSchedulerOptions(opts...),
993991
Reset: func() {
994992
alreadyExists = false
995993
},
@@ -1019,14 +1017,6 @@ func (c *KVStore) AddChannelEdge(edge *models.ChannelEdgeInfo,
10191017
},
10201018
}
10211019

1022-
for _, f := range op {
1023-
if f == nil {
1024-
return fmt.Errorf("nil scheduler option was used")
1025-
}
1026-
1027-
f(r)
1028-
}
1029-
10301020
return c.chanScheduler.Execute(r)
10311021
}
10321022

@@ -2696,7 +2686,7 @@ func makeZombiePubkeys(info *models.ChannelEdgeInfo,
26962686
// determined by the lexicographical ordering of the identity public keys of the
26972687
// nodes on either side of the channel.
26982688
func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
2699-
op ...batch.SchedulerOption) (route.Vertex, route.Vertex, error) {
2689+
opts ...batch.SchedulerOption) (route.Vertex, route.Vertex, error) {
27002690

27012691
var (
27022692
isUpdate1 bool
@@ -2705,6 +2695,7 @@ func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
27052695
)
27062696

27072697
r := &batch.Request{
2698+
Opts: batch.NewSchedulerOptions(opts...),
27082699
Reset: func() {
27092700
isUpdate1 = false
27102701
edgeNotFound = false
@@ -2738,10 +2729,6 @@ func (c *KVStore) UpdateEdgePolicy(edge *models.ChannelEdgePolicy,
27382729
},
27392730
}
27402731

2741-
for _, f := range op {
2742-
f(r)
2743-
}
2744-
27452732
err := c.chanScheduler.Execute(r)
27462733

27472734
return from, to, err

0 commit comments

Comments
 (0)