Skip to content

feat: prewarm the room connection in idle job processes - #8522

Closed
cnderrauber wants to merge 1 commit into
mainfrom
jie/prewarm-connection
Closed

cnderrauber wants to merge 1 commit into
mainfrom
jie/prewarm-connection

Conversation

@cnderrauber

Copy link
Copy Markdown
Contributor

Depends on

CI fails until then: against livekit 1.2.1, RoomOptions has no prewarmed_connection.

Summary

Idle job processes keep a TCP+TLS connection to the LiveKit server open, so when a job is dispatched, JobContext.connect() only does the WebSocket upgrade.

  • The worker picks the URL to warm:
    • Self-hosted: ws_url.
    • Cloud: /settings/regions, signed with the worker's API key, picking the entry for server_info.region. The first job_assign.url overrides it.
    • It re-runs on every registration.
  • ProcPool.set_prewarm_url sends PrewarmConnectionRequest (IPC msg 13) to idle processes.
  • Each idle process keeps one connection and replaces it once the server closes it, or when it's older than 45 s, checked every 10 s. That stays under the proxy read timeout on LiveKit Cloud: Traefik closes a connection that sends no request within 60 s.
  • The job gets the connection in JobContext.connect(), which passes it as RoomOptions.prewarmed_connection and closes it afterwards. If it's dead or for another host, connect dials fresh as before.
  • LIVEKIT_PREWARM_CONNECTION=0 disables it.

Why

Benchmark (dispatch → agent joined, network conditioner, wss): 631 → 400 ms p50 for connect, 1069 → 836 ms dispatch → joined.

Tests

  • tests/test_worker.py: region URL selection and Cloud URL detection.
  • tests/test_worker.py and tests/test_ipc.py pass against the python-sdks and rust-sdks branches above.

🤖 Generated with Claude Code

Idle job processes keep a TCP/TLS connection to the LiveKit server open and
hand it to JobContext.connect(), so a dispatched job only performs the
WebSocket upgrade.

- The worker picks the URL to warm: ws_url when self-hosted; on LiveKit Cloud,
  the /settings/regions entry for the registered region, signed with the
  worker's API key, re-run on every registration and overridden by the first
  job_assign.url.
- ProcPool sends PrewarmConnectionRequest (IPC msg 13) to idle processes; each
  keeps one connection, replacing it when older than 60s or closed by the server.
- LIVEKIT_PREWARM_CONNECTION=0 disables it.

Requires a livekit-rtc release with PrewarmedConnection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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