Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django channels uses persistent database connections? #148

Closed
sachinrekhi opened this issue May 9, 2016 · 5 comments
Closed

Django channels uses persistent database connections? #148

sachinrekhi opened this issue May 9, 2016 · 5 comments
Labels

Comments

@sachinrekhi
Copy link
Contributor

One of the things I've seemed to notice in my app that is deployed via django channels vs. my app that is deployed via gunicorn is that the django channels app seems to persist database connections for some time wheres they open/close for each request in gunicorn.

In both cases I have the Django setting CONN_MAX_AGE set to the default, meaning the db connection should open/close on each request. But it appears django channels ignores this option? If I add the option in, will django channels respect the timeframe given for max age?

@andrewgodwin
Copy link
Member

Are you talking about code deployed as views or as consumers? It's likely we need extra work to make them wrap well around consumers, as they don't have the request started/ended signals, but the view framework should continue to work as it did previously (unless I missed sending a request_close signal)

@sachinrekhi
Copy link
Contributor Author

aah, got it. Ya it appears that the persisted connections are coming from consumer-related code as opposed to view-related code, which seem to immediately close the connection as per usual.

The reason this came up is I was planning on increasing the value of CONN_MAX_AGE since I want the performance optimization associated with persisted connections. So in terms of prioritizing I'd personally be more interested in ensuring consumers respect long CONN_MAX_AGE values (as opposed to the scenario of ensuring they open/close on each request when CONN_MAX_AGE is set to 0).

@andrewgodwin
Copy link
Member

Sure, I suspect we can just hook up similar code to the ones that requests use to achieve this. I'll take a look at it this week,

@spearki
Copy link

spearki commented May 27, 2016

I'm seeing this cause issues every time we hit the MySQL wait_timeout. Any messages received after the timeout get a "MySQL server has gone away message" and the www worker needs to be restarted before it will process messages again.

At the moment we're handling this with a retry decorator that closes the stale connection. We only close when the exception is raised so this doesn't respect CONN_MAX_AGE either, but you could call close_old_connections() after each call to the consumer instead.

@heteddy
Copy link

heteddy commented Sep 11, 2018

the root cause is that django will close old connections(which has been closed by MySQL) before handing each request and response; while in channels no request trigger close old connections then the database connection will be disconnected by MySQL, so we should do it by yourself;

in channels, before access database

from django.db import close_old_connections close_old_connections

@django django deleted a comment from heteddy Sep 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants