mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 03:30:37 +00:00
p2p: limit ping requests from a single peer (#8113)
see: https://github.com/ethereum/go-ethereum/pull/27887
This commit is contained in:
parent
ba1e4679fc
commit
d6df923dd8
12
p2p/peer.go
12
p2p/peer.go
@ -114,6 +114,7 @@ type Peer struct {
|
|||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
protoErr chan *PeerError
|
protoErr chan *PeerError
|
||||||
closed chan struct{}
|
closed chan struct{}
|
||||||
|
pingRecv chan struct{}
|
||||||
disc chan *PeerError
|
disc chan *PeerError
|
||||||
|
|
||||||
// events receives message send / receive events if set
|
// events receives message send / receive events if set
|
||||||
@ -219,6 +220,7 @@ func newPeer(logger log.Logger, conn *conn, protocols []Protocol, pubkey [64]byt
|
|||||||
disc: make(chan *PeerError),
|
disc: make(chan *PeerError),
|
||||||
protoErr: make(chan *PeerError, len(protomap)+1), // protocols + pingLoop
|
protoErr: make(chan *PeerError, len(protomap)+1), // protocols + pingLoop
|
||||||
closed: make(chan struct{}),
|
closed: make(chan struct{}),
|
||||||
|
pingRecv: make(chan struct{}, 16),
|
||||||
log: logger.New("id", conn.node.ID(), "conn", conn.flags),
|
log: logger.New("id", conn.node.ID(), "conn", conn.flags),
|
||||||
pubkey: pubkey,
|
pubkey: pubkey,
|
||||||
metricsEnabled: metricsEnabled,
|
metricsEnabled: metricsEnabled,
|
||||||
@ -287,6 +289,11 @@ func (p *Peer) pingLoop() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ping.Reset(pingInterval)
|
ping.Reset(pingInterval)
|
||||||
|
case <-p.pingRecv:
|
||||||
|
if err := SendItems(p.rw, pongMsg); err != nil {
|
||||||
|
p.protoErr <- NewPeerError(PeerErrorPongFailure, DiscNetworkError, err, "Failed to send pongMsg")
|
||||||
|
return
|
||||||
|
}
|
||||||
case <-p.closed:
|
case <-p.closed:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -314,7 +321,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:
|
||||||
// This is the last message.
|
// This is the last message.
|
||||||
// We don't need to discard because the connection will be closed after it.
|
// We don't need to discard because the connection will be closed after it.
|
||||||
|
@ -26,6 +26,7 @@ const (
|
|||||||
PeerErrorInvalidMessageCode PeerErrorCode = iota
|
PeerErrorInvalidMessageCode PeerErrorCode = iota
|
||||||
PeerErrorInvalidMessage
|
PeerErrorInvalidMessage
|
||||||
PeerErrorPingFailure
|
PeerErrorPingFailure
|
||||||
|
PeerErrorPongFailure
|
||||||
PeerErrorDiscReason
|
PeerErrorDiscReason
|
||||||
PeerErrorDiscReasonRemote
|
PeerErrorDiscReasonRemote
|
||||||
PeerErrorMessageReceive
|
PeerErrorMessageReceive
|
||||||
@ -47,6 +48,7 @@ var peerErrorCodeToString = map[PeerErrorCode]string{
|
|||||||
PeerErrorInvalidMessageCode: "invalid message code",
|
PeerErrorInvalidMessageCode: "invalid message code",
|
||||||
PeerErrorInvalidMessage: "invalid message",
|
PeerErrorInvalidMessage: "invalid message",
|
||||||
PeerErrorPingFailure: "ping failure",
|
PeerErrorPingFailure: "ping failure",
|
||||||
|
PeerErrorPongFailure: "pong failure",
|
||||||
PeerErrorDiscReason: "disconnect reason",
|
PeerErrorDiscReason: "disconnect reason",
|
||||||
PeerErrorDiscReasonRemote: "remote disconnect reason",
|
PeerErrorDiscReasonRemote: "remote disconnect reason",
|
||||||
PeerErrorMessageReceive: "failed to receive a message",
|
PeerErrorMessageReceive: "failed to receive a message",
|
||||||
|
Loading…
Reference in New Issue
Block a user