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:
carla committed 2020-09-08 15:01:02 +02:00
1 parent a550ca3d64
commit 6cf66aea47
4 files changed
+155 -5

No files matched your search

+54
View File
@@ -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)
})
}
}