Skip to content

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.

2 participants