vutran1710/PyrateLimiter

Question: is is possible to programmatically consume several ratelimit usage?

Closed this issue · 4 comments

Hello,

I would like to be able to tell the limiter to consume all its available volume in bulk, I could loop on the ratelimiter like this:

        while True:
            try:
                with self._ratelimiter.ratelimit(identity, delay=False):
                    pass
            except BucketFullException as e:
                break

but that is sub optimal and really time consumming when I use a redis backend.

Would we have another way to achieve that?

How about you write a helper function like that and reuse it everywhere?

def consume_batch(limiter, batch):
    for identity in batch:
        while True:
            try:
                with self._ratelimiter.ratelimit(identity, delay=False):
                    pass
            except BucketFullException as e:
                break

Now you can easily import/export it and reuse across your app

I tried this helper function but it's really slow with a redis backend. I was looking for a more optimized solution.

I understand your point. I'll look at it soon.

The next major release will be packed with heavy performance boost (roughly ~1500 items/sec for usage with redis), therefore this probably wont be an issue any longer.