Fix large blocked users list preventing login #175
Merged
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.
Fixes #169.
Thanks to the detailed reporting in #169, we were able to discover that having a really large list of blocked users was blocking the UI and preventing the login from completing properly.
The cause
After logging in, there is an initialization process that involves fetching information regarding the logged-in user. These steps are performed sequentially through fetching with
await
, so the next steps must wait for the previous ones to complete.When fetching blocked users, the logged-in user could have thousands, if not, hundreds of thousands of blocked users (one reason being bots). This will cause "jank" and essentially freeze the UI until all users have been fetched.
Furthermore, the Twitch API only allows fetching up to 100 blocked users per request, with a limit of 800 requests per minute. Therefore, when too many blocked users are fetched, it will prevent the login initialization from completing.
The fix
In order to fix this, I've removed
await
from the request and have opted for a normal async future with a completion handler. This will prevent the main thread from being blocked and perform the request in the "background".To account for the 100 blocked users per request and the Twitch API limit, I've added a delay of 150 milliseconds between each request. This means that total requests for blocked users are capped at 400 per minute, half of the 800 per minute cap previously mentioned. Theoretically, this will prevent requests from throwing a rate limit error.