Skip to content

Commit 734021f

Browse files
iczcfjl
iczc
andauthored
p2p: limit ping requests from a single peer (#510)
* p2p: move ping handling into pingLoop goroutine (ethereum#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>
1 parent 9c1433b commit 734021f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

p2p/peer.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ type Peer struct {
112112
wg sync.WaitGroup
113113
protoErr chan error
114114
closed chan struct{}
115+
pingRecv chan struct{}
115116
disc chan DiscReason
116117

117118
// events receives message send / receive events if set
@@ -225,6 +226,7 @@ func newPeer(log log.Logger, conn *conn, protocols []Protocol) *Peer {
225226
disc: make(chan DiscReason),
226227
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
227228
closed: make(chan struct{}),
229+
pingRecv: make(chan struct{}, 16),
228230
log: log.New("id", conn.node.ID(), "conn", conn.flags),
229231
}
230232
return p
@@ -285,9 +287,11 @@ loop:
285287
}
286288

287289
func (p *Peer) pingLoop() {
288-
ping := time.NewTimer(pingInterval)
289290
defer p.wg.Done()
291+
292+
ping := time.NewTimer(pingInterval)
290293
defer ping.Stop()
294+
291295
for {
292296
select {
293297
case <-ping.C:
@@ -296,6 +300,10 @@ func (p *Peer) pingLoop() {
296300
return
297301
}
298302
ping.Reset(pingInterval)
303+
304+
case <-p.pingRecv:
305+
SendItems(p.rw, pongMsg)
306+
299307
case <-p.closed:
300308
return
301309
}
@@ -322,7 +330,10 @@ func (p *Peer) handle(msg Msg) error {
322330
switch {
323331
case msg.Code == pingMsg:
324332
msg.Discard()
325-
go SendItems(p.rw, pongMsg)
333+
select {
334+
case p.pingRecv <- struct{}{}:
335+
case <-p.closed:
336+
}
326337
case msg.Code == discMsg:
327338
// This is the last message. We don't need to discard or
328339
// check errors because, the connection will be closed after it.

params/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
const (
2525
VersionMajor = 4 // Major version component of the current release
2626
VersionMinor = 4 // Minor version component of the current release
27-
VersionPatch = 5 // Patch version component of the current release
27+
VersionPatch = 6 // Patch version component of the current release
2828
VersionMeta = "sepolia" // Version metadata to append to the version string
2929
)
3030

0 commit comments

Comments
 (0)