Skip to content

Commit

Permalink
[Hockey] Try and reduce edit goal tasks
Browse files Browse the repository at this point in the history
- Track goal edit task and wait until they're done before proceeding to a second edit. This should hopefully reduce instances of rate limiting if a goal info is changed before we've finished editing them prior.
  • Loading branch information
TrustyJAID committed Oct 16, 2024
1 parent e459085 commit 4d4dfbf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
40 changes: 39 additions & 1 deletion hockey/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,45 @@ async def check_team_goals(self, bot: Red) -> None:
# This is to keep shots consistent between edits
# Shots should not update as the game continues
bot.dispatch("hockey_goal_edit", self, goal)
asyncio.create_task(goal.edit_team_goal(bot, self))

# This is here in order to prevent the bot attempting to edit the same
# goal b2b causing increase of requests to discord for editing and
# hopefully reducing instances of rate limiting.
# The premise is that when we create this task, store a reference to it
# on the cog and when we want to edit see if there is already a task running.
# If a task is running we will instead wait for that task to finish and then
# run the same code. This way they work one after another instead of parallell.
# The done callback removes the task reference when the task is done so
# this should be efficient.

key = f"{self.game_id}-{goal.goal_id}"

def done_edit_callback(task):
task_name = task.get_name()
try:
del cog._edit_tasks[task_name]
except Exception:
log.exception(
"Error removing edit task from list, unknown task name %s",
task_name,
)

if key not in cog._edit_tasks:
log.debug("Creating edit task for %s", key)
cog._edit_tasks[key] = asyncio.create_task(
goal.edit_team_goal(bot, self), name=key
)
cog._edit_tasks[key].add_done_callback(done_edit_callback)
else:
log.debug("Found existing edit task, waiting for %s", key)
task = cog._edit_tasks[key]
await task.wait()
log.debug("Done waiting for %s", key)
cog._edit_tasks[key] = asyncio.create_task(
goal.edit_team_goal(bot, self), name=key
)
cog._edit_tasks[key].add_done_callback(done_edit_callback)

async with cog.config.teams() as teams:
for team in teams:
if (
Expand Down
1 change: 1 addition & 0 deletions hockey/hockey.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def __init__(self, bot):
self._commit = ""
self.api = NewAPI()
self.saving_goals = {}
self._edit_tasks = {}

def format_help_for_context(self, ctx: commands.Context) -> str:
"""
Expand Down

0 comments on commit 4d4dfbf

Please sign in to comment.