Change timeout for radio connections (#365)
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Tue, 14 Jun 2022 17:25:16 +0000 (19:25 +0200)
committerGitHub <noreply@github.com>
Tue, 14 Jun 2022 17:25:16 +0000 (19:25 +0200)
change timeout for radio connections

music_assistant/controllers/streams.py
music_assistant/helpers/audio.py

index 817246f89e242dce64d3475709dafd14db5fd0c0..9e0e1544ddf86980e41330b45facc01f9d2d640e 100644 (file)
@@ -369,7 +369,7 @@ class QueueStream:
 
             ffmpeg_proc.attach_task(writer())
 
-            # wait max 5 seconds for all client(s) to connect
+            # wait max 10 seconds for all client(s) to connect
             try:
                 await asyncio.wait_for(self.all_clients_connected.wait(), 10)
             except asyncio.exceptions.TimeoutError:
index c449dda576e37b8de774f6c6f59c33c7650189cf..e70f7c8e712784901fa3bcd5cafcad8dd999e050 100644 (file)
@@ -11,6 +11,7 @@ from time import time
 from typing import TYPE_CHECKING, AsyncGenerator, List, Optional, Tuple
 
 import aiofiles
+from aiohttp import ClientError, ClientTimeout
 
 from music_assistant.helpers.process import AsyncProcess, check_output
 from music_assistant.helpers.util import create_tempfile
@@ -444,11 +445,15 @@ async def get_radio_stream(
 ) -> AsyncGenerator[bytes, None]:
     """Get radio audio stream from HTTP, including metadata retrieval."""
     headers = {"Icy-MetaData": "1"}
+    timeout = ClientTimeout(total=0, connect=10, sock_read=10)
+    reconnects = 0
     while True:
         # in loop to reconnect on connection failure
         try:
             LOGGER.debug("radio stream (re)connecting to: %s", url)
-            async with mass.http_session.get(url, headers=headers, timeout=60) as resp:
+            async with mass.http_session.get(
+                url, headers=headers, timeout=timeout
+            ) as resp:
                 headers = resp.headers
                 meta_int = int(headers.get("icy-metaint", "0"))
                 # stream with ICY Metadata
@@ -476,8 +481,11 @@ async def get_radio_stream(
                 else:
                     async for chunk in resp.content.iter_any():
                         yield chunk
-        except asyncio.exceptions.TimeoutError:
-            pass
+        except (asyncio.exceptions.TimeoutError, ClientError) as err:
+            # reconnect on http error (max 5 times)
+            if reconnects >= 5:
+                raise err
+            reconnects += 1
 
 
 async def get_http_stream(