Let's say you want to limit some event to 5 occurrences every 20 seconds, you would set up the RateLimit like so:
var limit = new RateLimit(20); // Initialize a 20 second ticklog
// For events that we initiate...
if (limit.count('myevent_id') <= 5)) {
// Do some stuff
limit.tick('myevent_id');
} else {
// Over the limit!
}
// Or events that we respond to...
if (limit.count('someevent_id') > 5) {
// Respond with failure
} else {
// Respond with success!
}
You could run this forever and ever and the ticklog would remain the same size because it's a fixed length list. As we add new entries to the list, the index wraps around to the beginning (i.e. index % ticklog.length). Every call to count iterates over this fixed length list and performs a dictionary look up for tick count indexed by id.