Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/api-request-timeout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-api": patch
---

Apply a timeout to every API request. Requests previously ignored the session timeout and waited indefinitely, so a host that accepted the connection but never answered blocked the call and prevented region failover. `LiveKitAPI(timeout=...)` now also applies when a session is supplied by the caller, giving API calls a deadline without changing the timeout of other traffic sharing that session.
3 changes: 2 additions & 1 deletion livekit-api/livekit/api/_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def __init__(
api_key: str,
api_secret: str,
failover: bool = True,
timeout: aiohttp.ClientTimeout | None = None,
):
self._client = TwirpClient(session, host, "livekit", failover=failover)
self._client = TwirpClient(session, host, "livekit", failover=failover, timeout=timeout)
self.api_key = api_key
self.api_secret = api_secret
# A pre-signed token set by LiveKitAPI for token auth; sent verbatim,
Expand Down
3 changes: 2 additions & 1 deletion livekit-api/livekit/api/agent_dispatch_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def __init__(
api_key: str,
api_secret: str,
failover: bool = True,
timeout: Optional[aiohttp.ClientTimeout] = None,
):
super().__init__(session, url, api_key, api_secret, failover=failover)
super().__init__(session, url, api_key, api_secret, failover=failover, timeout=timeout)

async def create_dispatch(self, req: CreateAgentDispatchRequest) -> AgentDispatch:
"""Create an explicit dispatch for an agent to join a room.
Expand Down
3 changes: 2 additions & 1 deletion livekit-api/livekit/api/connector_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ def __init__(
api_key: str,
api_secret: str,
failover: bool = True,
timeout: Optional[aiohttp.ClientTimeout] = None,
):
super().__init__(session, url, api_key, api_secret, failover=failover)
super().__init__(session, url, api_key, api_secret, failover=failover, timeout=timeout)

async def dial_whatsapp_call(
self, request: DialWhatsAppCallRequest
Expand Down
4 changes: 3 additions & 1 deletion livekit-api/livekit/api/egress_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from ._service import Service
from .access_token import VideoGrants
from typing import Optional

SVC = "Egress"
"""@private"""
Expand All @@ -41,8 +42,9 @@ def __init__(
api_key: str,
api_secret: str,
failover: bool = True,
timeout: Optional[aiohttp.ClientTimeout] = None,
):
super().__init__(session, url, api_key, api_secret, failover=failover)
super().__init__(session, url, api_key, api_secret, failover=failover, timeout=timeout)

async def start_room_composite_egress(self, start: RoomCompositeEgressRequest) -> EgressInfo:
"""Starts a composite recording of a room."""
Expand Down
4 changes: 3 additions & 1 deletion livekit-api/livekit/api/ingress_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)
from ._service import Service
from .access_token import VideoGrants
from typing import Optional

SVC = "Ingress"
"""@private"""
Expand All @@ -35,8 +36,9 @@ def __init__(
api_key: str,
api_secret: str,
failover: bool = True,
timeout: Optional[aiohttp.ClientTimeout] = None,
):
super().__init__(session, url, api_key, api_secret, failover=failover)
super().__init__(session, url, api_key, api_secret, failover=failover, timeout=timeout)

async def create_ingress(self, create: CreateIngressRequest) -> IngressInfo:
return await self._client.request(
Expand Down
29 changes: 21 additions & 8 deletions livekit-api/livekit/api/livekit_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def __init__(
token: Pre-signed access token (read from `LIVEKIT_TOKEN` environment variable if not provided)
token_provider: Async callable that returns a token for the grants of each call,
in place of signing one with api_key and api_secret
timeout: Request timeout (default: 10 seconds)
timeout: Per-request timeout, applied to every API call. Defaults to
10 seconds when this instance creates its own session. When a
`session` is supplied, it defaults to that session's timeout;
pass this to give API calls a deadline of their own without
changing the timeout of other traffic sharing the session.
session: aiohttp.ClientSession instance to use for requests, if not provided, a new one will be created
"""
url = url or os.getenv("LIVEKIT_URL")
Expand Down Expand Up @@ -84,12 +88,17 @@ def __init__(
# the token, injected into each service below.
key = api_key or ""
secret = api_secret or ""
self._room = RoomService(self._session, url, key, secret, failover)
self._ingress = IngressService(self._session, url, key, secret, failover)
self._egress = EgressService(self._session, url, key, secret, failover)
self._sip = SipService(self._session, url, key, secret, failover)
self._agent_dispatch = AgentDispatchService(self._session, url, key, secret, failover)
self._connector = ConnectorService(self._session, url, key, secret, failover)
# Passed to every service so that `timeout` applies to API calls even
# when the session belongs to the caller, whose timeout is typically
# tuned for other traffic sharing that session.
self._room = RoomService(self._session, url, key, secret, failover, timeout)
self._ingress = IngressService(self._session, url, key, secret, failover, timeout)
self._egress = EgressService(self._session, url, key, secret, failover, timeout)
self._sip = SipService(self._session, url, key, secret, failover, timeout)
self._agent_dispatch = AgentDispatchService(
self._session, url, key, secret, failover, timeout
)
self._connector = ConnectorService(self._session, url, key, secret, failover, timeout)

if token or token_provider:
for svc in (
Expand Down Expand Up @@ -122,7 +131,11 @@ def with_token(
Args:
token: Pre-signed access token
url: LiveKit server URL (read from `LIVEKIT_URL` if not provided)
timeout: Request timeout (default: 10 seconds)
timeout: Per-request timeout, applied to every API call. Defaults to
10 seconds when this instance creates its own session. When a
`session` is supplied, it defaults to that session's timeout;
pass this to give API calls a deadline of their own without
changing the timeout of other traffic sharing the session.
session: aiohttp.ClientSession to use; a new one is created if omitted
"""
return cls(url, token=token, timeout=timeout, session=session, failover=failover)
Expand Down
4 changes: 3 additions & 1 deletion livekit-api/livekit/api/room_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from livekit.protocol.models import Room, ParticipantInfo
from ._service import Service
from .access_token import VideoGrants
from typing import Optional

SVC = "RoomService"
"""@private"""
Expand All @@ -52,8 +53,9 @@ def __init__(
api_key: str,
api_secret: str,
failover: bool = True,
timeout: Optional[aiohttp.ClientTimeout] = None,
):
super().__init__(session, url, api_key, api_secret, failover=failover)
super().__init__(session, url, api_key, api_secret, failover=failover, timeout=timeout)

async def create_room(
self,
Expand Down
3 changes: 2 additions & 1 deletion livekit-api/livekit/api/sip_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def __init__(
api_key: str,
api_secret: str,
failover: bool = True,
timeout: Optional[aiohttp.ClientTimeout] = None,
):
super().__init__(session, url, api_key, api_secret, failover=failover)
super().__init__(session, url, api_key, api_secret, failover=failover, timeout=timeout)

async def create_inbound_trunk(
self, create: CreateSIPInboundTrunkRequest
Expand Down
24 changes: 18 additions & 6 deletions livekit-api/livekit/api/twirp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def __init__(
prefix: str = DEFAULT_PREFIX,
failover: bool = True,
*,
timeout: Optional[aiohttp.ClientTimeout] = None,
_failover_force: bool = False,
_failover_backoff: float = FAILOVER_BACKOFF_BASE,
) -> None:
Expand All @@ -208,6 +209,10 @@ def __init__(
self.pkg = pkg
self.prefix = prefix
self._session = session
# Applies to every request this client issues, unless a call passes its
# own. Lets a caller who supplies a shared session still give the API a
# deadline of its own, without touching the session's.
self._timeout = timeout
# _failover_force / _failover_backoff are internal test-only knobs.
self._failover = failover
self._failover_force = _failover_force
Expand Down Expand Up @@ -237,11 +242,18 @@ async def request(
headers[REQUEST_ID_HEADER] = str(uuid.uuid4())
serialized_data = data.SerializeToString()

# The effective per-attempt timeout is the per-call override, or the
# session default; used to gate failover for short requests.
effective_timeout = timeout.total if timeout else None
if effective_timeout is None and self._session.timeout is not None:
effective_timeout = self._session.timeout.total
# The per-attempt timeout is the per-call override, then this client's
# default, then the session default. aiohttp treats an explicit
# ``timeout=None`` as "no timeout at all" rather than "use the session
# default" — only a *missing* argument selects the session timeout — so
# resolve the fallback here and pass the result explicitly.
request_timeout = timeout
if request_timeout is None:
request_timeout = self._timeout
if request_timeout is None:
request_timeout = self._session.timeout
# Used to gate failover for short requests.
effective_timeout = request_timeout.total if request_timeout is not None else None
host = urlparse(self._origin).hostname
max_attempts = failover_attempts(
self._failover, host, self._failover_force, effective_timeout
Expand All @@ -263,7 +275,7 @@ async def request(
error_data: Dict = {}
try:
async with self._session.post(
url, headers=headers, data=serialized_data, timeout=timeout
url, headers=headers, data=serialized_data, timeout=request_timeout
) as resp:
if resp.status == 200:
return response_class.FromString(await resp.read())
Expand Down
Loading
Loading