Specifying Algorithms#
The rate limiting algorithm is specified by the using parameter in the Throttled.
The supported algorithms are as follows:
Fixed Window :
RateLimiterType.FIXED_WINDOW.valueSliding Window:
RateLimiterType.SLIDING_WINDOW.valueToken Bucket:
RateLimiterType.TOKEN_BUCKET.valueLeaky Bucket:
RateLimiterType.LEAKING_BUCKET.valueGeneric Cell Rate Algorithm, GCRA:
RateLimiterType.GCRA.value
from throttled import RateLimiterType, Throttled
def main() -> None:
throttle = Throttled(
# 🌟Specifying a current limiting algorithm
using=RateLimiterType.FIXED_WINDOW.value,
# using=RateLimiterType.SLIDING_WINDOW.value,
# using=RateLimiterType.LEAKING_BUCKET.value,
# using=RateLimiterType.TOKEN_BUCKET.value,
# using=RateLimiterType.GCRA.value,
quota="1/m",
)
assert throttle.limit("key", 2).limited
if __name__ == "__main__":
main()
import asyncio
from throttled.asyncio import RateLimiterType, Throttled
async def main() -> None:
throttle = Throttled(
# 🌟Specifying a current limiting algorithm
using=RateLimiterType.FIXED_WINDOW.value,
# using=RateLimiterType.SLIDING_WINDOW.value,
# using=RateLimiterType.LEAKING_BUCKET.value,
# using=RateLimiterType.TOKEN_BUCKET.value,
# using=RateLimiterType.GCRA.value,
quota="1/m",
)
assert (await throttle.limit("key", 2)).limited
if __name__ == "__main__":
asyncio.run(main())