avoid goroutine bug

This commit is contained in:
kiwiidb
2022-12-02 12:10:52 +01:00
parent 3a5261388c
commit dbb8eb4709

View File

@@ -22,13 +22,16 @@ func (svc *LndhubService) CheckAllPendingOutgoingPayments(ctx context.Context) (
//call trackoutgoingpaymentstatus for each one
for _, inv := range pendingPayments {
//spawn goroutines
go svc.TrackOutgoingPaymentstatus(ctx, inv)
//https://go.dev/doc/faq#closures_and_goroutines
inv := inv
go svc.TrackOutgoingPaymentstatus(ctx, &inv)
}
return nil
}
// Should be called in a goroutine as the tracking can potentially take a long time
func (svc *LndhubService) TrackOutgoingPaymentstatus(ctx context.Context, invoice models.Invoice) {
func (svc *LndhubService) TrackOutgoingPaymentstatus(ctx context.Context, invoice *models.Invoice) {
fmt.Println(invoice.RHash)
//ask lnd using TrackPaymentV2 by hash of payment
rawHash, err := hex.DecodeString(invoice.RHash)
if err != nil {
@@ -65,7 +68,7 @@ func (svc *LndhubService) TrackOutgoingPaymentstatus(ctx context.Context, invoic
if payment.Status == lnrpc.Payment_FAILED {
svc.Logger.Infof("Failed payment detected: hash %s, reason %s", payment.PaymentHash, payment.FailureReason)
//todo handle failed payment
err = svc.HandleFailedPayment(ctx, &invoice, entry, fmt.Errorf(payment.FailureReason.String()))
err = svc.HandleFailedPayment(ctx, invoice, entry, fmt.Errorf(payment.FailureReason.String()))
if err != nil {
svc.Logger.Errorf("Error handling failed payment %v: %s", invoice, err.Error())
return
@@ -77,7 +80,7 @@ func (svc *LndhubService) TrackOutgoingPaymentstatus(ctx context.Context, invoic
invoice.Fee = payment.FeeSat
invoice.Preimage = payment.PaymentPreimage
svc.Logger.Infof("Completed payment detected: hash %s", payment.PaymentHash)
err = svc.HandleSuccessfulPayment(ctx, &invoice, entry)
err = svc.HandleSuccessfulPayment(ctx, invoice, entry)
if err != nil {
svc.Logger.Errorf("Error handling successful payment %v: %s", invoice, err.Error())
return