peer: Separate ping ticker functionality from outHandler.
This is part of a series of commits to make the internals of the peer package more modular, testable and tunable. No functionality has changed.
This commit is contained in:
parent
760c5299c7
commit
83432785f3
1 changed files with 23 additions and 12 deletions
35
peer/peer.go
35
peer/peer.go
|
@ -1746,10 +1746,6 @@ func (p *Peer) shouldLogWriteError(err error) bool {
|
||||||
// goroutine. It uses a buffered channel to serialize output messages while
|
// goroutine. It uses a buffered channel to serialize output messages while
|
||||||
// allowing the sender to continue running asynchronously.
|
// allowing the sender to continue running asynchronously.
|
||||||
func (p *Peer) outHandler() {
|
func (p *Peer) outHandler() {
|
||||||
// pingTicker is used to periodically send pings to the remote peer.
|
|
||||||
pingTicker := time.NewTicker(pingInterval)
|
|
||||||
defer pingTicker.Stop()
|
|
||||||
|
|
||||||
out:
|
out:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -1790,14 +1786,6 @@ out:
|
||||||
}
|
}
|
||||||
p.sendDoneQueue <- struct{}{}
|
p.sendDoneQueue <- struct{}{}
|
||||||
|
|
||||||
case <-pingTicker.C:
|
|
||||||
nonce, err := wire.RandomUint64()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Not sending ping to %s: %v", p, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
p.QueueMessage(wire.NewMsgPing(nonce), nil)
|
|
||||||
|
|
||||||
case <-p.quit:
|
case <-p.quit:
|
||||||
break out
|
break out
|
||||||
}
|
}
|
||||||
|
@ -1825,6 +1813,28 @@ cleanup:
|
||||||
log.Tracef("Peer output handler done for %s", p)
|
log.Tracef("Peer output handler done for %s", p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pingHandler periodically pings the peer. It must be run as a goroutine.
|
||||||
|
func (p *Peer) pingHandler() {
|
||||||
|
pingTicker := time.NewTicker(pingInterval)
|
||||||
|
defer pingTicker.Stop()
|
||||||
|
|
||||||
|
out:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-pingTicker.C:
|
||||||
|
nonce, err := wire.RandomUint64()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Not sending ping to %s: %v", p, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
p.QueueMessage(wire.NewMsgPing(nonce), nil)
|
||||||
|
|
||||||
|
case <-p.quit:
|
||||||
|
break out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// QueueMessage adds the passed bitcoin message to the peer send queue.
|
// QueueMessage adds the passed bitcoin message to the peer send queue.
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
|
@ -1952,6 +1962,7 @@ func (p *Peer) start() error {
|
||||||
go p.inHandler()
|
go p.inHandler()
|
||||||
go p.queueHandler()
|
go p.queueHandler()
|
||||||
go p.outHandler()
|
go p.outHandler()
|
||||||
|
go p.pingHandler()
|
||||||
|
|
||||||
// Send our verack message now that the IO processing machinery has started.
|
// Send our verack message now that the IO processing machinery has started.
|
||||||
p.QueueMessage(wire.NewMsgVerAck(), nil)
|
p.QueueMessage(wire.NewMsgVerAck(), nil)
|
||||||
|
|
Loading…
Reference in a new issue