Conversation
valentijnscholten
left a comment
There was a problem hiding this comment.
Years ago I also did some quick testing with CONN_MAX_AGE, but it caused problems. I don't know exactly what, but I think it was that most code isn't prepared to transparently handle closed connections to open a new one. Did you manage to test that scenario?
I think thats what we're likely seeing in these unit tests. Although im not too sure why they are failing with the age set to 3600. Possibly its using a default of 0 some-how/where. Investigation continues... |
Description
Celery workers are long-lived processes without Django's request/response lifecycle. Django normally closes stale DB connections via its request middleware, but Celery never triggers that. As a result:
For users with high frequency usage, this can cause the DB to consume a high amount of resources due to idle connection build up.
Fix: Two signal handlers on task_prerun and task_postrun that call close_old_connections():
I've set this to 300s versus the default of 0. I should note that it will not close a connection thats older than 300, rather, a connection that hasnt been used in 300s. An important distinction.