Time out when creating the ws connections

This commit is contained in:
Joscha 2019-04-14 22:25:42 +00:00
parent e53ce42e99
commit d9f25a04fb
2 changed files with 12 additions and 3 deletions

View file

@ -2,6 +2,8 @@
## Next version
- add timeout for creating ws connections
## 1.1.2 (2019-04-14)
- fix room authentication

View file

@ -82,6 +82,9 @@ class Connection:
"part-event" and "ping".
"""
# Timeout for waiting for the ws connection to be established
CONNECT_TIMEOUT = 10 # seconds
# Maximum duration between euphoria's ping messages. Euphoria usually sends
# ping messages every 20 to 30 seconds.
PING_TIMEOUT = 40 # seconds
@ -183,8 +186,12 @@ class Connection:
try:
logger.debug(f"Creating ws connection to {self._url!r}")
ws = await websockets.connect(self._url,
extra_headers=self._cookie_jar.get_cookies_as_headers())
ws = await asyncio.wait_for(
websockets.connect(self._url,
extra_headers=self._cookie_jar.get_cookies_as_headers()),
self.CONNECT_TIMEOUT
)
logger.debug(f"Established ws connection to {self._url!r}")
self._ws = ws
self._awaiting_replies = {}
@ -200,7 +207,7 @@ class Connection:
return True
except (websockets.InvalidHandshake, websockets.InvalidStatusCode,
socket.gaierror):
socket.gaierror, asyncio.TimeoutError):
logger.debug("Connection failed")
return False