mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-01 02:02:10 +02:00
invoices: reduce heap usage when starting the expiry watcher
This commit is contained in:
@@ -103,10 +103,11 @@ func (ew *InvoiceExpiryWatcher) Stop() {
|
||||
}
|
||||
}
|
||||
|
||||
// prepareInvoice checks if the passed invoice may be canceled and calculates
|
||||
// the expiry time.
|
||||
func (ew *InvoiceExpiryWatcher) prepareInvoice(
|
||||
paymentHash lntypes.Hash, invoice *channeldb.Invoice) *invoiceExpiry {
|
||||
// makeInvoiceExpiry checks if the passed invoice may be canceled and calculates
|
||||
// the expiry time and creates a slimmer invoiceExpiry object with the hash and
|
||||
// expiry time.
|
||||
func makeInvoiceExpiry(paymentHash lntypes.Hash,
|
||||
invoice *channeldb.Invoice) *invoiceExpiry {
|
||||
|
||||
if invoice.State != channeldb.ContractOpen {
|
||||
log.Debugf("Invoice not added to expiry watcher: %v",
|
||||
@@ -127,45 +128,14 @@ func (ew *InvoiceExpiryWatcher) prepareInvoice(
|
||||
}
|
||||
}
|
||||
|
||||
// AddInvoices adds multiple invoices to the InvoiceExpiryWatcher.
|
||||
func (ew *InvoiceExpiryWatcher) AddInvoices(
|
||||
invoices map[lntypes.Hash]*channeldb.Invoice) {
|
||||
|
||||
invoicesWithExpiry := make([]*invoiceExpiry, 0, len(invoices))
|
||||
for paymentHash, invoice := range invoices {
|
||||
newInvoiceExpiry := ew.prepareInvoice(paymentHash, invoice)
|
||||
if newInvoiceExpiry != nil {
|
||||
invoicesWithExpiry = append(
|
||||
invoicesWithExpiry, newInvoiceExpiry,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if len(invoicesWithExpiry) > 0 {
|
||||
log.Debugf("Added %d invoices to the expiry watcher",
|
||||
len(invoicesWithExpiry))
|
||||
// AddInvoices adds invoices to the InvoiceExpiryWatcher.
|
||||
func (ew *InvoiceExpiryWatcher) AddInvoices(invoices ...*invoiceExpiry) {
|
||||
if len(invoices) > 0 {
|
||||
select {
|
||||
case ew.newInvoices <- invoicesWithExpiry:
|
||||
// Select on quit too so that callers won't get blocked in case
|
||||
// of concurrent shutdown.
|
||||
case <-ew.quit:
|
||||
}
|
||||
}
|
||||
}
|
||||
case ew.newInvoices <- invoices:
|
||||
log.Debugf("Added %d invoices to the expiry watcher",
|
||||
len(invoices))
|
||||
|
||||
// AddInvoice adds a new invoice to the InvoiceExpiryWatcher. This won't check
|
||||
// if the invoice is already added and will only add invoices with ContractOpen
|
||||
// state.
|
||||
func (ew *InvoiceExpiryWatcher) AddInvoice(
|
||||
paymentHash lntypes.Hash, invoice *channeldb.Invoice) {
|
||||
|
||||
newInvoiceExpiry := ew.prepareInvoice(paymentHash, invoice)
|
||||
if newInvoiceExpiry != nil {
|
||||
log.Debugf("Adding invoice '%v' to expiry watcher,"+
|
||||
"expiration: %v", paymentHash, newInvoiceExpiry.Expiry)
|
||||
|
||||
select {
|
||||
case ew.newInvoices <- []*invoiceExpiry{newInvoiceExpiry}:
|
||||
// Select on quit too so that callers won't get blocked in case
|
||||
// of concurrent shutdown.
|
||||
case <-ew.quit:
|
||||
@@ -220,14 +190,21 @@ func (ew *InvoiceExpiryWatcher) mainLoop() {
|
||||
// Cancel any invoices that may have expired.
|
||||
ew.cancelNextExpiredInvoice()
|
||||
|
||||
pushInvoices := func(invoicesWithExpiry []*invoiceExpiry) {
|
||||
for _, invoiceWithExpiry := range invoicesWithExpiry {
|
||||
// Avoid pushing nil object to the heap.
|
||||
if invoiceWithExpiry != nil {
|
||||
ew.expiryQueue.Push(invoiceWithExpiry)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
|
||||
case invoicesWithExpiry := <-ew.newInvoices:
|
||||
// Take newly forwarded invoices with higher priority
|
||||
// in order to not block the newInvoices channel.
|
||||
for _, invoiceWithExpiry := range invoicesWithExpiry {
|
||||
ew.expiryQueue.Push(invoiceWithExpiry)
|
||||
}
|
||||
pushInvoices(invoicesWithExpiry)
|
||||
continue
|
||||
|
||||
default:
|
||||
@@ -238,9 +215,7 @@ func (ew *InvoiceExpiryWatcher) mainLoop() {
|
||||
continue
|
||||
|
||||
case invoicesWithExpiry := <-ew.newInvoices:
|
||||
for _, invoice := range invoicesWithExpiry {
|
||||
ew.expiryQueue.Push(invoice)
|
||||
}
|
||||
pushInvoices(invoicesWithExpiry)
|
||||
|
||||
case <-ew.quit:
|
||||
return
|
||||
|
Reference in New Issue
Block a user