-
-
Save rschrieken/5b7306450eb9ea74eaaa to your computer and use it in GitHub Desktop.
rate limiter
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
(function currentRate(seconds, currenttime) { | |
var i = seconds.length-1, | |
limit,msgnumber,deltafromstart, rate, deltaseconds, previousseconds = currenttime; | |
for(var l = seconds.length; i > 0 ; i = i - 1) { | |
msgnumber = l - i; | |
deltafromstart = currenttime - seconds[i]; | |
deltaseconds = previousseconds - seconds[i]; | |
previousseconds = seconds[i]; | |
limit = rateLimit(msgnumber); | |
rate = deltafromstart / msgnumber; | |
console.log( | |
'msgnumber: %d secs: %d diff: %d rate: %d cl: %d limit: %s', | |
msgnumber, deltafromstart, deltaseconds, rate, limit, deltaseconds > limit?'OK':'LIMIT'); | |
} | |
function rateLimit(x) { | |
return Math.min((4.1484 * Math.log(x) + 1.02242), 20); | |
} | |
})([1,2,3,4,5,6,7,8,9,10,11,12,13,15,18,22,26,32,41,46,49,55,61,67,69,76,83,92,98,105,109,113,117], 120) // heavily rate limited |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment