-
Notifications
You must be signed in to change notification settings - Fork 2.2k
peer+lnd: add new CLI option to control if we D/C on slow pongs #9801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"sync/atomic" | ||
"time" | ||
|
||
"github.com/lightningnetwork/lnd/fn/v2" | ||
"github.com/lightningnetwork/lnd/lnwire" | ||
) | ||
|
||
|
@@ -36,7 +37,8 @@ type PingManagerConfig struct { | |
// OnPongFailure is a closure that is responsible for executing the | ||
// logic when a Pong message is either late or does not match our | ||
// expectations for that Pong | ||
OnPongFailure func(error) | ||
OnPongFailure func(failureReason error, timeWaitedForPong time.Duration, | ||
lastKnownRTT time.Duration) | ||
} | ||
|
||
// PingManager is a structure that is designed to manage the internal state | ||
|
@@ -108,6 +110,26 @@ func (m *PingManager) Start() error { | |
return err | ||
} | ||
|
||
// getLastRTT safely retrieves the last known RTT, returning 0 if none exists. | ||
func (m *PingManager) getLastRTT() time.Duration { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Use fn.Option here as well similar as below |
||
rttPtr := m.pingTime.Load() | ||
if rttPtr == nil { | ||
return 0 | ||
} | ||
|
||
return *rttPtr | ||
} | ||
|
||
// pendingPingWait calculates the time waited since the last ping was sent. If | ||
// no ping time is reported, None is returned. defaultDuration. | ||
func (m *PingManager) pendingPingWait() fn.Option[time.Duration] { | ||
if m.pingLastSend != nil { | ||
return fn.Some(time.Since(*m.pingLastSend)) | ||
} | ||
|
||
return fn.None[time.Duration]() | ||
} | ||
|
||
// pingHandler is the main goroutine responsible for enforcing the ping/pong | ||
// protocol. | ||
func (m *PingManager) pingHandler() { | ||
|
@@ -119,6 +141,10 @@ func (m *PingManager) pingHandler() { | |
<-m.pingTimeout.C | ||
} | ||
|
||
// Because we don't know if the OnPingFailure callback actually | ||
// disconnects a peer (dependent on user config), we should never return | ||
// from this loop unless the ping manager is stopped explicitly (which | ||
// happens on disconnect). | ||
for { | ||
select { | ||
case <-m.pingTicker.C: | ||
|
@@ -127,12 +153,20 @@ func (m *PingManager) pingHandler() { | |
// awaiting a pong response. This should never occur, | ||
// but if it does, it implies a timeout. | ||
if m.outstandingPongSize >= 0 { | ||
e := errors.New("impossible: new ping" + | ||
"in unclean state", | ||
// Ping was outstanding, meaning it timed out by | ||
// the arrival of the next ping interval. | ||
timeWaited := m.pendingPingWait().UnwrapOr( | ||
m.cfg.IntervalDuration, | ||
) | ||
lastRTT := m.getLastRTT() | ||
|
||
m.cfg.OnPongFailure( | ||
errors.New("ping timed "+ | ||
"out by next interval"), | ||
timeWaited, lastRTT, | ||
) | ||
m.cfg.OnPongFailure(e) | ||
|
||
return | ||
m.resetPingState() | ||
} | ||
|
||
pongSize := m.cfg.NewPongSize() | ||
|
@@ -143,53 +177,67 @@ func (m *PingManager) pingHandler() { | |
|
||
// Set up our bookkeeping for the new Ping. | ||
if err := m.setPingState(pongSize); err != nil { | ||
m.cfg.OnPongFailure(err) | ||
// This is an internal error related to timer | ||
// reset. Pass it to OnPongFailure as it's | ||
// critical. Current and last RTT are not | ||
// directly applicable here. | ||
m.cfg.OnPongFailure(err, 0, 0) | ||
|
||
m.resetPingState() | ||
|
||
return | ||
continue | ||
Roasbeef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
m.cfg.SendPing(ping) | ||
|
||
case <-m.pingTimeout.C: | ||
m.resetPingState() | ||
|
||
e := errors.New("timeout while waiting for " + | ||
"pong response", | ||
timeWaited := m.pendingPingWait().UnwrapOr( | ||
m.cfg.TimeoutDuration, | ||
) | ||
lastRTT := m.getLastRTT() | ||
|
||
m.cfg.OnPongFailure(e) | ||
m.cfg.OnPongFailure( | ||
errors.New("timeout while waiting for "+ | ||
"pong response"), | ||
timeWaited, lastRTT, | ||
) | ||
|
||
return | ||
m.resetPingState() | ||
Roasbeef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
case pong := <-m.pongChan: | ||
pongSize := int32(len(pong.PongBytes)) | ||
|
||
// Save off values we are about to override when we | ||
// call resetPingState. | ||
// Save off values we are about to override when we call | ||
// resetPingState. | ||
expected := m.outstandingPongSize | ||
lastPing := m.pingLastSend | ||
lastPingTime := m.pingLastSend | ||
|
||
m.resetPingState() | ||
// This is an unexpected pong, we'll continue. | ||
if lastPingTime == nil { | ||
continue | ||
} | ||
|
||
// If the pong we receive doesn't match the ping we | ||
// sent out, then we fail out. | ||
actualRTT := time.Since(*lastPingTime) | ||
|
||
// If the pong we receive doesn't match the ping we sent | ||
// out, then we fail out. | ||
if pongSize != expected { | ||
e := errors.New("pong response does " + | ||
"not match expected size", | ||
) | ||
e := fmt.Errorf("pong response does not match "+ | ||
"expected size. Expected: %d, Got: %d", | ||
expected, pongSize) | ||
|
||
m.cfg.OnPongFailure(e) | ||
lastRTT := m.getLastRTT() | ||
m.cfg.OnPongFailure(e, actualRTT, lastRTT) | ||
Roasbeef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return | ||
} | ||
m.resetPingState() | ||
|
||
// Compute RTT of ping and save that for future | ||
// querying. | ||
if lastPing != nil { | ||
rtt := time.Since(*lastPing) | ||
m.pingTime.Store(&rtt) | ||
continue | ||
} | ||
|
||
// Pong is good, update RTT and reset state. | ||
m.pingTime.Store(&actualRTT) | ||
m.resetPingState() | ||
|
||
case <-m.quit: | ||
return | ||
} | ||
|
@@ -231,6 +279,7 @@ func (m *PingManager) setPingState(pongSize uint16) error { | |
func (m *PingManager) resetPingState() { | ||
m.pingLastSend = nil | ||
m.outstandingPongSize = -1 | ||
|
||
if !m.pingTimeout.Stop() { | ||
select { | ||
case <-m.pingTimeout.C: | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Comment can be removed, describes the code.