Giter VIP home page Giter VIP logo

Comments (6)

algmyr avatar algmyr commented on August 17, 2024

Untested sketch of a rate limiter

import time
import itertools
def rate_limit(f, per_second, active = {}, counter = itertools.count()):
    async def wrapped(*args, **kwargs):
        req_id = next(counter)
        now = time.time()

        # Next valid slot is 1s after the `per_second`th last request
        next_valid = now
        if len(active) >= per_second:
            next_valid = max(next_valid, active[list(active.keys())[-per_second]])
        active[req_id] = next_valid

        # Delay as needed
        delay = next_valid - now
        if delay > 0:
            await asyncio.sleep(delay)

        result = await f(*args, **kwargs)
        del active[req_id]
        return result
    return wrapped

If it looks reasonable I can test it and make a PR.

from tle.

aryamanarora avatar aryamanarora commented on August 17, 2024

Unlike blues who must be disrespected

from tle.

algmyr avatar algmyr commented on August 17, 2024

Ok, revamped and seems to work fine.

from collections import deque
import asyncio
import time

def cf_ratelimit(f):
    per_second = 5
    last = deque([0]*per_second)
    async def wrapped(*args, **kwargs):
        now = time.time()

        # Next valid slot is 1s after the `per_second`th last request
        next_valid = max(now, 1 + last[0])
        last.append(next_valid)
        last.popleft()

        # Delay as needed
        delay = next_valid - now
        if delay > 0:
            await asyncio.sleep(delay)

        return await f(*args, **kwargs)
    return wrapped

@cf_ratelimit
async def f(s):
    print(s)

async def main():
    for i in range(50):
        await f(i)

asyncio.run(main())

from tle.

meooow25 avatar meooow25 commented on August 17, 2024

Looks good. We should also integrate the lock here, and implement API request retries if possible.

from tle.

krofna avatar krofna commented on August 17, 2024

@algmyr can you create a pull request?

from tle.

algmyr avatar algmyr commented on August 17, 2024

Closed by #82. Can be replaced later on if we want something more fancy.

from tle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.