File: README.rst

package info (click to toggle)
python-django-dbconn-retry 0.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 148 kB
  • sloc: python: 209; sh: 7; sql: 4; makefile: 4
file content (126 lines) | stat: -rw-r--r-- 5,882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Django Database Connection Autoreconnect
========================================

.. image:: https://coveralls.io/repos/github/jdelic/django-dbconn-retry/badge.svg?branch=master
    :target: https://coveralls.io/github/jdelic/django-dbconn-retry?branch=master

.. image:: https://github.com/jdelic/django-dbconn-retry/actions/workflows/django.yml/badge.svg
    :target: https://github.com/jdelic/django-dbconn-retry/actions

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

.. code-block::

    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.
===========================  ==================================================

Both signals send a parameter ``dbwrapper`` which points to the current instance
of ``django.db.backends.base.BaseDatabaseWrapper`` which allows the signal
receiver to act on the database connection.


Settings
--------
Here’s a list of settings available in django-dbconn-retry and their default values.
You can change the value in your ``settings.py``.

===========================  ==================================================
Setting                       Description
===========================  ==================================================
``MAX_DBCONN_RETRY_TIMES``   Default: `1`
                             The max times which django-dbconn-retry will try.
===========================  ==================================================


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:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. 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.

3. 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.


.. _12factor-vault: https://github.com/jdelic/12factor-vault/
.. _Django Signals: https://docs.djangoproject.com/en/dev/topics/signals/
.. _HAProxy: http://www.haproxy.org/
.. _tcpka:
   https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#option%20tcpka
.. _clitcpka:
   https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#4-option%20clitcpka
.. _srvtcpka:
   https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#option%20srvtcpka
.. _Hashicorp Vault: https://vaultproject.io/