django-balancer
django-balancer copied to clipboard
Take DB downtime into account
When a database in the pool is down, temporarily remove it from the pool but periodically re-check the connection. If it comes back up, add the database back to the pool.
It appears that psycopg2.OperationalError
(the exception raised when the database cannot be connected to) is not caught by the try...except
block inside django.core.handlers.base.BaseHandler.get_response
, so I'm not able to use django.core.signals.got_request_exception
to take that database out of the pool. I'm trying to find another way, but it looks like I may be going down the wrong path. I'm open to suggestions on other ways to handle this.
I could probably create a new database backend that subclasses DatabaseWrapper and wraps ._cursor() in a try...except
block. I'd have to make a new backend for every existing backend, though, and that would be messy. I want something more elegant. Back to the drawing board...
Wrapping the cursor (or even ... you could monkeypatch it) is probably the easiest way.
I have looked at this issue a couple of months ago when writing django-statsd (http://pypi.python.org/pypi/django-statsd/) and it seemed that monkeypatching is the best way to go here.
But if you know any good way to do it, please let me know.