Add a lock on nest stream URL creation to avoid multiple in flight at once (#63212)
Add a lock to avoid multiple calls to create stream URLs when requests race in parallel to open streams. This is to avoid # of API calls on the nest server which can be rate limited, and to avoid any possibility of having too many streams per camera outstanding at once.
This commit is contained in:
parent
5c8e802cbf
commit
cb76a30233
1 changed files with 10 additions and 7 deletions
|
@ -1,6 +1,7 @@
|
|||
"""Support for Google Nest SDM Cameras."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Callable
|
||||
import datetime
|
||||
import logging
|
||||
|
@ -74,6 +75,7 @@ class NestCamera(Camera):
|
|||
self._device = device
|
||||
self._device_info = NestDeviceInfo(device)
|
||||
self._stream: RtspStream | None = None
|
||||
self._create_stream_url_lock = asyncio.Lock()
|
||||
self._stream_refresh_unsub: Callable[[], None] | None = None
|
||||
# Cache of most recent event image
|
||||
self._event_id: str | None = None
|
||||
|
@ -140,13 +142,14 @@ class NestCamera(Camera):
|
|||
trait = self._device.traits[CameraLiveStreamTrait.NAME]
|
||||
if StreamingProtocol.RTSP not in trait.supported_protocols:
|
||||
return None
|
||||
if not self._stream:
|
||||
_LOGGER.debug("Fetching stream url")
|
||||
try:
|
||||
self._stream = await trait.generate_rtsp_stream()
|
||||
except ApiException as err:
|
||||
raise HomeAssistantError(f"Nest API error: {err}") from err
|
||||
self._schedule_stream_refresh()
|
||||
async with self._create_stream_url_lock:
|
||||
if not self._stream:
|
||||
_LOGGER.debug("Fetching stream url")
|
||||
try:
|
||||
self._stream = await trait.generate_rtsp_stream()
|
||||
except ApiException as err:
|
||||
raise HomeAssistantError(f"Nest API error: {err}") from err
|
||||
self._schedule_stream_refresh()
|
||||
assert self._stream
|
||||
if self._stream.expires_at < utcnow():
|
||||
_LOGGER.warning("Stream already expired")
|
||||
|
|
Loading…
Add table
Reference in a new issue