feat: rework role caching #404
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Currently, role-caching has a problem. Creating a role with the bot causes a race condition to occur where the created role is cached twice. The code already tries to prevent this by checking if the role already exists, but to no avail. I can reproduce this race every time I create a role with the bot, so I don't think this is just some one-off case that we should ignore.
Currently, my solution is to internally store the roles in a hash keyed by ID (this was actually already being done
@roles_by_idby the IVAR would never be updated after initial data saturation), and then serialize the map back into an array when the user calls#roles. One additional benefit of storing the roles in a hash is that lookups by ID are a lot faster now. This should not have any user-facing changes except that there isn't a race condition anymore.This also seemed like a good chance to simplify how we update role data. In the spirit of #400, I believe that it's in our best interests to stop maintaining 3 different methods to update data and DRY things up to use a central function. This led me to realizing that I misspelled
colourand used the American spelling accidentally. This should not have any user-facing changes because these are not publicly documented methods and are explicitly marked asFOR INTERNAL USE ONLY.Fixed
Closes #177.