Skip to content

Limiting the number of concurrent Jobs per Worker reliably #4884

@dhansmair

Description

@dhansmair

Bug Description

We want to restrict our worker (AgentServer) to accept only one concurrent job at a time.

According to the LiveKit documentation (https://docs.livekit.io/agents/server/options/#agent-server-load), this can be achieved using load_threshold and load_fnc. See reproduction steps below.

However, we were able to reproduce situations where the AgentServer would still accept two jobs.

Problem

The problem lies in the interval where the server._worker_load variable is updated:

interval = utils.aio.interval(UPDATE_LOAD_INTERVAL)

The interval is set to 0.5s. If two job requests arrive within this interval, the _worker_load will be 0 for both and the server will accept both requests.

In the past few days I have tried many workarounds using multiprocessing Locks, Values, and other mechanisms, however I was unable to find a reliable solution to prevent this edge case behavior.

Questions:

  • Is there an alternative to guarantee that only one job can run at a time?
  • Could this functionality be provided natively?

Many thanks for your help!
Best, David

Expected Behavior

We would expect a solution that guarantees that only one job can be accepted at a time.

Reproduction Steps

# limit number of concurrent jobs using `load_threshold` and `load_fnc`:
from livekit.agents import AgentServer

server = AgentServer(
    load_threshold=1.0,
)

def compute_load(server: AgentServer) -> float:
    return float(len(server.active_jobs))

server.load_fnc=compute_load


The AgentServer can still accept two jobs**:
1. open two separate browser tabs
2. start two connection requests nearly exactly at the same time
3. => AgentServer will accept both jobs

Operating System

Windows, Linux

Models Used

No response

Package Versions

livekit==1.1.2
livekit-agents>=1.4.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions