Skip to content

Instantly share code, notes, and snippets.

@danish-rehman
Created June 3, 2015 06:52
Show Gist options
  • Save danish-rehman/7b2989d6ed09f2c22d41 to your computer and use it in GitHub Desktop.
Save danish-rehman/7b2989d6ed09f2c22d41 to your computer and use it in GitHub Desktop.
token_bucket
rate = 5.0; // unit: messages
per = 8.0; // unit: seconds
allowance = rate; // unit: messages
last_check = now(); // floating-point, e.g. usec accuracy. Unit: seconds
when (message_received):
current = now();
time_passed = current - last_check;
last_check = current;
allowance += time_passed * (rate / per);
if (allowance > rate):
allowance = rate; // throttle
if (allowance < 1.0):
discard_message();
else:
forward_message();
allowance -= 1.0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment