Skip to content

Commit 5a2fbed

Browse files
committed
peer: thread through peer parent context to ping manager
This ensures that when the peer shutsdown, then the ping manager does as well.
1 parent ed744ee commit 5a2fbed

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

peer/brontide.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@ func NewBrontide(cfg Config) *Brontide {
731731
)
732732
}
733733

734+
peerCtx, _ := p.cg.Create(context.Background())
735+
734736
p.pingManager = NewPingManager(&PingManagerConfig{
735737
NewPingPayload: newPingPayload,
736738
NewPongSize: randPongSize,
@@ -759,6 +761,7 @@ func NewBrontide(cfg Config) *Brontide {
759761
"due to config", logMsg)
760762
}
761763
},
764+
PeerCtx: peerCtx,
762765
})
763766

764767
return p

peer/ping_manager.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package peer
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"sync"
@@ -39,6 +40,11 @@ type PingManagerConfig struct {
3940
// expectations for that Pong
4041
OnPongFailure func(failureReason error, timeWaitedForPong time.Duration,
4142
lastKnownRTT time.Duration)
43+
44+
// PeerCtx is the context of the peer that owns this ping manager. We'll
45+
// use this to ensure that if the peer is shutting down, then we will as
46+
// well.
47+
PeerCtx context.Context
4248
}
4349

4450
// PingManager is a structure that is designed to manage the internal state
@@ -201,12 +207,12 @@ func (m *PingManager) pingHandler() {
201207
pongSize := int32(len(pong.PongBytes))
202208

203209
// Save off values we are about to override when we call
204-
// resetPingState.
205210
expected := m.outstandingPongSize
211+
// resetPingState.
206212
lastPingTime := m.pingLastSend
207213

214+
// This is an unexpected pong, we'll continue.
208215
if lastPingTime == nil {
209-
// This is an unexpected pong, we'll continue.
210216
continue
211217
}
212218

@@ -231,6 +237,9 @@ func (m *PingManager) pingHandler() {
231237
m.pingTime.Store(&actualRTT)
232238
m.resetPingState()
233239

240+
case <-m.cfg.PeerCtx.Done():
241+
return
242+
234243
case <-m.quit:
235244
return
236245
}
@@ -299,5 +308,6 @@ func (m *PingManager) ReceivedPong(msg *lnwire.Pong) {
299308
select {
300309
case m.pongChan <- msg:
301310
case <-m.quit:
311+
case <-m.cfg.PeerCtx.Done():
302312
}
303313
}

0 commit comments

Comments
 (0)