Skip to content

Commit

Permalink
Drop faulty neighbors.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrm50 committed Jun 5, 2024
1 parent f0b266a commit 65060ce
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/network/p2p/neighbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package p2p
import (
"context"
"sync"
"sync/atomic"
"time"

"github.com/libp2p/go-libp2p/core/protocol"
Expand All @@ -15,7 +16,8 @@ import (
)

const (
NeighborsSendQueueSize = 20_000
NeighborsSendQueueSize = 20_000
DroppedPacketDisconnectThreshold = 100
)

type queuedPacket struct {
Expand All @@ -31,7 +33,8 @@ type (

// neighbor describes the established p2p connection to another peer.
type neighbor struct {
peer *network.Peer
peer *network.Peer
droppedPacketCounter atomic.Uint32

logger log.Logger

Expand Down Expand Up @@ -84,7 +87,12 @@ func (n *neighbor) Peer() *network.Peer {
func (n *neighbor) Enqueue(packet proto.Message, protocolID protocol.ID) {
select {
case n.sendQueue <- &queuedPacket{protocolID: protocolID, packet: packet}:
n.droppedPacketCounter.Store(0)
default:
// Drop a neighbor that does not read from the full queue.
if n.droppedPacketCounter.Add(1) >= DroppedPacketDisconnectThreshold {
n.Close()
}
n.logger.LogWarn("Dropped packet due to SendQueue being full")
}
}
Expand Down

0 comments on commit 65060ce

Please sign in to comment.