1
1
package batch
2
2
3
3
import (
4
+ "context"
4
5
"errors"
5
6
"sync"
6
7
7
- "github.com/lightningnetwork/lnd/kvdb"
8
8
"github.com/lightningnetwork/lnd/sqldb"
9
9
)
10
10
@@ -14,28 +14,44 @@ var errSolo = errors.New(
14
14
"batch function returned an error and should be re-run solo" ,
15
15
)
16
16
17
- type request struct {
18
- * Request
17
+ // txOpts implements the sqldb.TxOptions interface. It is used to indicate that
18
+ // the transaction can be read-only or not transaction.
19
+ type txOpts struct {
20
+ readOnly bool
21
+ }
22
+
23
+ // ReadOnly returns true if the transaction should be read only.
24
+ //
25
+ // NOTE: This is part of the sqldb.TxOptions interface.
26
+ func (t * txOpts ) ReadOnly () bool {
27
+ return t .readOnly
28
+ }
29
+
30
+ type request [Q any ] struct {
31
+ * Request [Q ]
19
32
errChan chan error
20
33
}
21
34
22
- type batch struct {
23
- db kvdb. Backend
35
+ type batch [ Q any ] struct {
36
+ db sqldb. BatchedTx [ Q ]
24
37
start sync.Once
25
- reqs []* request
26
- clear func (b * batch )
38
+ reqs []* request [ Q ]
39
+ clear func (b * batch [ Q ] )
27
40
locker sync.Locker
41
+ txOpts txOpts
28
42
}
29
43
30
44
// trigger is the entry point for the batch and ensures that run is started at
31
45
// most once.
32
- func (b * batch ) trigger () {
33
- b .start .Do (b .run )
46
+ func (b * batch [Q ]) trigger (ctx context.Context ) {
47
+ b .start .Do (func () {
48
+ b .run (ctx )
49
+ })
34
50
}
35
51
36
52
// run executes the current batch of requests. If any individual requests fail
37
53
// alongside others they will be retried by the caller.
38
- func (b * batch ) run () {
54
+ func (b * batch [ Q ] ) run (ctx context. Context ) {
39
55
// Clear the batch from its scheduler, ensuring that no new requests are
40
56
// added to this batch.
41
57
b .clear (b )
@@ -52,9 +68,9 @@ func (b *batch) run() {
52
68
// that fail will be retried individually.
53
69
for len (b .reqs ) > 0 {
54
70
var failIdx = - 1
55
- err := kvdb . Update ( b . db , func (tx kvdb. RwTx ) error {
71
+ err := b . db . ExecTx ( ctx , & b . txOpts , func (tx Q ) error {
56
72
for i , req := range b .reqs {
57
- err := req .Update (tx )
73
+ err := req .Do (tx )
58
74
if err != nil {
59
75
// If we get a serialization error, we
60
76
// want the underlying SQL retry
0 commit comments