Skip to content

Commit

Permalink
. update fn for context guard
Browse files Browse the repository at this point in the history
  • Loading branch information
starius committed Jan 2, 2025
1 parent 625808f commit dd897e2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ replace github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
// allows us to specify that as an option.
replace google.golang.org/protobuf => github.com/lightninglabs/protobuf-go-hex-display v1.30.0-hex-display

replace github.com/lightningnetwork/lnd/fn/v2 => github.com/starius/lnd/fn/v2 v2.0.7-optcg

// If you change this please also update docs/INSTALL.md and GO_VERSION in
// Makefile (then run `make lint` to see where else it needs to be updated as
// well).
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ github.com/lightningnetwork/lnd/cert v1.2.2 h1:71YK6hogeJtxSxw2teq3eGeuy4rHGKcFf
github.com/lightningnetwork/lnd/cert v1.2.2/go.mod h1:jQmFn/Ez4zhDgq2hnYSw8r35bqGVxViXhX6Cd7HXM6U=
github.com/lightningnetwork/lnd/clock v1.1.1 h1:OfR3/zcJd2RhH0RU+zX/77c0ZiOnIMsDIBjgjWdZgA0=
github.com/lightningnetwork/lnd/clock v1.1.1/go.mod h1:mGnAhPyjYZQJmebS7aevElXKTFDuO+uNFFfMXK1W8xQ=
github.com/lightningnetwork/lnd/fn/v2 v2.0.4 h1:DiC/AEa7DhnY4qOEQBISu1cp+1+51LjbVDzNLVBwNjI=
github.com/lightningnetwork/lnd/fn/v2 v2.0.4/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
github.com/lightningnetwork/lnd/healthcheck v1.2.6 h1:1sWhqr93GdkWy4+6U7JxBfcyZIE78MhIHTJZfPx7qqI=
github.com/lightningnetwork/lnd/healthcheck v1.2.6/go.mod h1:Mu02um4CWY/zdTOvFje7WJgJcHyX2zq/FG3MhOAiGaQ=
github.com/lightningnetwork/lnd/kvdb v1.4.12 h1:Y0WY5Tbjyjn6eCYh068qkWur5oFtioJlfxc8w5SlJeQ=
Expand Down Expand Up @@ -588,6 +586,8 @@ github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/starius/lnd/fn/v2 v2.0.7-optcg h1:20scsrug8GKihMQ0UbCsTlRuo2rbKjXShOqFDb9z6p8=
github.com/starius/lnd/fn/v2 v2.0.7-optcg/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
Expand Down
41 changes: 21 additions & 20 deletions htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package htlcswitch

