Skip to content

Commit 3a25952

Browse files
authored
Merge pull request #163 from bhandras/challenger-batchsize-config
challenger: configurable invoice query batch size with sane default
2 parents 4d2bc1d + 94e80ab commit 3a25952

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

aperture.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ func (a *Aperture) Start(errChan chan error) error {
336336
}
337337

338338
a.challenger, err = challenger.NewLNCChallenger(
339-
session, lncStore, genInvoiceReq, errChan,
339+
session, lncStore, a.cfg.InvoiceBatchSize,
340+
genInvoiceReq, errChan,
340341
)
341342
if err != nil {
342343
return fmt.Errorf("unable to start lnc "+
@@ -359,8 +360,8 @@ func (a *Aperture) Start(errChan chan error) error {
359360
}
360361

361362
a.challenger, err = challenger.NewLndChallenger(
362-
client, genInvoiceReq, context.Background,
363-
errChan,
363+
client, a.cfg.InvoiceBatchSize, genInvoiceReq,
364+
context.Background, errChan,
364365
)
365366
if err != nil {
366367
return err

challenger/lnc.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type LNCChallenger struct {
1919
// NewLNCChallenger creates a new challenger that uses the given LNC session to
2020
// connect to an lnd backend to create payment challenges.
2121
func NewLNCChallenger(session *lnc.Session, lncStore lnc.Store,
22-
genInvoiceReq InvoiceRequestGenerator,
22+
invoiceBatchSize int, genInvoiceReq InvoiceRequestGenerator,
2323
errChan chan<- error) (*LNCChallenger, error) {
2424

2525
nodeConn, err := lnc.NewNodeConn(session, lncStore)
@@ -34,7 +34,8 @@ func NewLNCChallenger(session *lnc.Session, lncStore lnc.Store,
3434
}
3535

3636
lndChallenger, err := NewLndChallenger(
37-
client, genInvoiceReq, nodeConn.CtxFunc, errChan,
37+
client, invoiceBatchSize, genInvoiceReq, nodeConn.CtxFunc,
38+
errChan,
3839
)
3940
if err != nil {
4041
return nil, err

challenger/lnd.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@ import (
1212
"github.com/lightningnetwork/lnd/lntypes"
1313
)
1414

15-
const (
16-
// invoiceQueryPageSize is the maximum number of invoices that will be
17-
// queried in a single request.
18-
invoiceQueryPageSize = 1000
19-
)
20-
2115
// LndChallenger is a challenger that uses an lnd backend to create new L402
2216
// payment challenges.
2317
type LndChallenger struct {
2418
client InvoiceClient
19+
batchSize int
2520
clientCtx func() context.Context
2621
genInvoiceReq InvoiceRequestGenerator
2722

@@ -42,7 +37,7 @@ var _ Challenger = (*LndChallenger)(nil)
4237

4338
// NewLndChallenger creates a new challenger that uses the given connection to
4439
// an lnd backend to create payment challenges.
45-
func NewLndChallenger(client InvoiceClient,
40+
func NewLndChallenger(client InvoiceClient, batchSize int,
4641
genInvoiceReq InvoiceRequestGenerator,
4742
ctxFunc func() context.Context,
4843
errChan chan<- error) (*LndChallenger, error) {
@@ -60,6 +55,7 @@ func NewLndChallenger(client InvoiceClient,
6055
invoicesMtx := &sync.Mutex{}
6156
challenger := &LndChallenger{
6257
client: client,
58+
batchSize: batchSize,
6359
clientCtx: ctxFunc,
6460
genInvoiceReq: genInvoiceReq,
6561
invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState),
@@ -100,7 +96,7 @@ func (l *LndChallenger) Start() error {
10096
invoiceResp, err := l.client.ListInvoices(
10197
ctx, &lnrpc.ListInvoiceRequest{
10298
IndexOffset: indexOffset,
103-
NumMaxInvoices: invoiceQueryPageSize,
99+
NumMaxInvoices: uint64(l.batchSize),
104100
},
105101
)
106102
if err != nil {

challenger/lnd_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) {
112112
mainErrChan := make(chan error)
113113
return &LndChallenger{
114114
client: mockClient,
115+
batchSize: 1,
115116
clientCtx: context.Background,
116117
genInvoiceReq: genInvoiceReq,
117118
invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState),
@@ -141,7 +142,7 @@ func TestLndChallenger(t *testing.T) {
141142
// First of all, test that the NewLndChallenger doesn't allow a nil
142143
// invoice generator function.
143144
errChan := make(chan error)
144-
_, err := NewLndChallenger(nil, nil, nil, errChan)
145+
_, err := NewLndChallenger(nil, 1, nil, nil, errChan)
145146
require.Error(t, err)
146147

147148
// Now mock the lnd backend and create a challenger instance that we can

config.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import (
1212
)
1313

1414
var (
15-
apertureDataDir = btcutil.AppDataDir("aperture", false)
16-
defaultConfigFilename = "aperture.yaml"
17-
defaultTLSKeyFilename = "tls.key"
18-
defaultTLSCertFilename = "tls.cert"
19-
defaultLogLevel = "info"
20-
defaultLogFilename = "aperture.log"
21-
defaultMaxLogFiles = 3
22-
defaultMaxLogFileSize = 10
15+
apertureDataDir = btcutil.AppDataDir("aperture", false)
16+
defaultConfigFilename = "aperture.yaml"
17+
defaultTLSKeyFilename = "tls.key"
18+
defaultTLSCertFilename = "tls.cert"
19+
defaultLogLevel = "info"
20+
defaultLogFilename = "aperture.log"
21+
defaultMaxLogFiles = 3
22+
defaultMaxLogFileSize = 10
23+
defaultInvoiceBatchSize = 100000
2324

2425
defaultSqliteDatabaseFileName = "aperture.db"
2526

@@ -219,6 +220,10 @@ type Config struct {
219220
// WriteTimeout is the maximum amount of time to wait for a response to
220221
// be fully written.
221222
WriteTimeout time.Duration `long:"writetimeout" description:"The maximum amount of time to wait for a response to be fully written."`
223+
224+
// InvoiceBatchSize is the number of invoices to fetch in a single
225+
// request.
226+
InvoiceBatchSize int `long:"invoicebatchsize" description:"The number of invoices to fetch in a single request."`
222227
}
223228

224229
func (c *Config) validate() error {
@@ -232,6 +237,10 @@ func (c *Config) validate() error {
232237
return fmt.Errorf("missing listen address for server")
233238
}
234239

240+
if c.InvoiceBatchSize <= 0 {
241+
return fmt.Errorf("invoice batch size must be greater than 0")
242+
}
243+
235244
return nil
236245
}
237246

@@ -246,16 +255,17 @@ func DefaultSqliteConfig() *aperturedb.SqliteConfig {
246255
// NewConfig initializes a new Config variable.
247256
func NewConfig() *Config {
248257
return &Config{
249-
DatabaseBackend: "etcd",
250-
Etcd: &EtcdConfig{},
251-
Sqlite: DefaultSqliteConfig(),
252-
Postgres: &aperturedb.PostgresConfig{},
253-
Authenticator: &AuthConfig{},
254-
Tor: &TorConfig{},
255-
HashMail: &HashMailConfig{},
256-
Prometheus: &PrometheusConfig{},
257-
IdleTimeout: defaultIdleTimeout,
258-
ReadTimeout: defaultReadTimeout,
259-
WriteTimeout: defaultWriteTimeout,
258+
DatabaseBackend: "etcd",
259+
Etcd: &EtcdConfig{},
260+
Sqlite: DefaultSqliteConfig(),
261+
Postgres: &aperturedb.PostgresConfig{},
262+
Authenticator: &AuthConfig{},
263+
Tor: &TorConfig{},
264+
HashMail: &HashMailConfig{},
265+
Prometheus: &PrometheusConfig{},
266+
IdleTimeout: defaultIdleTimeout,
267+
ReadTimeout: defaultReadTimeout,
268+
WriteTimeout: defaultWriteTimeout,
269+
InvoiceBatchSize: defaultInvoiceBatchSize,
260270
}
261271
}

0 commit comments

Comments
 (0)