-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dot/network): Add warp sync spam limiter (#4219)
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2024 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package ratelimiters | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
lrucache "github.com/ChainSafe/gossamer/lib/utils/lru-cache" | ||
) | ||
|
||
const DefaultMaxSlidingWindowTime = 1 * time.Minute | ||
const DefaultMaxCachedRequestSize = 500 | ||
|
||
// SlidingWindowRateLimiter is a rate limiter implementation designed to prevent | ||
// more than `maxReqs` requests from being processed within a `windowSize` time window | ||
type SlidingWindowRateLimiter struct { | ||
mu sync.Mutex | ||
limits *lrucache.LRUCache[common.Hash, []time.Time] | ||
maxReqs uint32 | ||
windowSize time.Duration | ||
} | ||
|
||
// NewSlidingWindowRateLimiter creates a new SlidingWindowRateLimiter with the given maximum number of requests | ||
func NewSlidingWindowRateLimiter(maxReqs uint32, windowSize time.Duration) *SlidingWindowRateLimiter { | ||
return &SlidingWindowRateLimiter{ | ||
limits: lrucache.NewLRUCache[common.Hash, []time.Time](DefaultMaxCachedRequestSize), | ||
maxReqs: maxReqs, | ||
windowSize: windowSize, | ||
} | ||
} | ||
|
||
// AddRequest adds a request to the SlidingWindowRateLimiter | ||
func (rl *SlidingWindowRateLimiter) AddRequest(id common.Hash) { | ||
rl.mu.Lock() | ||
defer rl.mu.Unlock() | ||
|
||
recentRequests := rl.recentRequests(id) | ||
|
||
// Add the current request and update the cache | ||
recentRequests = append(recentRequests, time.Now()) | ||
rl.limits.Put(id, recentRequests) | ||
} | ||
|
||
// IsLimitExceeded returns true if the limit is exceeded for the given peer and hash | ||
func (rl *SlidingWindowRateLimiter) IsLimitExceeded(id common.Hash) bool { | ||
rl.mu.Lock() | ||
defer rl.mu.Unlock() | ||
|
||
recentRequests := rl.recentRequests(id) | ||
rl.limits.Put(id, recentRequests) | ||
|
||
return uint32(len(recentRequests)) > rl.maxReqs | ||
} | ||
|
||
func (rl *SlidingWindowRateLimiter) recentRequests(id common.Hash) []time.Time { | ||
// Get the timestamps for the hash | ||
timestamps := rl.limits.Get(id) | ||
if timestamps == nil { | ||
return []time.Time{} | ||
} | ||
|
||
now := time.Now() | ||
|
||
// Filter requests that are within the time window | ||
var recentRequests []time.Time | ||
for _, t := range timestamps { | ||
if now.Sub(t) <= rl.windowSize { | ||
recentRequests = append(recentRequests, t) | ||
} | ||
} | ||
|
||
return recentRequests | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2024 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package ratelimiters | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSlidingWindowRateLimiter_AddRequestAndCheckLimitExceeded(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Create a SlidingWindowRateLimiter with a limit of 5 requests and a time window of 10 seconds | ||
limiter := NewSlidingWindowRateLimiter(5, 10*time.Second) | ||
|
||
hash := common.Hash{0x01} | ||
|
||
// Add 5 requests for the same hash | ||
for i := 0; i < 5; i++ { | ||
limiter.AddRequest(hash) | ||
} | ||
|
||
// Limit should not be exceeded after 5 requests | ||
assert.False(t, limiter.IsLimitExceeded(hash)) | ||
|
||
// Add one more request and check that the limit is exceeded | ||
limiter.AddRequest(hash) | ||
assert.True(t, limiter.IsLimitExceeded(hash)) | ||
} | ||
|
||
func TestSlidingWindowRateLimiter_WindowExpiry(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Create a SlidingWindowRateLimiter with a limit of 3 requests and a time window of 1 second | ||
limiter := NewSlidingWindowRateLimiter(3, 1*time.Second) | ||
|
||
hash := common.Hash{0x02} | ||
|
||
// Add 3 requests | ||
for i := 0; i < 3; i++ { | ||
limiter.AddRequest(hash) | ||
} | ||
|
||
// Limit should not be exceeded | ||
assert.False(t, limiter.IsLimitExceeded(hash)) | ||
|
||
// Wait for the time window to expire | ||
time.Sleep(2 * time.Second) | ||
|
||
// Add another request, should be considered as the first in a new window | ||
limiter.AddRequest(hash) | ||
assert.False(t, limiter.IsLimitExceeded(hash)) | ||
} | ||
|
||
func TestSlidingWindowRateLimiter_DifferentHashes(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Create a SlidingWindowRateLimiter with a limit of 2 requests and a time window of 5 seconds | ||
limiter := NewSlidingWindowRateLimiter(2, 5*time.Second) | ||
|
||
hash1 := common.Hash{0x01} | ||
hash2 := common.Hash{0x02} | ||
|
||
// Add requests for hash1 | ||
limiter.AddRequest(hash1) | ||
limiter.AddRequest(hash1) | ||
|
||
// Add requests for hash2 | ||
limiter.AddRequest(hash2) | ||
limiter.AddRequest(hash2) | ||
|
||
// No limit should be exceeded yet | ||
assert.False(t, limiter.IsLimitExceeded(hash1)) | ||
assert.False(t, limiter.IsLimitExceeded(hash2)) | ||
|
||
// Add another request for each and check that the limit is exceeded | ||
limiter.AddRequest(hash1) | ||
assert.True(t, limiter.IsLimitExceeded(hash1)) | ||
|
||
limiter.AddRequest(hash2) | ||
assert.True(t, limiter.IsLimitExceeded(hash2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters