Skip to content

Instantly share code, notes, and snippets.

@mrkirby153
Created June 25, 2018 08:07
Show Gist options
  • Save mrkirby153/c4cf35fae45bdcd9d63ef3a738861042 to your computer and use it in GitHub Desktop.
Save mrkirby153/c4cf35fae45bdcd9d63ef3a738861042 to your computer and use it in GitHub Desktop.
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