chanfitness: cool down flap count for rate limiting
Since we store all-time flap count for a peer, we add a cooldown factor which will discount poor flap counts in the past. This is only applied to peers that have not flapped for at least a cooldown period, so that we do not downgrade our rate limiting for badly behaved peers.
This commit is contained in:
4 files changed
+155
-5
No files matched your search
@@ -49,3 +49,57 @@ func TestGetRateLimit(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCooldownFlapCount tests cooldown of all time flap counts.
|
||||
func TestCooldownFlapCount(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
flapCount int
|
||||
lastFlap time.Time
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "just flapped, do not cooldown",
|
||||
flapCount: 1,
|
||||
lastFlap: testNow,
|
||||
expected: 1,
|
||||
},
|
||||
{
|
||||
name: "period not elapsed, do not cooldown",
|
||||
flapCount: 1,
|
||||
lastFlap: testNow.Add(flapCountCooldownPeriod / 2 * -1),
|
||||
expected: 1,
|
||||
},
|
||||
{
|
||||
name: "rounded to 0",
|
||||
flapCount: 1,
|
||||
lastFlap: testNow.Add(flapCountCooldownPeriod * -1),
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "decreased to integer value",
|
||||
flapCount: 10,
|
||||
lastFlap: testNow.Add(flapCountCooldownPeriod * -1),
|
||||
expected: 9,
|
||||
},
|
||||
{
|
||||
name: "multiple cooldown periods",
|
||||
flapCount: 10,
|
||||
lastFlap: testNow.Add(flapCountCooldownPeriod * -3),
|
||||
expected: 8,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
flapCount := cooldownFlapCount(
|
||||
testNow, test.flapCount, test.lastFlap,
|
||||
)
|
||||
require.Equal(t, test.expected, flapCount)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user