Created
June 25, 2018 08:07
-
-
Save mrkirby153/c4cf35fae45bdcd9d63ef3a738861042 to your computer and use it in GitHub Desktop.
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
spamBucket := {} // A key/value store. Can be anything, really | |
count := 10 | |
period := 30 | |
fun violate(count): | |
start := currentTime() - period | |
end := currentTime() | |
// Clean up expired keys | |
for k, v in spamBucket: | |
if(v < start): | |
spamBucket.remove(k) | |
for i in 0 until count: | |
spamBucket.add(currentTime()+"-"+i, currentTime()) | |
violations := 0 | |
// Check if there are any keys that are in the range of time we want to consider | |
for k, v in spamBucket | |
if(v > start && v < end): | |
violations := violations + 1 | |
if(violations > count): | |
throw ViolationException | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment