mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-15 09:20:44 +00:00
p2p: move ping handling into pingLoop goroutine (#27887)
Moving the response sending there allows tracking all peer goroutines in the peer WaitGroup.
This commit is contained in:
parent
24d02fe2b4
commit
f5cdfb2730
1 changed files with 13 additions and 2 deletions
15
p2p/peer.go
15
p2p/peer.go
|
|
@ -105,6 +105,7 @@ type Peer struct {
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
protoErr chan error
|
protoErr chan error
|
||||||
closed chan struct{}
|
closed chan struct{}
|
||||||
|
pingRecv chan struct{}
|
||||||
disc chan DiscReason
|
disc chan DiscReason
|
||||||
|
|
||||||
// events receives message send / receive events if set
|
// events receives message send / receive events if set
|
||||||
|
|
@ -175,6 +176,7 @@ func newPeer(conn *conn, protocols []Protocol) *Peer {
|
||||||
disc: make(chan DiscReason),
|
disc: make(chan DiscReason),
|
||||||
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
|
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
|
||||||
closed: make(chan struct{}),
|
closed: make(chan struct{}),
|
||||||
|
pingRecv: make(chan struct{}, 16),
|
||||||
log: log.New("id", conn.id, "conn", conn.flags),
|
log: log.New("id", conn.id, "conn", conn.flags),
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
|
|
@ -236,9 +238,11 @@ loop:
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Peer) pingLoop() {
|
func (p *Peer) pingLoop() {
|
||||||
ping := time.NewTimer(pingInterval)
|
|
||||||
defer p.wg.Done()
|
defer p.wg.Done()
|
||||||
|
|
||||||
|
ping := time.NewTimer(pingInterval)
|
||||||
defer ping.Stop()
|
defer ping.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ping.C:
|
case <-ping.C:
|
||||||
|
|
@ -247,6 +251,10 @@ func (p *Peer) pingLoop() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ping.Reset(pingInterval)
|
ping.Reset(pingInterval)
|
||||||
|
|
||||||
|
case <-p.pingRecv:
|
||||||
|
SendItems(p.rw, pongMsg)
|
||||||
|
|
||||||
case <-p.closed:
|
case <-p.closed:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -273,7 +281,10 @@ func (p *Peer) handle(msg Msg) error {
|
||||||
switch {
|
switch {
|
||||||
case msg.Code == pingMsg:
|
case msg.Code == pingMsg:
|
||||||
msg.Discard()
|
msg.Discard()
|
||||||
go SendItems(p.rw, pongMsg)
|
select {
|
||||||
|
case p.pingRecv <- struct{}{}:
|
||||||
|
case <-p.closed:
|
||||||
|
}
|
||||||
case msg.Code == discMsg:
|
case msg.Code == discMsg:
|
||||||
var reason [1]DiscReason
|
var reason [1]DiscReason
|
||||||
// This is the last message. We don't need to discard or
|
// This is the last message. We don't need to discard or
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue