Skip to content

Commit

Permalink
Fix aiohttp timeout issue
Browse files Browse the repository at this point in the history
This addresses the issue described in #190 (comment)

Signed-off-by: rany <[email protected]>
  • Loading branch information
rany2 committed May 17, 2024
1 parent 6d9299a commit 547d91d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/edge_tts/communicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ def __init__(
volume: str = "+0%",
pitch: str = "+0Hz",
proxy: Optional[str] = None,
receive_timeout: int = 5,
connect_timeout: int = 10,
receive_timeout: int = 60,
):
"""
Initializes the Communicate class.
Expand Down Expand Up @@ -306,9 +307,14 @@ def __init__(
raise TypeError("proxy must be str")
self.proxy: Optional[str] = proxy

if not isinstance(receive_timeout, int):
raise TypeError("receive_timeout must be int")
self.receive_timeout: int = receive_timeout
if not isinstance(connect_timeout, int) or not isinstance(receive_timeout, int):
raise TypeError("connect_timeout and receive_timeout must be int")
self.session_timeout = aiohttp.ClientTimeout(
total=None,
connect=None,
sock_connect=connect_timeout,
sock_read=receive_timeout,
)

async def stream(self) -> AsyncGenerator[Dict[str, Any], None]:
"""Streams audio and metadata from the service."""
Expand Down Expand Up @@ -390,11 +396,11 @@ def parse_metadata() -> Dict[str, Any]:
ssl_ctx = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession(
trust_env=True,
timeout=self.session_timeout,
) as session, session.ws_connect(
f"{WSS_URL}&ConnectionId={connect_id()}",
compress=15,
proxy=self.proxy,
receive_timeout=self.receive_timeout,
headers={
"Pragma": "no-cache",
"Cache-Control": "no-cache",
Expand Down

0 comments on commit 547d91d

Please sign in to comment.