vutran1710 / PyrateLimiter

⚔️Python Rate-Limiter using Leaky-Bucket Algorithm Family
https://pyratelimiter.readthedocs.io
MIT License
334 stars 36 forks source link

Bug? causing rate limit to not be applied as expected #125

Closed Adam-D-Lewis closed 1 year ago

Adam-D-Lewis commented 1 year ago

I would expect the following code to add 10 items to the indices_added list, but only 6 are added. This seems like a bug.

import time
from pyrate_limiter import Duration, Rate, InMemoryBucket, Limiter, BucketFullException

rates = [Rate(1, Duration.SECOND*1/10)]

basic_bucket = InMemoryBucket(rates)
limiter = Limiter(basic_bucket)

indices_added = []
for index in range(10):
    try:
        time.sleep(0.1)
        limiter.try_acquire(index)
        indices_added.append(index)
    except BucketFullException:
        pass

print(len(indices_added))
# 6
print(indices_added)
# [0, 2, 4, 5, 7, 9]
vutran1710 commented 1 year ago

when interval is too small, some tolerance must be considered because of computation and stuffs.

can u try sleep(0.105) or 0.11?