Skip to content

Commit b58cab9

Browse files
committed
multi: thread context through DeleteFailedAttempts
1 parent c67f83c commit b58cab9

File tree

7 files changed

+45
-18
lines changed

7 files changed

+45
-18
lines changed

payments/db/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type PaymentControl interface {
104104
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
105105
// be called for a given payment whenever all inflight htlcs are
106106
// completed, and the payment has reached a final terminal state.
107-
DeleteFailedAttempts(lntypes.Hash) error
107+
DeleteFailedAttempts(context.Context, lntypes.Hash) error
108108
}
109109

110110
// DBMPPayment is an interface that represents the payment state during a

payments/db/kv_store.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,14 @@ func (p *KVStore) InitPayment(_ context.Context, paymentHash lntypes.Hash,
290290

291291
// DeleteFailedAttempts deletes all failed htlcs for a payment if configured
292292
// by the KVStore db.
293-
func (p *KVStore) DeleteFailedAttempts(hash lntypes.Hash) error {
293+
func (p *KVStore) DeleteFailedAttempts(ctx context.Context,
294+
hash lntypes.Hash) error {
295+
294296
// TODO(ziggie): Refactor to not mix application logic with database
295297
// logic. This decision should be made in the application layer.
296298
if !p.keepFailedPaymentAttempts {
297299
const failedHtlcsOnly = true
298-
err := p.DeletePayment(context.TODO(), hash, failedHtlcsOnly)
300+
err := p.DeletePayment(ctx, hash, failedHtlcsOnly)
299301
if err != nil {
300302
return err
301303
}

payments/db/payment_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,9 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
503503

504504
// Calling DeleteFailedAttempts on a failed payment should delete all
505505
// HTLCs.
506-
require.NoError(t, paymentDB.DeleteFailedAttempts(payments[0].id))
506+
require.NoError(t, paymentDB.DeleteFailedAttempts(
507+
t.Context(), payments[0].id,
508+
))
507509

508510
// Expect all HTLCs to be deleted if the config is set to delete them.
509511
if !keepFailedPaymentAttempts {
@@ -518,19 +520,25 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
518520
// operation are performed in general therefore we do NOT expect an
519521
// error in this case.
520522
if keepFailedPaymentAttempts {
521-
require.NoError(t, paymentDB.DeleteFailedAttempts(
522-
payments[1].id),
523+
err := paymentDB.DeleteFailedAttempts(
524+
t.Context(), payments[1].id,
523525
)
526+
require.NoError(t, err)
524527
} else {
525-
require.Error(t, paymentDB.DeleteFailedAttempts(payments[1].id))
528+
err := paymentDB.DeleteFailedAttempts(
529+
t.Context(), payments[1].id,
530+
)
531+
require.Error(t, err)
526532
}
527533

528534
// Since DeleteFailedAttempts returned an error, we should expect the
529535
// payment to be unchanged.
530536
assertDBPayments(t, paymentDB, payments)
531537

532538
// Cleaning up a successful payment should remove failed htlcs.
533-
require.NoError(t, paymentDB.DeleteFailedAttempts(payments[2].id))
539+
require.NoError(t, paymentDB.DeleteFailedAttempts(
540+
t.Context(), payments[2].id,
541+
))
534542

535543
// Expect all HTLCs except for the settled one to be deleted if the
536544
// config is set to delete them.
@@ -547,13 +555,17 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
547555
// payments, if the control tower is configured to keep failed
548556
// HTLCs.
549557
require.NoError(
550-
t, paymentDB.DeleteFailedAttempts(lntypes.ZeroHash),
558+
t, paymentDB.DeleteFailedAttempts(
559+
t.Context(), lntypes.ZeroHash,
560+
),
551561
)
552562
} else {
553563
// Attempting to cleanup a non-existent payment returns an
554564
// error.
555565
require.Error(
556-
t, paymentDB.DeleteFailedAttempts(lntypes.ZeroHash),
566+
t, paymentDB.DeleteFailedAttempts(
567+
t.Context(), lntypes.ZeroHash,
568+
),
557569
)
558570
}
559571
}

payments/db/sql_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,9 @@ func (s *SQLStore) FetchInFlightPayments(ctx context.Context) ([]*MPPayment,
10121012
// the final step (step 5) in the payment lifecycle control flow and should be
10131013
// called after a payment reaches a terminal state (succeeded or permanently
10141014
// failed) to clean up historical failed attempts.
1015-
func (s *SQLStore) DeleteFailedAttempts(paymentHash lntypes.Hash) error {
1015+
func (s *SQLStore) DeleteFailedAttempts(ctx context.Context,
1016+
paymentHash lntypes.Hash) error {
1017+
10161018
// In case we are configured to keep failed payment attempts, we exit
10171019
// early.
10181020
//
@@ -1021,7 +1023,6 @@ func (s *SQLStore) DeleteFailedAttempts(paymentHash lntypes.Hash) error {
10211023
if s.keepFailedPaymentAttempts {
10221024
return nil
10231025
}
1024-
ctx := context.TODO()
10251026

10261027
err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
10271028
dbPayment, err := fetchPaymentByHash(ctx, db, paymentHash)

routing/control_tower.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ControlTower interface {
2626
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
2727
// be called for a given payment whenever all inflight htlcs are
2828
// completed, and the payment has reached a final settled state.
29-
DeleteFailedAttempts(lntypes.Hash) error
29+
DeleteFailedAttempts(context.Context, lntypes.Hash) error
3030

3131
// RegisterAttempt atomically records the provided HTLCAttemptInfo.
3232
//
@@ -192,8 +192,10 @@ func (p *controlTower) InitPayment(ctx context.Context,
192192

193193
// DeleteFailedAttempts deletes all failed htlcs if the payment was
194194
// successfully settled.
195-
func (p *controlTower) DeleteFailedAttempts(paymentHash lntypes.Hash) error {
196-
return p.db.DeleteFailedAttempts(paymentHash)
195+
func (p *controlTower) DeleteFailedAttempts(ctx context.Context,
196+
paymentHash lntypes.Hash) error {
197+
198+
return p.db.DeleteFailedAttempts(ctx, paymentHash)
197199
}
198200

199201
// RegisterAttempt atomically records the provided HTLCAttemptInfo to the

routing/mock_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ func (m *mockControlTowerOld) InitPayment(_ context.Context,
328328
return nil
329329
}
330330

331-
func (m *mockControlTowerOld) DeleteFailedAttempts(phash lntypes.Hash) error {
331+
func (m *mockControlTowerOld) DeleteFailedAttempts(_ context.Context,
332+
phash lntypes.Hash) error {
333+
332334
p, ok := m.payments[phash]
333335
if !ok {
334336
return paymentsdb.ErrPaymentNotInitiated
@@ -742,7 +744,9 @@ func (m *mockControlTower) InitPayment(_ context.Context, phash lntypes.Hash,
742744
return args.Error(0)
743745
}
744746

745-
func (m *mockControlTower) DeleteFailedAttempts(phash lntypes.Hash) error {
747+
func (m *mockControlTower) DeleteFailedAttempts(_ context.Context,
748+
phash lntypes.Hash) error {
749+
746750
args := m.Called(phash)
747751
return args.Error(0)
748752
}

routing/payment_lifecycle.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ func (p *paymentLifecycle) decideNextStep(
190190
func (p *paymentLifecycle) resumePayment(ctx context.Context) ([32]byte,
191191
*route.Route, error) {
192192

193+
// We need to make sure we can still do db operations after the context
194+
// is cancelled.
195+
cleanupCtx := context.WithoutCancel(ctx)
196+
193197
// When the payment lifecycle loop exits, we make sure to signal any
194198
// sub goroutine of the HTLC attempt to exit, then wait for them to
195199
// return.
@@ -328,7 +332,9 @@ lifecycle:
328332
// Optionally delete the failed attempts from the database. Depends on
329333
// the database options deleting attempts is not allowed so this will
330334
// just be a no-op.
331-
err = p.router.cfg.Control.DeleteFailedAttempts(p.identifier)
335+
err = p.router.cfg.Control.DeleteFailedAttempts(
336+
cleanupCtx, p.identifier,
337+
)
332338
if err != nil {
333339
log.Errorf("Error deleting failed htlc attempts for payment "+
334340
"%v: %v", p.identifier, err)

0 commit comments

Comments
 (0)