Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Last active March 30, 2022 03:37
Show Gist options
  • Select an option

  • Save jayhuang75/6b7decb57a5139b1ad2120091c73ce46 to your computer and use it in GitHub Desktop.

Select an option

Save jayhuang75/6b7decb57a5139b1ad2120091c73ce46 to your computer and use it in GitHub Desktop.
token_bucket_update
fn update(&mut self) {
let current = Utc::now();
let diff = current.time() - self.last_refill_timestamp.time();
let tokens_added = diff.num_seconds() * self.rate / 1000000000;
self.current_tokens = cmp::min(self.current_tokens + tokens_added, self.max_tokens);
self.last_refill_timestamp = current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment