Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lock Redis from global detached context. #350

Merged
merged 5 commits into from
Jun 20, 2023

Conversation

MeirShpilraien
Copy link
Collaborator

@MeirShpilraien MeirShpilraien commented Jun 19, 2023

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:

  1. Creating a ThreadSafeContext is costly.
  2. 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 implements Deref<Context> so it can be used just like a regular Context 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.

Sometimes we need to perfrom 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:

1. Creating a `ThreadSafeContext` is costly.
2. `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` implements `Deref<Context>` so it can be used just like
a regular `Context` to perform operations.

**Notice: This context should not be use to return any replies!!!**

Future improvement is to seperate contexts for command invocation and replies so those can not be accidently misstaken, notice that this PR do not introduce any regression regarding this topic because we already have this issue with `ThreadSafeContext`.
@MeirShpilraien MeirShpilraien requested a review from iddm June 19, 2023 14:36
@iddm
Copy link
Collaborator

iddm commented Jun 19, 2023

I like the idea of having separate contexts that can never be used mistakenly.

@MeirShpilraien
Copy link
Collaborator Author

Issue opened: #351

@MeirShpilraien MeirShpilraien merged commit 9fe7eac into master Jun 20, 2023
@MeirShpilraien MeirShpilraien deleted the lock_from_detached_context branch June 20, 2023 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants