own snapshot timeout

This commit is contained in:
Maxim Devaev 2024-03-04 08:15:30 +02:00
parent 4690e33088
commit 12c4a5ec06
2 changed files with 42 additions and 35 deletions

View File

@ -470,6 +470,7 @@ def _get_config_scheme() -> dict:
"unix": Option("/run/kvmd/ustreamer.sock", type=valid_abs_path, unpack_as="unix_path"), "unix": Option("/run/kvmd/ustreamer.sock", type=valid_abs_path, unpack_as="unix_path"),
"timeout": Option(2.0, type=valid_float_f01), "timeout": Option(2.0, type=valid_float_f01),
"snapshot_timeout": Option(1.0, type=valid_float_f01), # error_delay * 3 + 1
"process_name_prefix": Option("kvmd/streamer"), "process_name_prefix": Option("kvmd/streamer"),

View File

@ -179,6 +179,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
unix_path: str, unix_path: str,
timeout: float, timeout: float,
snapshot_timeout: float,
process_name_prefix: str, process_name_prefix: str,
@ -203,6 +204,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
self.__unix_path = unix_path self.__unix_path = unix_path
self.__timeout = timeout self.__timeout = timeout
self.__snapshot_timeout = snapshot_timeout
self.__process_name_prefix = process_name_prefix self.__process_name_prefix = process_name_prefix
@ -350,41 +352,45 @@ class Streamer: # pylint: disable=too-many-instance-attributes
async def take_snapshot(self, save: bool, load: bool, allow_offline: bool) -> (StreamerSnapshot | None): async def take_snapshot(self, save: bool, load: bool, allow_offline: bool) -> (StreamerSnapshot | None):
if load: if load:
return self.__snapshot return self.__snapshot
else: logger = get_logger()
logger = get_logger() session = self.__ensure_http_session()
session = self.__ensure_http_session() try:
try: async with session.get(
async with session.get(self.__make_url("snapshot")) as response: self.__make_url("snapshot"),
htclient.raise_not_200(response) timeout=self.__snapshot_timeout,
online = (response.headers["X-UStreamer-Online"] == "true") ) as response:
if online or allow_offline:
snapshot = StreamerSnapshot( htclient.raise_not_200(response)
online=online, online = (response.headers["X-UStreamer-Online"] == "true")
width=int(response.headers["X-UStreamer-Width"]), if online or allow_offline:
height=int(response.headers["X-UStreamer-Height"]), snapshot = StreamerSnapshot(
headers=tuple( online=online,
(key, value) width=int(response.headers["X-UStreamer-Width"]),
for (key, value) in tools.sorted_kvs(dict(response.headers)) height=int(response.headers["X-UStreamer-Height"]),
if key.lower().startswith("x-ustreamer-") or key.lower() in [ headers=tuple(
"x-timestamp", (key, value)
"access-control-allow-origin", for (key, value) in tools.sorted_kvs(dict(response.headers))
"cache-control", if key.lower().startswith("x-ustreamer-") or key.lower() in [
"pragma", "x-timestamp",
"expires", "access-control-allow-origin",
] "cache-control",
), "pragma",
data=bytes(await response.read()), "expires",
) ]
if save: ),
self.__snapshot = snapshot data=bytes(await response.read()),
self.__notifier.notify() )
return snapshot if save:
logger.error("Stream is offline, no signal or so") self.__snapshot = snapshot
except (aiohttp.ClientConnectionError, aiohttp.ServerConnectionError) as err: self.__notifier.notify()
logger.error("Can't connect to streamer: %s", tools.efmt(err)) return snapshot
except Exception: logger.error("Stream is offline, no signal or so")
logger.exception("Invalid streamer response from /snapshot")
return None except (aiohttp.ClientConnectionError, aiohttp.ServerConnectionError) as err:
logger.error("Can't connect to streamer: %s", tools.efmt(err))
except Exception:
logger.exception("Invalid streamer response from /snapshot")
return None
def remove_snapshot(self) -> None: def remove_snapshot(self) -> None:
self.__snapshot = None self.__snapshot = None