peer: refactor main event loop for ping handler

The error was never used as the init couldn't return an error, so we do
away with that. We also modify the main event loop dispatch to more
closely match other areas of the codebase.
This commit is contained in:
Olaoluwa Osuntokun committed 2024-01-22 18:20:30 -08:00
1 parent b5cbeb4ad7
commit 185119f5c3
1 file changed
+69 -73
+69 -73
View File
@@ -98,16 +98,20 @@ func NewPingManager(cfg *PingManagerConfig) *PingManager {
func (m *PingManager) Start() error { func (m *PingManager) Start() error {
var err error var err error
m.started.Do(func() { m.started.Do(func() {
err = m.start() m.pingTicker = time.NewTicker(m.cfg.IntervalDuration)
m.pingTimeout = time.NewTimer(0)
m.wg.Add(1)
go m.pingHandler()
}) })
return err return err
} }
func (m *PingManager) start() error { // pingHandler is the main goroutine responsible for enforcing the ping/pong
m.pingTicker = time.NewTicker(m.cfg.IntervalDuration) // protocol.
func (m *PingManager) pingHandler() {
m.pingTimeout = time.NewTimer(0) defer m.wg.Done()
defer m.pingTimeout.Stop() defer m.pingTimeout.Stop()
// Ensure that the pingTimeout channel is empty. // Ensure that the pingTimeout channel is empty.
@@ -115,89 +119,81 @@ func (m *PingManager) start() error {
<-m.pingTimeout.C <-m.pingTimeout.C
} }
m.wg.Add(1) for {
go func() { select {
defer m.wg.Done() case <-m.pingTicker.C:
for { // If this occurs it means that the new ping cycle has
select { // begun while there is still an outstanding ping
case <-m.pingTicker.C: // awaiting a pong response. This should never occur,
// If this occurs it means that the new ping // but if it does, it implies a timeout.
// cycle has begun while there is still an if m.outstandingPongSize >= 0 {
// outstanding ping awaiting a pong response. e := errors.New("impossible: new ping" +
// This should never occur, but if it does, it "in unclean state",
// implies a timeout. )
if m.outstandingPongSize >= 0 { m.cfg.OnPongFailure(e)
e := errors.New("impossible: new ping" +
"in unclean state",
)
m.cfg.OnPongFailure(e)
return return
} }
pongSize := m.cfg.NewPongSize() pongSize := m.cfg.NewPongSize()
ping := &lnwire.Ping{ ping := &lnwire.Ping{
NumPongBytes: pongSize, NumPongBytes: pongSize,
PaddingBytes: m.cfg.NewPingPayload(), PaddingBytes: m.cfg.NewPingPayload(),
} }
// Set up our bookkeeping for the new Ping. // Set up our bookkeeping for the new Ping.
if err := m.setPingState(pongSize); err != nil { if err := m.setPingState(pongSize); err != nil {
m.cfg.OnPongFailure(err)
m.cfg.OnPongFailure(err) return
}
return m.cfg.SendPing(ping)
}
m.cfg.SendPing(ping) case <-m.pingTimeout.C:
m.resetPingState()
case <-m.pingTimeout.C: e := errors.New("timeout while waiting for " +
m.resetPingState() "pong response",
)
e := errors.New("timeout while waiting for " + m.cfg.OnPongFailure(e)
"pong response",
return
case pong := <-m.pongChan:
pongSize := int32(len(pong.PongBytes))
// Save off values we are about to override when we
// call resetPingState.
expected := m.outstandingPongSize
lastPing := m.pingLastSend
m.resetPingState()
// 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",
) )
m.cfg.OnPongFailure(e) m.cfg.OnPongFailure(e)
return return
case pong := <-m.pongChan:
pongSize := int32(len(pong.PongBytes))
// Save off values we are about to override
// when we call resetPingState.
expected := m.outstandingPongSize
lastPing := m.pingLastSend
m.resetPingState()
// 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",
)
m.cfg.OnPongFailure(e)
return
}
// Compute RTT of ping and save that for future
// querying.
if lastPing != nil {
rtt := time.Since(*lastPing)
m.pingTime.Store(&rtt)
}
case <-m.quit:
return
} }
}
}()
return nil // Compute RTT of ping and save that for future
// querying.
if lastPing != nil {
rtt := time.Since(*lastPing)
m.pingTime.Store(&rtt)
}
case <-m.quit:
return
}
}
} }
// Stop interrupts the goroutines that the PingManager owns. Can only be called // Stop interrupts the goroutines that the PingManager owns. Can only be called