Django Database Connection Autoreconnect
This library monkeypatches django.db.backends.base.BaseDatabaseWrapper so that when a database operation fails because the underlying TCP connection was already closed, it first tried to reconnect, instead of immediately raising an OperationException.
Why is this useful?
I use HAProxy as a load-balancer in front of my PostgreSQL databases all the time, sometimes in addition to pgbouncer. Even though you can mostly prevent surprises by enabling TCP keep-alive packets through tcpka, clitcpka and srvtcpka, I still encounter situations where the underlying TCP connection has been closed through the load-balancer. Most often this results in
django.db.utils.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally before or while
processing the request.
This library patches Django such that it try to reconnect once before failing.
Another application of this is when using Hashicorp Vault, where credentials for a database connection can expire at any time and then need to be refreshed from Vault.
How to install?
Just pull the library in using pip install django-dbconn-retry. Then add django_dbconn_retry to INSTALLED_APPS in your settings.py.
Signals
The library provides an interface for other code to plug into the process to, for example, allow 12factor-vault to refresh the database credentials before the code tries to reestablish the database connection. These are implemented using Django Signals.
Signal |
Description |
|---|---|
pre_reconnect |
Installs a hook of the type Callable[[type, BaseDatabaseWrapper], None] that will be called before the library tries to reestablish a connection. 12factor-vault uses this to refresh the database credentials from Vault. |
post_reconnect |
Installs a hook of the type Callable[[type, BaseDatabaseWrapper], None] that will be called after the library tried to reestablish the connection. Success or failure has not been tested at this point. So the connection may be in any state. |
License
Copyright (c) 2018, Jonas Maurus All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Metadata
Release files for django-dbconn-retry-coreteam 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-dbconn-retry-coreteam-0.1.7.tar.gz | 7.9 kB | Without provenance | Details for django-dbconn-retry-coreteam-0.1.7.tar.gz |
Release files / django-dbconn-retry-coreteam-0.1.7.tar.gz
| Download URL | django-dbconn-retry-coreteam-0.1.7.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
05bd78401e8a97b567c4ce80d1996e9ed258d8ef0f300e20555d0e13500083e9
|
|
BLAKE2b-256 checksum How to use checksums |
e940cccc02d64c453783bdcd7eb3712283fa078e58db3924970b6ffc5fd21250
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10
|
Security for django-dbconn-retry-coreteam 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.