Skip to content

Commit 061a11c

Browse files
committed
move _close_old_connections to module level
1 parent 4d02693 commit 061a11c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

channels/db.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
HAS_INC_THREAD_SHARING = django.VERSION >= (2, 2)
77

88

9+
def _close_old_connections():
10+
"""Like django.db.close_old_connections, but skipping in_atomic_block.
11+
12+
Ref: https://code.djangoproject.com/ticket/30448
13+
Ref: https://github.com/django/django/pull/11769
14+
"""
15+
for conn in connections.all():
16+
if not conn.in_atomic_block:
17+
conn.close_if_unusable_or_obsolete()
18+
19+
920
class DatabaseSyncToAsync(SyncToAsync):
1021
"""
1122
SyncToAsync version that cleans up old database connections.
@@ -34,22 +45,13 @@ def _inherit_main_thread_connections(self):
3445
connections[name].allow_thread_sharing = True
3546
return restore_allow_thread_sharing
3647

37-
def _close_old_connections(self):
38-
"""Like django.db.close_old_connections, but skipping in_atomic_block.
39-
40-
Ref: https://github.com/django/django/pull/11769
41-
"""
42-
for conn in connections.all():
43-
if not conn.in_atomic_block:
44-
conn.close_if_unusable_or_obsolete()
45-
4648
def thread_handler(self, loop, *args, **kwargs):
4749
restore_allow_thread_sharing = self._inherit_main_thread_connections()
48-
self._close_old_connections()
50+
_close_old_connections()
4951
try:
5052
return super().thread_handler(loop, *args, **kwargs)
5153
finally:
52-
self._close_old_connections()
54+
_close_old_connections()
5355
for name, saved_sharing in restore_allow_thread_sharing.items():
5456
connections[name].allow_thread_sharing = saved_sharing
5557

0 commit comments

Comments
 (0)