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

+41
View File
@@ -204,6 +204,47 @@ func TestRateLimitAdd(t *testing.T) {
require.Equal(t, []*event{peerEvent}, peerLog.onlineEvents)
require.Equal(t, nextEvent, peerLog.stagedEvent)
}
// Now, we test the case where a peer's flap count is cooled down
// because it has not flapped for a while. Set our peer's flap count so
// that we fall within our second rate limiting tier and assert that we
// are at this level.
peerLog.flapCount = rateLimitScale + 1
rateLimit := getRateLimit(peerLog.flapCount)
require.Equal(t, rateLimits[1], rateLimit)
// Progress our clock to the point where we will have our flap count
// cooled.
newNow = mockedClock.Now().Add(flapCountCooldownPeriod)
mockedClock.SetTime(newNow)
// Add an online event, and expect it to be staged.
onlineEvent := &event{
timestamp: newNow,
eventType: peerOnlineEvent,
}
peerLog.onlineEvent(true)
require.Equal(t, onlineEvent, peerLog.stagedEvent)
// Progress our clock by the rate limit level that we will be on if
// our flap rate is cooled down to a lower level.
newNow = mockedClock.Now().Add(rateLimits[0] + 1)
mockedClock.SetTime(newNow)
// Add another event. We expect this event to be staged and our previous
// event to be flushed to the event log (because our cooldown has been
// applied).
offlineEvent := &event{
timestamp: newNow,
eventType: peerOfflineEvent,
}
peerLog.onlineEvent(false)
require.Equal(t, offlineEvent, peerLog.stagedEvent)
flushedEventIdx := len(peerLog.onlineEvents) - 1
require.Equal(
t, onlineEvent, peerLog.onlineEvents[flushedEventIdx],
)
}
// TestGetOnlinePeriod tests the getOnlinePeriod function. It tests the case