Use context during invoice polling

This commit is contained in:
ekzyis 2023-11-27 22:09:51 +01:00
parent ecda577e3e
commit 3445693ef4
2 changed files with 22 additions and 11 deletions

View File

@ -98,8 +98,8 @@ func (db *DB) FetchUserInvoices(pubkey string, invoices *[]Invoice) error {
return nil return nil
} }
func (db *DB) ConfirmInvoice(hash string, confirmedAt time.Time, msatsReceived int) error { func (db *DB) ConfirmInvoice(tx *sql.Tx, c context.Context, hash string, confirmedAt time.Time, msatsReceived int) error {
if _, err := db.Exec("UPDATE invoices SET confirmed_at = $2, msats_received = $3 WHERE hash = $1", hash, confirmedAt, msatsReceived); err != nil { if _, err := tx.ExecContext(c, "UPDATE invoices SET confirmed_at = $2, msats_received = $3 WHERE hash = $1", hash, confirmedAt, msatsReceived); err != nil {
return err return err
} }
return nil return nil

View File

@ -67,24 +67,34 @@ func (lnd *LNDClient) CheckInvoice(d *db.DB, hash lntypes.Hash) {
return return
} }
handleLoopError := func(err error) {
log.Println(err)
time.Sleep(pollInterval)
}
for { for {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
var tx *sql.Tx
if tx, err = d.BeginTx(ctx, nil); err != nil {
cancel()
continue
}
handleLoopError := func(err error) {
log.Println(err)
tx.Rollback()
cancel()
time.Sleep(pollInterval)
}
log.Printf("lookup invoice: hash=%s", hash) log.Printf("lookup invoice: hash=%s", hash)
if lnInvoice, err = lnd.Client.LookupInvoice(context.TODO(), hash); err != nil { if lnInvoice, err = lnd.Client.LookupInvoice(ctx, hash); err != nil {
handleLoopError(err) handleLoopError(err)
continue continue
} }
if time.Now().After(invoice.ExpiresAt) { if time.Now().After(invoice.ExpiresAt) {
// cancel invoices after expiration if no matching order found yet // cancel invoices after expiration if no matching order found yet
if err = lnd.Invoices.CancelInvoice(context.TODO(), hash); err != nil { if err = lnd.Invoices.CancelInvoice(ctx, hash); err != nil {
handleLoopError(err) handleLoopError(err)
continue continue
} }
log.Printf("invoice expired: hash=%s", hash) log.Printf("invoice expired: hash=%s", hash)
tx.Commit()
break break
} }
if lnInvoice.AmountPaid == lnInvoice.Amount { if lnInvoice.AmountPaid == lnInvoice.Amount {
@ -93,15 +103,16 @@ func (lnd *LNDClient) CheckInvoice(d *db.DB, hash lntypes.Hash) {
continue continue
} }
// TODO settle invoice after matching order was found // TODO settle invoice after matching order was found
if err = lnd.Invoices.SettleInvoice(context.TODO(), preimage); err != nil { if err = lnd.Invoices.SettleInvoice(ctx, preimage); err != nil {
handleLoopError(err) handleLoopError(err)
continue continue
} }
if err = d.ConfirmInvoice(hash.String(), time.Now(), int(lnInvoice.AmountPaid)); err != nil { if err = d.ConfirmInvoice(tx, ctx, hash.String(), time.Now(), int(lnInvoice.AmountPaid)); err != nil {
handleLoopError(err) handleLoopError(err)
continue continue
} }
log.Printf("invoice confirmed: hash=%s", hash) log.Printf("invoice confirmed: hash=%s", hash)
tx.Commit()
break break
} }
time.Sleep(pollInterval) time.Sleep(pollInterval)