Repository navigation
feat: raise BackendUnavailableError when the server cannot be reached (#260) - #464
Merged
Merged
Conversation
…#260) Each network backend raised its own client's exceptions on a transport failure, so handling "the cache is down" meant knowing which backend is configured and importing redis or pymemcache exceptions. The Redis and Memcached backends' methods now re-raise a refused or reset connection, a timeout, and pymemcache's "All servers seem to be down" as BackendUnavailableError (a CacheXError), chained to the original. The class raised is built per original type and inherits from both, so it is also a redis.exceptions.ConnectionError, an OSError and so on: existing except clauses keep catching it. It pickles. Other errors (bad input, an item too large) pass through unchanged, and @cache still fails open on every backend error. Closes #260
18 tasks done
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #260
Summary
On a transport failure, each network backend raised its own client's exceptions. To handle "the cache is down" around
CacheManager,CacheLockor a backend call, you had to know which backend was configured and import redis or pymemcache exceptions.What changes
BackendUnavailableError(CacheXError), exported fromfastapi_cachex.get,set,delete,delete_many, the atomic primitives,increment,clear*,get_all_keys,get_cache_data) withbackends/_unavailable.py. A transport failure is raised again asBackendUnavailableError, chained to the original. Transport failures are:ConnectionError(and its subclasses),TimeoutError, and a rawOSError.OSError: refused, reset, timed out),MemcacheUnexpectedCloseError, and HashClient's "All servers seem to be down" while a server is out of rotation.MemcacheClientErrorsuch as an item too large (Handle Memcached "object too large" consistently outside @cache #261), and the existingCacheXErrorcases. The memory backend never raises it.Not a breaking change. This answers the issue's first open question. The class raised is built once per original type and inherits from both
BackendUnavailableErrorand that type, so:ConnectionRefusedErrorandOSError, with the sameerrno. A Redis one is also aredis.exceptions.ConnectionError.except redis.exceptions.ConnectionError,except OSErrorandexcept MemcacheErrorclauses keep catching it. The existing suite passes unchanged, including the tests that expectConnectionRefusedError.BackendUnavailableErrorin tracebacks and pickle correctly through__reduce__.Left as is
@cachestill fails open on any backend error. I did not narrow its catch, as the issue only suggested considering it; that would change behaviour.Docs (English and zh-TW): a new "When the server is unavailable" section in
BACKENDS.mdwith an example, and links from the Memcached and fail-fast sections. Changelog fragment:260.added.md.As in #463, the docs say "before 0.5.1", assuming this ships in the next patch release.
Checklist
The issue above is assigned to me (maintainer's own issue)
Tests under
tests/cover the change.tests/backends/test_unavailable.pyneeds no server; it points at a port nothing listens on. It covers:__cause__,errno)CacheManagerpropagating the errorDisabling the translation fails the three backend tests.
A changelog fragment:
changelog.d/260.added.mduv run pre-commit run --all-filesanduv run pytest --covpass:mypy tests,mypy scriptsand bothzensical build --strictruns passGenerated by Claude Code