Skip to content

Commit

Permalink
Merge pull request #134 from FasterSpeeding/task/chunking-tasks
Browse files Browse the repository at this point in the history
avoid the shard ratelimits delaying event dispatch
  • Loading branch information
Nekokatt authored Sep 8, 2020
2 parents 9ea919f + 3568161 commit 51d815b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hikari/impl/stateful_event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

__all__: typing.Final[typing.List[str]] = ["StatefulEventManagerImpl"]

import asyncio
import typing

from hikari import channels
Expand Down Expand Up @@ -166,7 +167,9 @@ async def on_guild_create(self, shard: gateway_shard.GatewayShard, payload: data
# payload if presence intents are also declared, so if this isn't the case then we also want
# to chunk small guilds.
if (event.guild.is_large or not presences_declared) and members_declared:
await shard.request_guild_members(event.guild)
# We create a task here instead of awaiting the result to avoid any rate-limits from delaying dispatch.
coroutine = shard.request_guild_members(event.guild)
asyncio.create_task(coroutine, name=f"{event.shard.id}:{event.guild.id} guild create members request")

await self.dispatch(event)

Expand Down
5 changes: 5 additions & 0 deletions tests/hikari/hikari_test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,8 @@ async def wrapper(*args, **kwargs):
return decorator(decorated_func)
else:
return decorator


async def gather_all_tasks():
"""Ensure all created tasks except the current are finished before asserting anything."""
await asyncio.gather(*(task for task in asyncio.all_tasks() if task is not asyncio.current_task()))

0 comments on commit 51d815b

Please sign in to comment.