feat(backends): add aclose() and async with to every backend - #308
Merged
Merged
Conversation
BaseCacheBackend gains a no-op aclose() plus __aenter__/__aexit__. AsyncRedisCacheBackend closes its redis-py client (a caller-supplied connection_pool stays open, as in redis-py) and MemcachedBackend closes every pooled socket in a worker thread. The redis example and the test fixture that closes network clients now go through backend.aclose().
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.
Summary
BaseCacheBackendgets a no-opaclose()(not abstract, so third-party backends keep working) plus__aenter__(returnsSelf) /__aexit__(callsaclose()). Every backend can now be used withasync with.AsyncRedisCacheBackend.aclose()awaitsclient.aclose(). That closes the connection pool the client created. A pool the caller passes asconnection_pool=stays open, which is redis-py's own ownership rule (auto_close_connection_poolis False for a supplied pool). The types-redisattr-definedignore now lives inside the backend.MemcachedBackend.aclose()runsHashClient.close()in a worker thread. For a pooled client that callsObjectPool.clear(), which closes both free and in-use sockets on every server.aclose()reconnects (redis-py and pymemcache both reopen connections on demand), so it needs anotheraclose(). This is documented.examples/redis_backend.pynow callsawait backend.aclose()and no longer needs the# type: ignore.mypy --strict examples/redis_backend.pyis clean.close_network_clientsfixture now callsbackend.aclose(). It still skips mocked clients.docs/BACKENDS.mdand the zh-TW mirror. It covers the lifespan pattern for every backend,async with, the ownership and reuse rules, and the fact thatBackendProxy.set(None)does not close anything. The MemoryBackend section links to it.Not in scope:
BackendProxydoes not close a backend when it is replaced or unset.Changelog
changelog.d/243.added.mdTests
async withreturn value, closing when the body raises, Redis/Memcached with mocked clients, a repeatedaclose()on unconnected real clients, and the Redis caller-supplied pool left open. The pool test fails ifclose_connection_pool=Trueis forced.aclose(), and a second call is safe.Closes #243