From 5a06fa5f3e28d2b7191722e1a84c541560008aea Mon Sep 17 00:00:00 2001 From: Lilly Rose Berner Date: Mon, 25 Apr 2022 19:00:14 +0200 Subject: [PATCH] Give large guilds more time to chunk before timing out --- discord/state.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/discord/state.py b/discord/state.py index 3069075e8..6db292c31 100644 --- a/discord/state.py +++ b/discord/state.py @@ -528,8 +528,10 @@ class ConnectionState: self.dispatch('guild_join', guild) for guild, future in states: + timeout = self._chunk_timeout(guild) + try: - await asyncio.wait_for(future, timeout=5.0) + await asyncio.wait_for(future, timeout=timeout) except asyncio.TimeoutError: _log.warning('Shard ID %s timed out waiting for chunks for guild_id %s.', guild.shard_id, guild.id) @@ -1096,9 +1098,14 @@ class ConnectionState: return await request.wait() return request.get_future() + def _chunk_timeout(self, guild: Guild) -> float: + return max(5.0, (guild.member_count or 0) / 10000) + async def _chunk_and_dispatch(self, guild, unavailable): + timeout = self._chunk_timeout(guild) + try: - await asyncio.wait_for(self.chunk_guild(guild), timeout=60.0) + await asyncio.wait_for(self.chunk_guild(guild), timeout=timeout) except asyncio.TimeoutError: _log.info('Somehow timed out waiting for chunks.') @@ -1582,8 +1589,10 @@ class AutoShardedConnectionState(ConnectionState): self.dispatch('guild_join', guild) for guild, future in states: + timeout = self._chunk_timeout(guild) + try: - await asyncio.wait_for(future, timeout=5.0) + await asyncio.wait_for(future, timeout=timeout) except asyncio.TimeoutError: _log.warning('Shard ID %s timed out waiting for chunks for guild_id %s.', guild.shard_id, guild.id)