Browse Source

Timeout when doing initial connection.

pull/546/head
Rapptz 8 years ago
parent
commit
3cfebc4605
  1. 13
      discord/client.py
  2. 2
      discord/gateway.py
  3. 26
      discord/shard.py

13
discord/client.py

@ -342,17 +342,18 @@ class Client:
@asyncio.coroutine @asyncio.coroutine
def _connect(self): def _connect(self):
self.ws = yield from DiscordWebSocket.from_client(self, shard_id=self.shard_id) coro = DiscordWebSocket.from_client(self, shard_id=self.shard_id)
self.ws = yield from asyncio.wait_for(coro, timeout=180.0, loop=self.loop)
while True: while True:
try: try:
yield from self.ws.poll_event() yield from self.ws.poll_event()
except ResumeWebSocket as e: except ResumeWebSocket as e:
log.info('Got a request to RESUME the websocket.') log.info('Got a request to RESUME the websocket.')
self.ws = yield from DiscordWebSocket.from_client(self, shard_id=self.shard_id, coro = DiscordWebSocket.from_client(self, shard_id=self.shard_id,
session=self.ws.session_id, session=self.ws.session_id,
sequence=self.ws.sequence, sequence=self.ws.sequence,
resume=True) resume=True)
self.ws = yield from asyncio.wait_for(coro, timeout=180.0, loop=self.loop)
@asyncio.coroutine @asyncio.coroutine
def connect(self, *, reconnect=True): def connect(self, *, reconnect=True):

2
discord/gateway.py

@ -220,7 +220,7 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
except websockets.exceptions.ConnectionClosed: except websockets.exceptions.ConnectionClosed:
# ws got closed so let's just do a regular IDENTIFY connect. # ws got closed so let's just do a regular IDENTIFY connect.
log.info('RESUME failed (the websocket decided to close) for Shard ID %s. Retrying.', shard_id) log.info('RESUME failed (the websocket decided to close) for Shard ID %s. Retrying.', shard_id)
return (yield from cls.from_client(client)) return (yield from cls.from_client(client, shard_id=shard_id))
else: else:
return ws return ws

26
discord/shard.py

@ -56,10 +56,12 @@ class Shard:
yield from self.ws.poll_event() yield from self.ws.poll_event()
except ResumeWebSocket as e: except ResumeWebSocket as e:
log.info('Got a request to RESUME the websocket at Shard ID %s.', self.id) log.info('Got a request to RESUME the websocket at Shard ID %s.', self.id)
self.ws = yield from DiscordWebSocket.from_client(self._client, resume=True, coro = DiscordWebSocket.from_client(self._client, resume=True,
shard_id=self.id, shard_id=self.id,
session=self.ws.session_id, session=self.ws.session_id,
sequence=self.ws.sequence) sequence=self.ws.sequence)
self.ws = yield from asyncio.wait_for(coro, timeout=180.0, loop=self.loop)
def get_future(self): def get_future(self):
if self._current.done(): if self._current.done():
self._current = compat.create_task(self.poll(), loop=self.loop) self._current = compat.create_task(self.poll(), loop=self.loop)
@ -180,9 +182,10 @@ class AutoShardedClient(Client):
@asyncio.coroutine @asyncio.coroutine
def launch_shard(self, gateway, shard_id): def launch_shard(self, gateway, shard_id):
try: try:
ws = yield from websockets.connect(gateway, loop=self.loop, klass=DiscordWebSocket) ws = yield from asyncio.wait_for(websockets.connect(gateway, loop=self.loop, klass=DiscordWebSocket),
loop=self.loop, timeout=180.0)
except Exception as e: except Exception as e:
log.info('Failed to connect for shard_id: %s. Retrying...' % shard_id) log.info('Failed to connect for shard_id: %s. Retrying...', shard_id)
yield from asyncio.sleep(5.0, loop=self.loop) yield from asyncio.sleep(5.0, loop=self.loop)
return (yield from self.launch_shard(gateway, shard_id)) return (yield from self.launch_shard(gateway, shard_id))
@ -193,9 +196,14 @@ class AutoShardedClient(Client):
ws.shard_id = shard_id ws.shard_id = shard_id
ws.shard_count = self.shard_count ws.shard_count = self.shard_count
# OP HELLO try:
yield from ws.poll_event() # OP HELLO
yield from ws.identify() yield from asyncio.wait_for(ws.poll_event(), loop=self.loop, timeout=180.0)
yield from asyncio.wait_for(ws.identify(), loop=self.loop, timeout=180.0)
except asyncio.TimeoutError:
log.info('Timed out when connecting for shard_id: %s. Retrying...', shard_id)
yield from asyncio.sleep(5.0, loop=self.loop)
return (yield from self.launch_shard(gateway, shard_id))
# keep reading the shard while others connect # keep reading the shard while others connect
self.shards[shard_id] = ret = Shard(ws, self) self.shards[shard_id] = ret = Shard(ws, self)

Loading…
Cancel
Save