import (
"bytes"
"context"
crand "crypto/rand"
"crypto/sha256"
"errors"
Expand Down Expand Up @@ -596,7 +597,7 @@ func (l *channelLink) Start() error {

l.updateFeeTimer = time.NewTimer(l.randomFeeUpdateTimeout())

l.Wg.Add(1)
l.WgAdd(1)
go l.htlcManager()

return nil
Expand Down Expand Up @@ -636,8 +637,8 @@ func (l *channelLink) Stop() {
l.hodlQueue.Stop()
}

close(l.Quit)
l.Wg.Wait()
l.Quit()
l.WgWait()

// Now that the htlcManager has completely exited, reset the packet
// courier. This allows the mailbox to revaluate any lingering Adds that
Expand All @@ -662,7 +663,7 @@ func (l *channelLink) Stop() {
// WaitForShutdown blocks until the link finishes shutting down, which includes
// termination of all dependent goroutines.
func (l *channelLink) WaitForShutdown() {
l.Wg.Wait()
l.WgWait()
}

// EligibleToForward returns a bool indicating if the channel is able to
Expand Down Expand Up @@ -740,7 +741,7 @@ func (l *channelLink) IsFlushing(linkDirection LinkDirection) bool {
func (l *channelLink) OnFlushedOnce(hook func()) {
select {
case l.flushHooks.newTransients <- hook:
case <-l.Quit:
case <-l.Done():
}
}

Expand All @@ -759,7 +760,7 @@ func (l *channelLink) OnCommitOnce(direction LinkDirection, hook func()) {

select {
case queue <- hook:
case <-l.Quit:
case <-l.Done():
}
}

Expand All @@ -777,7 +778,7 @@ func (l *channelLink) InitStfu() <-chan fn.Result[lntypes.ChannelParty] {

select {
case l.quiescenceReqs <- req:
case <-l.Quit:
case <-l.Done():
req.Resolve(fn.Err[lntypes.ChannelParty](ErrLinkShuttingDown))
}

Expand Down Expand Up @@ -989,7 +990,7 @@ func (l *channelLink) syncChanStates() error {
// We've just received a ChanSync message from the remote
// party, so we'll process the message in order to determine
// if we need to re-transmit any messages to the remote party.
ctx, cancel := l.WithCtxQuitNoTimeout()
ctx, cancel := l.Create(context.Background())
defer cancel()
msgsToReSend, openedCircuits, closedCircuits, err =
l.channel.ProcessChanSyncMsg(ctx, remoteChanSyncMsg)
Expand Down Expand Up @@ -1021,7 +1022,7 @@ func (l *channelLink) syncChanStates() error {
l.cfg.Peer.SendMessage(false, msg)
}

case <-l.Quit:
case <-l.Done():
return ErrLinkShuttingDown
}

Expand Down Expand Up @@ -1111,7 +1112,7 @@ func (l *channelLink) resolveFwdPkg(fwdPkg *channeldb.FwdPkg) error {
//
// NOTE: This MUST be run as a goroutine.
func (l *channelLink) fwdPkgGarbager() {
defer l.Wg.Done()
defer l.WgDone()

l.cfg.FwdPkgGCTicker.Resume()
defer l.cfg.FwdPkgGCTicker.Stop()
Expand All @@ -1128,7 +1129,7 @@ func (l *channelLink) fwdPkgGarbager() {
err)
continue
}
case <-l.Quit:
case <-l.Done():
return
}
}
Expand Down Expand Up @@ -1251,7 +1252,7 @@ func (l *channelLink) handleChanSyncErr(err error) {
func (l *channelLink) htlcManager() {
defer func() {
l.cfg.BatchTicker.Stop()
l.Wg.Done()
l.WgDone()
l.log.Infof("exited")
}()

Expand Down Expand Up @@ -1345,7 +1346,7 @@ func (l *channelLink) htlcManager() {
// With our link's in-memory state fully reconstructed, spawn a
// goroutine to manage the reclamation of disk space occupied by
// completed forwarding packages.
l.Wg.Add(1)
l.WgAdd(1)
go l.fwdPkgGarbager()
}

Expand Down Expand Up @@ -1543,7 +1544,7 @@ func (l *channelLink) htlcManager() {
}
}

case <-l.Quit:
case <-l.Done():
return
}
}
Expand Down Expand Up @@ -2418,7 +2419,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
}

select {
case <-l.Quit:
case <-l.Done():
return
default:
}
Expand Down Expand Up @@ -2488,7 +2489,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
}

select {
case <-l.Quit:
case <-l.Done():
return
default:
}
Expand Down Expand Up @@ -2782,7 +2783,7 @@ func (l *channelLink) updateCommitTx() error {
return nil
}

ctx, done := l.WithCtxQuitNoTimeout()
ctx, done := l.Create(context.Background())
defer done()

newCommit, err := l.channel.SignNextCommitment(ctx)
Expand Down Expand Up @@ -2822,7 +2823,7 @@ func (l *channelLink) updateCommitTx() error {
}

select {
case <-l.Quit:
case <-l.Done():
return ErrLinkShuttingDown
default:
}
Expand Down Expand Up @@ -3529,7 +3530,7 @@ func (l *channelLink) handleSwitchPacket(pkt *htlcPacket) error {
// NOTE: Part of the ChannelLink interface.
func (l *channelLink) HandleChannelUpdate(message lnwire.Message) {
select {
case <-l.Quit:
case <-l.Done():
// Return early if the link is already in the process of
// quitting. It doesn't make sense to hand the message to the
// mailbox here.
Expand Down Expand Up @@ -4290,7 +4291,7 @@ func (l *channelLink) forwardBatch(replay bool, packets ...*htlcPacket) {
filteredPkts = append(filteredPkts, pkt)
}

err := l.cfg.ForwardPackets(l.Quit, replay, filteredPkts...)
err := l.cfg.ForwardPackets(l.Done(), replay, filteredPkts...)
if err != nil {
log.Errorf("Unhandled error while reforwarding htlc "+
"settle/fail over htlcswitch: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion htlcswitch/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ func (h *hopNetwork) createChannelLink(server, peer *mockServer,
for {
select {
case <-notifyUpdateChan:
case <-chanLink.Quit:
case <-chanLink.Done():
close(doneChan)
return
}
Expand Down

0 comments on commit dd897e2

Please sign in to comment.