Skip to content

multi: make invoice loading async, only block callers when they wish to read the invoice state #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Roasbeef
Copy link
Member

@Roasbeef Roasbeef commented Apr 2, 2025

Fixes #167

Roasbeef added 2 commits April 1, 2025 19:15
This will be used to simplify some of the logic the upcoming commit. We
retain the existing condition variable usage, while also adding some
methods that will be of use for the upcoming async background load.
This commit refactors the LND challenger's invoice handling mechanism
to improve performance, reliability, and resource usage, especially
for nodes with a large number of historical invoices.

Previously, the challenger attempted to load all historical invoices
in a single `ListInvoices` call during startup. This could lead to
long startup times, high memory consumption, and potential timeouts
or failures for nodes with extensive invoice history. Additionally,
the state management relied on a mutex and condition variable, which
could be complex to manage correctly.

The key changes include:

- **Concurrent Loading and Subscription:** The challenger now starts
  two background goroutines concurrently: one to load historical
  invoices and another to subscribe to new invoice updates using the
  latest known indices.

- **Paginated Historical Loading:** Historical invoices are now fetched
  in batches using `ListInvoices` with `IndexOffset`. The batch size
  is configurable via the new `InvoiceBatchSize` config option
  (defaulting to 1000) and passed down from the main Aperture config.

- **InvoiceStateStore:** A new `InvoiceStateStore` type is introduced
  to manage the invoice states map. This store handles thread-safe
  access, tracks the completion of the initial historical load, and
  provides a `WaitForState` method that correctly handles waiting for
  the initial load before checking for a specific invoice state.

- **Improved Shutdown/Cancellation:** Context cancellation and handling
  of the quit signal are improved throughout the loading and
  subscription processes to ensure cleaner shutdowns.

- **Refactored VerifyInvoiceStatus:** This method now delegates waiting
  logic to the `InvoiceStateStore`, simplifying the challenger code
  and ensuring it waits for the initial load if necessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aperture: add support for async invoice loading on start up
1 participant