Allow lock Redis from global detached context. #350
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.
Sometimes we need to perform operation on Redis from a background thread, for this we need to lock Redis. We can use
ThreadSafeContext
but we might prefer not to for the following reasons:ThreadSafeContext
is costly.ThreadSafeContext
which is not attached to a client do not have the module pointer and this could cause some operations to fail.The PR adds the ability to lock Redis using the global detached context. After locking, we will get
DetachedContextGuard
object which will automatically unlock Redis when dispose.DetachedContextGuard
implementsDeref<Context>
so it can be used just like a regularContext
to perform operations.Notice: This context should not be use to return any replies!!!
Notice: Locking Redis by a thread that already owns the Redis lock is considered undefined behaviour!!!
Future improvement is to separate contexts for command invocation and replies so those can not be accidentally mistaken, notice that this PR do not introduce any regression regarding this topic because we already have this issue with
ThreadSafeContext
.