1
1
package peer
2
2
3
3
import (
4
+ "context"
4
5
"errors"
5
6
"fmt"
6
7
"sync"
@@ -39,6 +40,11 @@ type PingManagerConfig struct {
39
40
// expectations for that Pong
40
41
OnPongFailure func (failureReason error , timeWaitedForPong time.Duration ,
41
42
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
42
48
}
43
49
44
50
// PingManager is a structure that is designed to manage the internal state
@@ -201,12 +207,12 @@ func (m *PingManager) pingHandler() {
201
207
pongSize := int32 (len (pong .PongBytes ))
202
208
203
209
// Save off values we are about to override when we call
204
- // resetPingState.
205
210
expected := m .outstandingPongSize
211
+ // resetPingState.
206
212
lastPingTime := m .pingLastSend
207
213
214
+ // This is an unexpected pong, we'll continue.
208
215
if lastPingTime == nil {
209
- // This is an unexpected pong, we'll continue.
210
216
continue
211
217
}
212
218
@@ -231,6 +237,9 @@ func (m *PingManager) pingHandler() {
231
237
m .pingTime .Store (& actualRTT )
232
238
m .resetPingState ()
233
239
240
+ case <- m .cfg .PeerCtx .Done ():
241
+ return
242
+
234
243
case <- m .quit :
235
244
return
236
245
}
@@ -299,5 +308,6 @@ func (m *PingManager) ReceivedPong(msg *lnwire.Pong) {
299
308
select {
300
309
case m .pongChan <- msg :
301
310
case <- m .quit :
311
+ case <- m .cfg .PeerCtx .Done ():
302
312
}
303
313
}
0 commit comments