limits: sync streams, listings per author, inbound connections per IP
A peer may hold 4 sync streams and open 1500 a minute; banned peers get none and cannot redial. A node keeps at most 100 unexpired listings per author (--max-per-author); updates, renewals and deletions of held ones still work. At most 16 inbound connections per public IP or IPv6 /64 (--max-conns-per-ip); local and private addresses are exempt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
+49
-3
@@ -38,8 +38,9 @@ type peerGuard struct {
|
||||
disconnect func(peer.ID)
|
||||
self peer.ID
|
||||
|
||||
mu sync.Mutex
|
||||
peers map[peer.ID]*peerCount
|
||||
mu sync.Mutex
|
||||
peers map[peer.ID]*peerCount
|
||||
streams map[peer.ID]int // sync streams open per peer
|
||||
}
|
||||
|
||||
type peerCount struct {
|
||||
@@ -47,6 +48,9 @@ type peerCount struct {
|
||||
total int
|
||||
bad int
|
||||
noted bool // the flood was already logged this window
|
||||
|
||||
syncs int // sync requests this window
|
||||
syncNoted bool
|
||||
}
|
||||
|
||||
func newPeerGuard(self peer.ID, now func() time.Time, log *slog.Logger, disconnect func(peer.ID)) (*peerGuard, error) {
|
||||
@@ -54,7 +58,8 @@ func newPeerGuard(self peer.ID, now func() time.Time, log *slog.Logger, disconne
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &peerGuard{now: now, log: log, bl: bl, disconnect: disconnect, self: self, peers: map[peer.ID]*peerCount{}}, nil
|
||||
return &peerGuard{now: now, log: log, bl: bl, disconnect: disconnect, self: self,
|
||||
peers: map[peer.ID]*peerCount{}, streams: map[peer.ID]int{}}, nil
|
||||
}
|
||||
|
||||
// wrap puts the guard around a topic validator. Messages we published
|
||||
@@ -128,5 +133,46 @@ func (g *peerGuard) bad(p peer.ID) {
|
||||
}
|
||||
}
|
||||
|
||||
// Limits on what a peer may ask of us over the sync protocol. A full sync of a
|
||||
// region of 100,000 listings is about 200 inventory pages and a thousand
|
||||
// fetches, so the rate leaves room for it; the limit on parallel streams is
|
||||
// what stops one peer from tying up the node.
|
||||
const (
|
||||
maxSyncPerWindow = 1500
|
||||
maxSyncStreams = 4
|
||||
)
|
||||
|
||||
// beginSync is called when p opens a sync stream. It refuses banned peers,
|
||||
// peers asking too often and peers with too many streams open; otherwise it
|
||||
// returns the function to call when the stream is done.
|
||||
func (g *peerGuard) beginSync(p peer.ID) (end func(), ok bool) {
|
||||
if g.banned(p) {
|
||||
return nil, false
|
||||
}
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
c := g.count(p)
|
||||
c.syncs++
|
||||
if c.syncs > maxSyncPerWindow {
|
||||
if !c.syncNoted {
|
||||
c.syncNoted = true
|
||||
g.log.Warn("peer asks for too much sync; refusing its requests until the minute is over",
|
||||
"peer", p, "limit_per_minute", maxSyncPerWindow)
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
if g.streams[p] >= maxSyncStreams {
|
||||
return nil, false
|
||||
}
|
||||
g.streams[p]++
|
||||
return func() {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
if g.streams[p]--; g.streams[p] <= 0 {
|
||||
delete(g.streams, p)
|
||||
}
|
||||
}, true
|
||||
}
|
||||
|
||||
// banned reports whether p is currently banned.
|
||||
func (g *peerGuard) banned(p peer.ID) bool { return g.bl.Contains(p) }
|
||||
|
||||
Reference in New Issue
Block a user