-
Notifications
You must be signed in to change notification settings - Fork 7.4k
feat(lock_store): make locking backend overridable #6015
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
Merged
+88
−9
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1a18c9b
feat(lock_store): make locking backend overridable
mattatcha 623f35e
refactor(lock_store): use explicit body for LockBackend protocol method
mattatcha ccaacb3
refactor(lock_store): drop scoped lock_backend context manager
mattatcha 9ecc097
refactor(lock_store): drop reset_lock_backend alias
mattatcha 07f177c
style(lock_store): apply ruff format
mattatcha 986dfff
refactor(lock_store): simplify overridable backend to a single setter
mattatcha f399c74
fix(lock_store): snapshot backend to avoid check-then-call race
mattatcha a48fb20
docs(lock_store): clarify name handling for custom backends
mattatcha b4c097d
docs(lock_store): note set_lock_backend is for one-time startup setup
mattatcha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| """Centralised lock factory. | ||
|
|
||
| If ``REDIS_URL`` is set and the ``redis`` package is installed, locks are | ||
| distributed via ``portalocker.RedisLock``. Otherwise, falls back to the | ||
| standard file-based ``portalocker.Lock`` in the system temp dir. | ||
| By default, if ``REDIS_URL`` is set and the ``redis`` package is installed, | ||
| locks are distributed via ``portalocker.RedisLock``. Otherwise, falls back to | ||
| the standard file-based ``portalocker.Lock`` in the system temp dir. | ||
|
|
||
| The backend can be replaced via :func:`set_lock_backend` to plug in a custom | ||
| locking strategy (e.g. a different distributed lock service, or an in-process | ||
| lock for tests). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator | ||
| from contextlib import contextmanager | ||
| from collections.abc import Callable, Iterator | ||
| from contextlib import AbstractContextManager, contextmanager | ||
| from functools import lru_cache | ||
| from hashlib import md5 | ||
| import logging | ||
|
|
@@ -30,6 +34,22 @@ | |
|
|
||
| _DEFAULT_TIMEOUT: Final[int] = 120 | ||
|
|
||
| # A backend is called as ``backend(name, timeout=...)`` and returns a context | ||
| # manager that holds the lock while the ``with`` block runs. | ||
| LockBackend = Callable[..., AbstractContextManager[None]] | ||
|
|
||
| # ``None`` means use the built-in Redis/file selection. | ||
| _backend: LockBackend | None = None | ||
|
|
||
|
|
||
| def set_lock_backend(backend: LockBackend | None) -> None: | ||
| """Replace the locking backend used by :func:`lock`. | ||
|
|
||
| Pass ``None`` to restore the built-in Redis/file default. | ||
| """ | ||
| global _backend | ||
| _backend = backend | ||
|
|
||
|
|
||
| def _redis_available() -> bool: | ||
| """Return True if redis is installed and REDIS_URL is set.""" | ||
|
|
@@ -62,6 +82,11 @@ def lock(name: str, *, timeout: float = _DEFAULT_TIMEOUT) -> Iterator[None]: | |
| Automatically namespaced to avoid collisions. | ||
| timeout: Maximum seconds to wait for the lock before raising. | ||
| """ | ||
| if _backend is not None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Race here |
||
| with _backend(name, timeout=timeout): | ||
| yield | ||
| return | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| channel = f"crewai:{md5(name.encode(), usedforsecurity=False).hexdigest()}" | ||
|
|
||
| if _redis_available(): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.