Use a single throttler per type

This commit is contained in:
Andrea Maria Piana 2020-06-09 12:34:22 +02:00
parent 35e95d1568
commit 82deccef60
1 changed files with 10 additions and 15 deletions

View File

@ -113,19 +113,16 @@ type PeerRateLimiterConfig struct {
var defaultPeerRateLimiterConfig = PeerRateLimiterConfig{ var defaultPeerRateLimiterConfig = PeerRateLimiterConfig{
PacketLimitPerSecIP: 10, PacketLimitPerSecIP: 10,
PacketLimitPerSecPeerID: 5, PacketLimitPerSecPeerID: 5,
BytesLimitPerSecIP: 1048576, // 2MB BytesLimitPerSecIP: 1048576, // 1MB
BytesLimitPerSecPeerID: 1048576, // 2MB BytesLimitPerSecPeerID: 1048576, // 1MB
WhitelistedIPs: nil, WhitelistedIPs: nil,
WhitelistedPeerIDs: nil, WhitelistedPeerIDs: nil,
} }
// PeerRateLimiter represents a rate limiter that limits communication between Peers // PeerRateLimiter represents a rate limiter that limits communication between Peers
type PeerRateLimiter struct { type PeerRateLimiter struct {
packetPeerIDThrottler *tb.Throttler packetThrottler *tb.Throttler
packetIPThrottler *tb.Throttler bytesThrottler *tb.Throttler
bytesPeerIDThrottler *tb.Throttler
bytesIPThrottler *tb.Throttler
PacketLimitPerSecIP int64 PacketLimitPerSecIP int64
PacketLimitPerSecPeerID int64 PacketLimitPerSecPeerID int64
@ -146,10 +143,8 @@ func NewPeerRateLimiter(cfg *PeerRateLimiterConfig, handlers ...RateLimiterHandl
} }
return &PeerRateLimiter{ return &PeerRateLimiter{
packetPeerIDThrottler: tb.NewThrottler(time.Millisecond * 100), packetThrottler: tb.NewThrottler(time.Millisecond * 100),
packetIPThrottler: tb.NewThrottler(time.Millisecond * 100), bytesThrottler: tb.NewThrottler(time.Millisecond * 100),
bytesPeerIDThrottler: tb.NewThrottler(time.Millisecond * 100),
bytesIPThrottler: tb.NewThrottler(time.Millisecond * 100),
PacketLimitPerSecIP: cfg.PacketLimitPerSecIP, PacketLimitPerSecIP: cfg.PacketLimitPerSecIP,
PacketLimitPerSecPeerID: cfg.PacketLimitPerSecPeerID, PacketLimitPerSecPeerID: cfg.PacketLimitPerSecPeerID,
BytesLimitPerSecIP: cfg.BytesLimitPerSecIP, BytesLimitPerSecIP: cfg.BytesLimitPerSecIP,
@ -253,10 +248,10 @@ func (r *PeerRateLimiter) throttleIP(ip string, size uint32) bool {
var bytesLimiterResponse bool var bytesLimiterResponse bool
if r.PacketLimitPerSecIP != 0 { if r.PacketLimitPerSecIP != 0 {
packetLimiterResponse = r.packetIPThrottler.Halt(ip, 1, r.PacketLimitPerSecIP) packetLimiterResponse = r.packetThrottler.Halt(ip, 1, r.PacketLimitPerSecIP)
} }
if r.BytesLimitPerSecIP != 0 { if r.BytesLimitPerSecIP != 0 {
bytesLimiterResponse = r.bytesIPThrottler.Halt(ip, int64(size), r.BytesLimitPerSecIP) bytesLimiterResponse = r.bytesThrottler.Halt(ip, int64(size), r.BytesLimitPerSecIP)
} }
return packetLimiterResponse || bytesLimiterResponse return packetLimiterResponse || bytesLimiterResponse
@ -274,11 +269,11 @@ func (r *PeerRateLimiter) throttlePeer(peerID []byte, size uint32) bool {
var bytesLimiterResponse bool var bytesLimiterResponse bool
if r.PacketLimitPerSecPeerID != 0 { if r.PacketLimitPerSecPeerID != 0 {
packetLimiterResponse = r.packetPeerIDThrottler.Halt(id.String(), 1, r.PacketLimitPerSecPeerID) packetLimiterResponse = r.packetThrottler.Halt(id.String(), 1, r.PacketLimitPerSecPeerID)
} }
if r.BytesLimitPerSecPeerID != 0 { if r.BytesLimitPerSecPeerID != 0 {
bytesLimiterResponse = r.bytesPeerIDThrottler.Halt(id.String(), int64(size), r.BytesLimitPerSecPeerID) bytesLimiterResponse = r.bytesThrottler.Halt(id.String(), int64(size), r.BytesLimitPerSecPeerID)
} }
return packetLimiterResponse || bytesLimiterResponse return packetLimiterResponse || bytesLimiterResponse