mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 05:36:46 +00:00
p2p: limit ping requests from a single peer (#510)
* p2p: move ping handling into pingLoop goroutine (#27887) Moving the response sending there allows tracking all peer goroutines in the peer WaitGroup. * bump version --------- Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
parent
9c1433b62a
commit
734021fc27
2 changed files with 14 additions and 3 deletions
15
p2p/peer.go
15
p2p/peer.go
|
|
@ -112,6 +112,7 @@ type Peer struct {
|
|||
wg sync.WaitGroup
|
||||
protoErr chan error
|
||||
closed chan struct{}
|
||||
pingRecv chan struct{}
|
||||
disc chan DiscReason
|
||||
|
||||
// events receives message send / receive events if set
|
||||
|
|
@ -225,6 +226,7 @@ func newPeer(log log.Logger, conn *conn, protocols []Protocol) *Peer {
|
|||
disc: make(chan DiscReason),
|
||||
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
|
||||
closed: make(chan struct{}),
|
||||
pingRecv: make(chan struct{}, 16),
|
||||
log: log.New("id", conn.node.ID(), "conn", conn.flags),
|
||||
}
|
||||
return p
|
||||
|
|
@ -285,9 +287,11 @@ loop:
|
|||
}
|
||||
|
||||
func (p *Peer) pingLoop() {
|
||||
ping := time.NewTimer(pingInterval)
|
||||
defer p.wg.Done()
|
||||
|
||||
ping := time.NewTimer(pingInterval)
|
||||
defer ping.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ping.C:
|
||||
|
|
@ -296,6 +300,10 @@ func (p *Peer) pingLoop() {
|
|||
return
|
||||
}
|
||||
ping.Reset(pingInterval)
|
||||
|
||||
case <-p.pingRecv:
|
||||
SendItems(p.rw, pongMsg)
|
||||
|
||||
case <-p.closed:
|
||||
return
|
||||
}
|
||||
|
|
@ -322,7 +330,10 @@ func (p *Peer) handle(msg Msg) error {
|
|||
switch {
|
||||
case msg.Code == pingMsg:
|
||||
msg.Discard()
|
||||
go SendItems(p.rw, pongMsg)
|
||||
select {
|
||||
case p.pingRecv <- struct{}{}:
|
||||
case <-p.closed:
|
||||
}
|
||||
case msg.Code == discMsg:
|
||||
// This is the last message. We don't need to discard or
|
||||
// check errors because, the connection will be closed after it.
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ import (
|
|||
const (
|
||||
VersionMajor = 4 // Major version component of the current release
|
||||
VersionMinor = 4 // Minor version component of the current release
|
||||
VersionPatch = 5 // Patch version component of the current release
|
||||
VersionPatch = 6 // Patch version component of the current release
|
||||
VersionMeta = "sepolia" // Version metadata to append to the version string
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue