mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-09 18:52:09 +02:00
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:
@@ -98,16 +98,20 @@ func NewPingManager(cfg *PingManagerConfig) *PingManager {
|
||||
func (m *PingManager) Start() error {
|
||||
var err error
|
||||
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
|
||||
}
|
||||
|
||||
func (m *PingManager) start() error {
|
||||
m.pingTicker = time.NewTicker(m.cfg.IntervalDuration)
|
||||
|
||||
m.pingTimeout = time.NewTimer(0)
|
||||
// pingHandler is the main goroutine responsible for enforcing the ping/pong
|
||||
// protocol.
|
||||
func (m *PingManager) pingHandler() {
|
||||
defer m.wg.Done()
|
||||
defer m.pingTimeout.Stop()
|
||||
|
||||
// Ensure that the pingTimeout channel is empty.
|
||||
@@ -115,17 +119,13 @@ func (m *PingManager) start() error {
|
||||
<-m.pingTimeout.C
|
||||
}
|
||||
|
||||
m.wg.Add(1)
|
||||
go func() {
|
||||
defer m.wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-m.pingTicker.C:
|
||||
// If this occurs it means that the new ping
|
||||
// cycle has begun while there is still an
|
||||
// outstanding ping awaiting a pong response.
|
||||
// This should never occur, but if it does, it
|
||||
// implies a timeout.
|
||||
// If this occurs it means that the new ping cycle has
|
||||
// begun while there is still an outstanding ping
|
||||
// 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",
|
||||
@@ -143,7 +143,6 @@ func (m *PingManager) start() error {
|
||||
|
||||
// Set up our bookkeeping for the new Ping.
|
||||
if err := m.setPingState(pongSize); err != nil {
|
||||
|
||||
m.cfg.OnPongFailure(err)
|
||||
|
||||
return
|
||||
@@ -165,15 +164,15 @@ func (m *PingManager) start() error {
|
||||
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
|
||||
|
||||
m.resetPingState()
|
||||
|
||||
// If the pong we receive doesn't match the
|
||||
// ping we sent out, then we fail out.
|
||||
// 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",
|
||||
@@ -195,9 +194,6 @@ func (m *PingManager) start() error {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop interrupts the goroutines that the PingManager owns. Can only be called
|
||||
|
Reference in New Issue
Block a user