Add timeout when connecting

This commit is contained in:
Dan 2018-06-17 18:44:45 +02:00
parent 0a6583a43c
commit 52354b93d0

View file

@ -25,6 +25,7 @@ log = logging.getLogger(__name__)
class Connection: class Connection:
TIMEOUT = 10
MAX_RETRIES = 3 MAX_RETRIES = 3
MODES = { MODES = {
@ -49,8 +50,8 @@ class Connection:
try: try:
log.info("Connecting...") log.info("Connecting...")
await self.protocol.connect(self.address) await asyncio.wait_for(self.protocol.connect(self.address), Connection.TIMEOUT)
except OSError: except (OSError, asyncio.TimeoutError):
self.protocol.close() self.protocol.close()
await asyncio.sleep(1) await asyncio.sleep(1)
else: else: