Skip to content

Commit a6321cc

Browse files
committed
htlcswitch: add ctx, cancel to memoryMailbox
And remove one context.TODO
1 parent b411b41 commit a6321cc

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

htlcswitch/link.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,7 @@ func (l *channelLink) handleDownstreamUpdateAdd(ctx context.Context,
16951695
// already sent Stfu, then we can't add new htlcs to the link and we
16961696
// need to bounce it.
16971697
if l.IsFlushing(Outgoing) || !l.quiescer.CanSendUpdates() {
1698-
l.mailBox.FailAdd(pkt)
1698+
l.mailBox.FailAdd(ctx, pkt)
16991699

17001700
return NewDetailedLinkError(
17011701
&lnwire.FailTemporaryChannelFailure{},
@@ -1718,7 +1718,7 @@ func (l *channelLink) handleDownstreamUpdateAdd(ctx context.Context,
17181718
l.log.Debugf("Unable to handle downstream HTLC - max fee " +
17191719
"exposure exceeded")
17201720

1721-
l.mailBox.FailAdd(pkt)
1721+
l.mailBox.FailAdd(ctx, pkt)
17221722

17231723
return NewDetailedLinkError(
17241724
lnwire.NewTemporaryChannelFailure(nil),
@@ -1752,7 +1752,7 @@ func (l *channelLink) handleDownstreamUpdateAdd(ctx context.Context,
17521752
// the switch, since the circuit was never fully opened,
17531753
// and the forwarding package shows it as
17541754
// unacknowledged.
1755-
l.mailBox.FailAdd(pkt)
1755+
l.mailBox.FailAdd(ctx, pkt)
17561756

17571757
return NewDetailedLinkError(
17581758
lnwire.NewTemporaryChannelFailure(nil),

htlcswitch/mailbox.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/lightningnetwork/lnd/clock"
13+
"github.com/lightningnetwork/lnd/fn/v2"
1314
"github.com/lightningnetwork/lnd/lntypes"
1415
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1516
"github.com/lightningnetwork/lnd/lnwire"
@@ -53,7 +54,7 @@ type MailBox interface {
5354
// packet from being delivered after the link restarts if the switch has
5455
// remained online. The generated LinkError will show an
5556
// OutgoingFailureDownstreamHtlcAdd FailureDetail.
56-
FailAdd(pkt *htlcPacket)
57+
FailAdd(ctx context.Context, pkt *htlcPacket)
5758

5859
// MessageOutBox returns a channel that any new messages ready for
5960
// delivery will be sent on.
@@ -149,6 +150,7 @@ type memoryMailBox struct {
149150
wireShutdown chan struct{}
150151
pktShutdown chan struct{}
151152
quit chan struct{}
153+
cancel fn.Option[context.CancelFunc]
152154

153155
// feeRate is set when the link receives or sends out fee updates. It
154156
// is refreshed when AttachMailBox is called in case a fee update did
@@ -207,8 +209,11 @@ const (
207209
// NOTE: This method is part of the MailBox interface.
208210
func (m *memoryMailBox) Start() {
209211
m.started.Do(func() {
212+
ctx, cancel := context.WithCancel(context.Background())
213+
m.cancel = fn.Some(cancel)
214+
210215
go m.wireMailCourier()
211-
go m.pktMailCourier()
216+
go m.pktMailCourier(ctx)
212217
})
213218
}
214219

@@ -324,6 +329,7 @@ func (m *memoryMailBox) HasPacket(inKey CircuitKey) bool {
324329
// NOTE: This method is part of the MailBox interface.
325330
func (m *memoryMailBox) Stop() {
326331
m.stopped.Do(func() {
332+
m.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
327333
close(m.quit)
328334

329335
m.signalUntilShutdown(wireCourier)
@@ -424,7 +430,7 @@ func (m *memoryMailBox) wireMailCourier() {
424430

425431
// pktMailCourier is a dedicated goroutine whose job is to reliably deliver
426432
// packet messages.
427-
func (m *memoryMailBox) pktMailCourier() {
433+
func (m *memoryMailBox) pktMailCourier(ctx context.Context) {
428434
defer close(m.pktShutdown)
429435

430436
for {
@@ -541,7 +547,7 @@ func (m *memoryMailBox) pktMailCourier() {
541547
case <-deadline:
542548
log.Debugf("Expiring add htlc with "+
543549
"keystone=%v", add.keystone())
544-
m.FailAdd(add)
550+
m.FailAdd(ctx, add)
545551

546552
case pktDone := <-m.pktReset:
547553
m.pktCond.L.Lock()
@@ -688,9 +694,7 @@ func (m *memoryMailBox) DustPackets() (lnwire.MilliSatoshi,
688694
// delivered after the link restarts if the switch has remained online. The
689695
// generated LinkError will show an OutgoingFailureDownstreamHtlcAdd
690696
// FailureDetail.
691-
func (m *memoryMailBox) FailAdd(pkt *htlcPacket) {
692-
ctx := context.TODO()
693-
697+
func (m *memoryMailBox) FailAdd(ctx context.Context, pkt *htlcPacket) {
694698
// First, remove the packet from mailbox. If we didn't find the packet
695699
// because it has already been acked, we'll exit early to avoid sending
696700
// a duplicate fail message through the switch.

htlcswitch/mailbox_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ func (c *mailboxContext) checkFails(adds []*htlcPacket) {
334334
// TestMailBoxFailAdd asserts that FailAdd returns a response to the switch
335335
// under various interleavings with other operations on the mailbox.
336336
func TestMailBoxFailAdd(t *testing.T) {
337+
t.Parallel()
338+
337339
var (
338340
batchDelay = time.Second
339341
expiry = time.Minute
@@ -346,7 +348,7 @@ func TestMailBoxFailAdd(t *testing.T) {
346348

347349
failAdds := func(adds []*htlcPacket) {
348350
for _, add := range adds {
349-
ctx.mailbox.FailAdd(add)
351+
ctx.mailbox.FailAdd(context.Background(), add)
350352
}
351353
}
352354

0 commit comments

Comments
 (0)