Skip to content

Commit

Permalink
Merge pull request #163 from bhandras/challenger-batchsize-config
Browse files Browse the repository at this point in the history
challenger: configurable invoice query batch size with sane default
  • Loading branch information
bhandras authored Jan 16, 2025
2 parents 4d2bc1d + 94e80ab commit 3a25952
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
7 changes: 4 additions & 3 deletions aperture.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ func (a *Aperture) Start(errChan chan error) error {
}

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

a.challenger, err = challenger.NewLndChallenger(
client, genInvoiceReq, context.Background,
errChan,
client, a.cfg.InvoiceBatchSize, genInvoiceReq,
context.Background, errChan,
)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions challenger/lnc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type LNCChallenger struct {
// NewLNCChallenger creates a new challenger that uses the given LNC session to
// connect to an lnd backend to create payment challenges.
func NewLNCChallenger(session *lnc.Session, lncStore lnc.Store,
genInvoiceReq InvoiceRequestGenerator,
invoiceBatchSize int, genInvoiceReq InvoiceRequestGenerator,
errChan chan<- error) (*LNCChallenger, error) {

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

lndChallenger, err := NewLndChallenger(
client, genInvoiceReq, nodeConn.CtxFunc, errChan,
client, invoiceBatchSize, genInvoiceReq, nodeConn.CtxFunc,
errChan,
)
if err != nil {
return nil, err
Expand Down
12 changes: 4 additions & 8 deletions challenger/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ import (
"github.com/lightningnetwork/lnd/lntypes"
)

const (
// invoiceQueryPageSize is the maximum number of invoices that will be
// queried in a single request.
invoiceQueryPageSize = 1000
)

// LndChallenger is a challenger that uses an lnd backend to create new L402
// payment challenges.
type LndChallenger struct {
client InvoiceClient
batchSize int
clientCtx func() context.Context
genInvoiceReq InvoiceRequestGenerator

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

// NewLndChallenger creates a new challenger that uses the given connection to
// an lnd backend to create payment challenges.
func NewLndChallenger(client InvoiceClient,
func NewLndChallenger(client InvoiceClient, batchSize int,
genInvoiceReq InvoiceRequestGenerator,
ctxFunc func() context.Context,
errChan chan<- error) (*LndChallenger, error) {
Expand All @@ -60,6 +55,7 @@ func NewLndChallenger(client InvoiceClient,
invoicesMtx := &sync.Mutex{}
challenger := &LndChallenger{
client: client,
batchSize: batchSize,
clientCtx: ctxFunc,
genInvoiceReq: genInvoiceReq,
invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState),
Expand Down Expand Up @@ -100,7 +96,7 @@ func (l *LndChallenger) Start() error {
invoiceResp, err := l.client.ListInvoices(
ctx, &lnrpc.ListInvoiceRequest{
IndexOffset: indexOffset,
NumMaxInvoices: invoiceQueryPageSize,
NumMaxInvoices: uint64(l.batchSize),
},
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion challenger/lnd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) {
mainErrChan := make(chan error)
return &LndChallenger{
client: mockClient,
batchSize: 1,
clientCtx: context.Background,
genInvoiceReq: genInvoiceReq,
invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState),
Expand Down Expand Up @@ -141,7 +142,7 @@ func TestLndChallenger(t *testing.T) {
// First of all, test that the NewLndChallenger doesn't allow a nil
// invoice generator function.
errChan := make(chan error)
_, err := NewLndChallenger(nil, nil, nil, errChan)
_, err := NewLndChallenger(nil, 1, nil, nil, errChan)
require.Error(t, err)

// Now mock the lnd backend and create a challenger instance that we can
Expand Down
48 changes: 29 additions & 19 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
)

var (
apertureDataDir = btcutil.AppDataDir("aperture", false)
defaultConfigFilename = "aperture.yaml"
defaultTLSKeyFilename = "tls.key"
defaultTLSCertFilename = "tls.cert"
defaultLogLevel = "info"
defaultLogFilename = "aperture.log"
defaultMaxLogFiles = 3
defaultMaxLogFileSize = 10
apertureDataDir = btcutil.AppDataDir("aperture", false)
defaultConfigFilename = "aperture.yaml"
defaultTLSKeyFilename = "tls.key"
defaultTLSCertFilename = "tls.cert"
defaultLogLevel = "info"
defaultLogFilename = "aperture.log"
defaultMaxLogFiles = 3
defaultMaxLogFileSize = 10
defaultInvoiceBatchSize = 100000

defaultSqliteDatabaseFileName = "aperture.db"

Expand Down Expand Up @@ -219,6 +220,10 @@ type Config struct {
// WriteTimeout is the maximum amount of time to wait for a response to
// be fully written.
WriteTimeout time.Duration `long:"writetimeout" description:"The maximum amount of time to wait for a response to be fully written."`

// InvoiceBatchSize is the number of invoices to fetch in a single
// request.
InvoiceBatchSize int `long:"invoicebatchsize" description:"The number of invoices to fetch in a single request."`
}

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

if c.InvoiceBatchSize <= 0 {
return fmt.Errorf("invoice batch size must be greater than 0")
}

return nil
}

Expand All @@ -246,16 +255,17 @@ func DefaultSqliteConfig() *aperturedb.SqliteConfig {
// NewConfig initializes a new Config variable.
func NewConfig() *Config {
return &Config{
DatabaseBackend: "etcd",
Etcd: &EtcdConfig{},
Sqlite: DefaultSqliteConfig(),
Postgres: &aperturedb.PostgresConfig{},
Authenticator: &AuthConfig{},
Tor: &TorConfig{},
HashMail: &HashMailConfig{},
Prometheus: &PrometheusConfig{},
IdleTimeout: defaultIdleTimeout,
ReadTimeout: defaultReadTimeout,
WriteTimeout: defaultWriteTimeout,
DatabaseBackend: "etcd",
Etcd: &EtcdConfig{},
Sqlite: DefaultSqliteConfig(),
Postgres: &aperturedb.PostgresConfig{},
Authenticator: &AuthConfig{},
Tor: &TorConfig{},
HashMail: &HashMailConfig{},
Prometheus: &PrometheusConfig{},
IdleTimeout: defaultIdleTimeout,
ReadTimeout: defaultReadTimeout,
WriteTimeout: defaultWriteTimeout,
InvoiceBatchSize: defaultInvoiceBatchSize,
}
}

0 comments on commit 3a25952

Please sign in to comment.