Skip to content

Instantly share code, notes, and snippets.

@link2xt
link2xt / ewma.py
Created September 3, 2024 21:52
Exponential rate limiter with a single variable
#!/usr/bin/env python3
"""Implementation of exponential rate limiter from <https://dotat.at/@/2024-09-02-ewma.html>,
but using a single variable `next-time` to store the state."""
import time
import math
class RateLimiter:
def __init__(self, window, limit):
self.next_time = 0.0