Skip to content

Implement cache lock when saving files(TS-2404) #1136

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Roffenlund
Copy link
Contributor

Implement a locking mechanism to storage.py that prevents race conditions when saving files to the mirrors.

Implement tests.

Implement a locking mechanism to storage.py that prevents race
conditions when saving files to the mirrors.

Implement tests.

Refs. TS-2404
Comment on lines +51 to +63
cache_key = f"cache_mirror_storage_{name}"

if not cache.add(cache_key, "LOCKED", timeout=5):
raise Exception("Another save operation is in progress for this file.")

try:
for storage_mirror in self.mirrors:
with TemporarySpooledCopy(content) as tmp_content:
storage_mirror.save(name, tmp_content, max_length)

return super().save(name, content, max_length)
finally:
cache.delete(cache_key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use Django cache directly, Redis has first party support for locking which should be used. Check thunderstore.cache.utils.get_cache and how it's utilized for locking in thunderstore.cache.cache.try_regenerate_cache for example

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