Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove visit in-memory cache, but add rate limiting #6

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vladsavelyev
Copy link
Member

@vladsavelyev vladsavelyev commented Mar 29, 2024

Make the API more robust to restarts and to memory caps.

The previous solution - which consists of an in-memory cache that regularly persisted into a CSV file which is regularly summarized into a database - was designed to avoid usage spikes hanging the server.

Here I'm suggesting a simpler solution: we write every visit directly to the DB, but to avoid the spike problem, we drop repeated requests coming from the same IP. Specifically, we cap to 1000 in 1 second from the same IP.

Of course, there is a chance that users might be running very large workflows behind the same IP (e.g. on the same machine), and multiple legit MultiQC runs might start on the same second. I'm not sure how probably that is. Haven't happened so far (the max number of requests from the same IP per minute was 22). But maybe having some separately running cache layer might work better.

@app.get("/version")
async def version(
    background_tasks: BackgroundTasks,
    ...
):
    background_tasks.add_task(
        _log_visit,
        ...
    )
    return models.VersionResponse(latest_release=app.latest_release)

REQUESTS = 1000  # max requests allowed within a window
WINDOW = 1  # seconds

def _log_visit_endpoint(
    background_tasks: BackgroundTasks,
    request: Request,
    ...
):
    current_time = float(time.time())
    client_ip = request.client.host
    if client_ip in requests_by_ip:
        # check the call rate and skip request if exceeded
        ...
        return

    with Session(engine) as session:
        session.add(
            VisitStats(...)
        )
        session.commit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant