Async instead of sync methods in realtime classes #6777
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.
Changes
I changed all synchronous calls to asynchronous calls in interfaces/classes:
And in all places where they are called (including tests).
Removed:
I also changed the logic of IOnlineClientManager.GetAllByUserId,
namely optimized this by storing a separate hash set of users with references to the connection hash set in Redis.
Added GetAllByUserId / GetAllByUserIdAsync to IOnlineClientStore and RedisOnlineClientStore.
Reasoning
When using the latest version of a project, I noticed that the number of threads in the thread pool grows about x3 times the usual value since synchronous calls create thread pool starvation issues.
First, you can see the graphs from Application Insights, which clearly show the increase in ThreadPool:
The following screenshots are an accurate description of what blocks the application the most, and it turned out to be synchronous methods:
This all means that synchronous calls block and should be replaced with asynchronous calls
About Redis
In the current implementation, all online clients are retrieved from the Redis cache, which is very expensive. For instance in production env, this means transferring ~3 MB of data.
After local changes and testing, the situation has improved, so I would like to see these changes in the framework itself.