alexdelorenzo / limiter

⏲️ Easy rate limiting for Python using a token bucket algorithm, with async and thread-safe decorators and context managers
https://alexdelorenzo.dev/
GNU Lesser General Public License v3.0
40 stars 3 forks source link

Jitter usage? #5

Open hemna opened 3 weeks ago

hemna commented 3 weeks ago

Hello, I'm trying to use jitter to add a randomness to the refresh rate a bit. I want to add a random amount of time between 1 and 20 seconds for the refresh rate but it doesn't seem to do anything.

from limiter import Limiter
from rich.console import Console
from datetime import datetime

REFRESH_RATE: int = 1
BURST_RATE: int = 1
MSG_BUCKET: str = 'messages'

limiter: Limiter = Limiter(rate=REFRESH_RATE, capacity=BURST_RATE)
limit_msgs: Limiter = limiter(bucket=MSG_BUCKET)

@limit_msgs(jitter=range(1000,20000))
def send_message(console, message):
    msg = f"{datetime.now()} {message}"
    console.print(f"Sending {msg}")

def main():
    console = Console()
    x = 1
    while True:
        msg = f"MSG {x}"
        send_message(console, msg)
        x += 1

if __name__ == "__main__":
    main()
hemna commented 3 weeks ago
Sending 2024-10-01 09:43:06.055223 MSG 220
Sending 2024-10-01 09:43:07.060343 MSG 221
Sending 2024-10-01 09:43:08.065478 MSG 222
Sending 2024-10-01 09:43:09.070621 MSG 223
Sending 2024-10-01 09:43:10.073807 MSG 224
Sending 2024-10-01 09:43:11.076373 MSG 225
Sending 2024-10-01 09:43:12.079037 MSG 226
Sending 2024-10-01 09:43:13.082216 MSG 227
Sending 2024-10-01 09:43:14.087347 MSG 228
Sending 2024-10-01 09:43:15.090792 MSG 229
Sending 2024-10-01 09:43:16.094575 MSG 230
Sending 2024-10-01 09:43:17.096106 MSG 231
Sending 2024-10-01 09:43:18.101246 MSG 232
Sending 2024-10-01 09:43:19.104858 MSG 233
Sending 2024-10-01 09:43:20.105910 MSG 234
Sending 2024-10-01 09:43:21.110366 MSG 235
Sending 2024-10-01 09:43:22.112382 MSG 236
Sending 2024-10-01 09:43:23.114554 MSG 237
Sending 2024-10-01 09:43:24.118568 MSG 238
Sending 2024-10-01 09:43:25.123707 MSG 239
Sending 2024-10-01 09:43:26.123890 MSG 240
Sending 2024-10-01 09:43:27.125231 MSG 241
Sending 2024-10-01 09:43:28.126932 MSG 242
Sending 2024-10-01 09:43:29.128665 MSG 243
Sending 2024-10-01 09:43:30.129436 MSG 244
Sending 2024-10-01 09:43:31.134592 MSG 245
Sending 2024-10-01 09:43:32.139734 MSG 246
Sending 2024-10-01 09:43:33.143030 MSG 247
Sending 2024-10-01 09:43:34.144749 MSG 248
Sending 2024-10-01 09:43:35.149357 MSG 249
Sending 2024-10-01 09:43:36.154467 MSG 250
Sending 2024-10-01 09:43:37.158837 MSG 251
Sending 2024-10-01 09:43:38.160435 MSG 252
Sending 2024-10-01 09:43:39.162116 MSG 253