From c831eee5134dffe0ff62814d6b1e9077f38645d4 Mon Sep 17 00:00:00 2001 From: geanamonte Date: Mon, 7 Oct 2024 23:55:03 -0400 Subject: [PATCH 1/6] added Token Bucket rate limiter algorithm --- other/token_bucket.py | 97 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 other/token_bucket.py diff --git a/other/token_bucket.py b/other/token_bucket.py new file mode 100644 index 000000000000..b188c73d2f90 --- /dev/null +++ b/other/token_bucket.py @@ -0,0 +1,97 @@ +# Implementation of Token Bucket Algorithm +# Token `rate` is added to the bucket every `frequency` in seconds. +# The bucket can hold tokens up to `capacity` (full). +# The bucket starts full. +# Each request consume one token. +# If a token arrives when the bucket is full, token is discarded. +# If a request arrives when bucket is empty, request will be discarded. +# If bucket has tokens available, requests will pass. +# https://en.wikipedia.org/wiki/Token_bucket +import threading +import time + + +class TokenBucketRateLimiter: + def __init__(self, rate: int, capacity: int, frequency: int) -> None: + """ + Initialize a Token Bucket rate limiter. + + :param rate: Number of tokens added to the bucket per refill + :param capacity: Maximum number of tokens the bucket can hold. + :param frequency: Frequency of refill in seconds + >>> bucket = TokenBucketRateLimiter(4, 4, 60) + >>> bucket.tokens + 4 + >>> bucket.capacity + 4 + >>> bucket.frequency + 60 + """ + self.rate = rate # Tokens added per refill + self.capacity = capacity # Maximum capacity of the bucket + self.frequency = frequency # Frequency tokens are refilled + self.tokens = capacity # Current tokens in the bucket + self.last_checked = time.time() # Time when tokens were last checked + self.lock = threading.Lock() # To make the rate limiter thread-safe + + def _add_tokens(self) -> None: + """ + Refill tokens only when a full minute has passed. + >>> bucket = TokenBucketRateLimiter(1, 4, 60) + >>> bucket.tokens # Initially has 4 token (rate) + 4 + >>> bucket._add_tokens() + >>> bucket.tokens # Bucket already full + 4 + """ + current_time = time.time() + elapsed_time = current_time - self.last_checked + + if elapsed_time >= self.frequency: + minutes_passed = int(elapsed_time // self.frequency) + + # Add tokens based on rate + added_tokens = minutes_passed * self.rate + self.tokens = min(self.capacity, self.tokens + added_tokens) + + # Update the last checked time + self.last_checked += minutes_passed * self.frequency + + def allow_request(self) -> bool: + """ + Check if a request is allowed. + If there are enough tokens, it consumes one token. + :return: True if the request is allowed, False otherwise. + >>> bucket = TokenBucketRateLimiter(1, 2, 60) + >>> bucket.allow_request() # Token is available, request passes + True + >>> bucket.allow_request() # Token is available, request passes + True + >>> bucket.allow_request() # No token left, request is dropped + False + """ + with self.lock: + self._add_tokens() + if self.tokens >= 1: + self.tokens -= 1 + return True + return False + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + print("Allow 4 requests per minute, capacity of 4") + bucket = TokenBucketRateLimiter(4, 4, 60) + total_requests = 10 + delay_in_seconds = 10 + print("Simulate 1 request per 10 seconds...") + for i in range(total_requests): + result = "pass" if bucket.allow_request() else "dropped" + print( + f"Request {i+1}/{total_requests} \ + timeline: {i*delay_in_seconds} seconds = {result}" + ) + time.sleep(delay_in_seconds) From 4145400d7ddbfc47fd73008b2ccb9318ad451389 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 17 Sep 2026 10:32:42 +0200 Subject: [PATCH 2/6] Update comments for clarity in token_bucket.py --- other/token_bucket.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/other/token_bucket.py b/other/token_bucket.py index b188c73d2f90..425066d0f3b4 100644 --- a/other/token_bucket.py +++ b/other/token_bucket.py @@ -1,12 +1,15 @@ -# Implementation of Token Bucket Algorithm -# Token `rate` is added to the bucket every `frequency` in seconds. -# The bucket can hold tokens up to `capacity` (full). -# The bucket starts full. -# Each request consume one token. -# If a token arrives when the bucket is full, token is discarded. -# If a request arrives when bucket is empty, request will be discarded. -# If bucket has tokens available, requests will pass. -# https://en.wikipedia.org/wiki/Token_bucket +""" +Implementation of the Token Bucket Algorithm +Token `rate` is added to the bucket every `frequency` seconds. +The bucket can hold tokens up to `capacity` (full). +The bucket starts full. +Each request consumes one token. +If a token arrives when the bucket is full, the token is discarded. +If a request arrives when the bucket is empty, it is discarded. +If the bucket has tokens available, requests will pass. +https://en.wikipedia.org/wiki/Token_bucket +""" + import threading import time @@ -38,7 +41,7 @@ def _add_tokens(self) -> None: """ Refill tokens only when a full minute has passed. >>> bucket = TokenBucketRateLimiter(1, 4, 60) - >>> bucket.tokens # Initially has 4 token (rate) + >>> bucket.tokens # Initially has a rate of 4 tokens 4 >>> bucket._add_tokens() >>> bucket.tokens # Bucket already full From 3e41f38de1803b19373902eddc31f9a796e0eee6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 17 Sep 2026 08:32:55 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/token_bucket.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/token_bucket.py b/other/token_bucket.py index 425066d0f3b4..3bfa6ec79512 100644 --- a/other/token_bucket.py +++ b/other/token_bucket.py @@ -94,7 +94,7 @@ def allow_request(self) -> bool: for i in range(total_requests): result = "pass" if bucket.allow_request() else "dropped" print( - f"Request {i+1}/{total_requests} \ - timeline: {i*delay_in_seconds} seconds = {result}" + f"Request {i + 1}/{total_requests} \ + timeline: {i * delay_in_seconds} seconds = {result}" ) time.sleep(delay_in_seconds) From f2006c10e8696f41a91e763fd794418748983215 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 17 Sep 2026 10:34:51 +0200 Subject: [PATCH 4/6] Implement progress iterator for item processing Added a progress iterator function that displays progress in stderr while processing items from an iterable. --- other/cheap_progress | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 other/cheap_progress diff --git a/other/cheap_progress b/other/cheap_progress new file mode 100644 index 000000000000..8656cfcc3fb9 --- /dev/null +++ b/other/cheap_progress @@ -0,0 +1,54 @@ +from collections.abc import Iterable, Iterator +from sys import stderr + + +def progress[T](items: Iterable[T], desc: str = "", total: int = 0) -> Iterator[T]: + """ + A simple progress iterator that yields items from the given iterable while + displaying a progress indicator in place on a single line of stderr. The output is + not written to stdout, so the output of the program remains clean (see doctests). + + for item in progress(range(1_000), desc="Processing"): + process(item) + + Args: + items: The iterable of items to process. + desc: A description to display alongside the progress. Defaults to "". + total: The total number of items, defaults to 0. If 0, it will be inferred from + the iterable if possible. + + Yields: + Iterator[T]: The items from the iterable, one by one. + + >>> tuple(progress(range(5))) + (0, 1, 2, 3, 4) + >>> tuple(progress(range(5), desc="Processing", total=3)) + (0, 1, 2, 3, 4) + >>> tuple(progress(range(5), desc="Processing", total=10)) + (0, 1, 2, 3, 4) + >>> tuple(progress(range(5), desc="Processing", total=-5)) + (0, 1, 2, 3, 4) + >>> from string import printable + >>> tuple(progress(printable, desc="Printable")) # doctest: +ELLIPSIS + ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',... + """ + total = max(total, 0) + if not total and hasattr(items, "__len__"): + total = len(items) # type: ignore[invalid-argument-type] + + for i, item in enumerate(items, 1): + suffix = f"{i:,}/{total:,}" if total else f"{i:,}" + print(f"\r\033[K{desc}: {suffix}", end="", file=stderr, flush=True) + yield item + + print("\r\033[K", end="", file=stderr, flush=True) + + +if __name__ == "__main__": + import time + + print("start") + for _item in progress(range(1_000), desc="Processing"): + time.sleep(0.02) + + print("stop") From 08821b26cdafec22ac100cd46d3485fb0a06ed5c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 17 Sep 2026 08:35:04 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/cheap_progress | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/cheap_progress b/other/cheap_progress index 8656cfcc3fb9..62da9b59d1d9 100644 --- a/other/cheap_progress +++ b/other/cheap_progress @@ -5,7 +5,7 @@ from sys import stderr def progress[T](items: Iterable[T], desc: str = "", total: int = 0) -> Iterator[T]: """ A simple progress iterator that yields items from the given iterable while - displaying a progress indicator in place on a single line of stderr. The output is + displaying a progress indicator in place on a single line of stderr. The output is not written to stdout, so the output of the program remains clean (see doctests). for item in progress(range(1_000), desc="Processing"): @@ -19,7 +19,7 @@ def progress[T](items: Iterable[T], desc: str = "", total: int = 0) -> Iterator[ Yields: Iterator[T]: The items from the iterable, one by one. - + >>> tuple(progress(range(5))) (0, 1, 2, 3, 4) >>> tuple(progress(range(5), desc="Processing", total=3)) From 84fc9af5d8be475b9fe7d7aea225c7679052fdce Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 17 Sep 2026 10:37:15 +0200 Subject: [PATCH 6/6] Rename cheap_progress to cheap_progress.py --- other/{cheap_progress => cheap_progress.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename other/{cheap_progress => cheap_progress.py} (100%) diff --git a/other/cheap_progress b/other/cheap_progress.py similarity index 100% rename from other/cheap_progress rename to other/cheap_progress.py