Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions ascender/main/tests/unit/settings/test_connection_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from ascender.settings.connection_reuse import DEFAULT_WEB_CONN_MAX_AGE, is_web_process, set_conn_max_age
from ascender.settings.connection_reuse import set_conn_max_age


@pytest.fixture
Expand All @@ -20,22 +20,13 @@ def no_web_marker(monkeypatch):
monkeypatch.delenv('AWX_WEB_PROCESS', raising=False)


class TestIsWebProcess:
def test_the_marker_is_a_web_process(self, monkeypatch):
monkeypatch.setenv('AWX_WEB_PROCESS', '1')
assert is_web_process() is True

def test_anything_else_is_not(self):
assert is_web_process() is False


class TestWebProcess:
def test_a_web_process_keeps_its_connection(self, databases, monkeypatch):
def test_a_web_process_keeps_no_connection(self, databases, monkeypatch):
monkeypatch.setenv('AWX_WEB_PROCESS', '1')

set_conn_max_age(databases)

assert databases['default']['CONN_MAX_AGE'] == DEFAULT_WEB_CONN_MAX_AGE
assert 'CONN_MAX_AGE' not in databases['default']

def test_the_setting_wins_over_the_default(self, databases, monkeypatch):
monkeypatch.setenv('AWX_WEB_PROCESS', '1')
Expand Down
34 changes: 4 additions & 30 deletions ascender/settings/connection_reuse.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
from ascender.settings.environment import environment_setting

# Django's default is 0: open a connection for each request and close it when
# the response is sent. For a process that serves one request after another
# that is pure churn, since the next request immediately needs another one,
# and a connection costs several milliseconds to establish before it has run
# any query at all.
#
# Sixty seconds is deliberately short. A worker that goes idle holds its
# connection until its next request notices the age, so the number held at
# rest is the number of web workers, and a short life keeps a connection from
# outliving a network path that has quietly gone away.
DEFAULT_WEB_CONN_MAX_AGE = 60


def is_web_process():
'''
Whether this process serves HTTP.

A web process says so with AWX_WEB_PROCESS, which the supervisor programs
that run uvicorn set. Nothing else here serves HTTP.
'''
return bool(environment_setting('WEB_PROCESS'))


def set_conn_max_age(DATABASES, DATABASE_CONN_MAX_AGE=None):
'''
Keep a web process's database connection alive between requests.
Leave every process at Django's default of 0 unless DATABASE_CONN_MAX_AGE
is set. A web process too: uvicorn runs each request in a thread of its
own, so a connection kept past the request is left behind with that thread
and never closed.

Task workers, management commands and migrations are left alone: they hold
one connection for the life of a long process, or want it closed the moment
Expand All @@ -40,7 +18,3 @@ def set_conn_max_age(DATABASES, DATABASE_CONN_MAX_AGE=None):

if DATABASE_CONN_MAX_AGE is not None:
DATABASES['default']['CONN_MAX_AGE'] = DATABASE_CONN_MAX_AGE
return

if is_web_process():
DATABASES['default']['CONN_MAX_AGE'] = DEFAULT_WEB_CONN_MAX_AGE
5 changes: 2 additions & 3 deletions ascender/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@
JOB_EVENT_ARCHIVE_DIR = None

# Optional manual override for how long a database connection is kept and
# reused, in seconds. Unset, a web process reuses one for a minute and every
# other process opens one per request, which is Django's default. 0 turns
# reuse off everywhere.
# reused, in seconds. Unset, every process opens one per request, which is
# Django's default. 0 turns reuse off everywhere.
DATABASE_CONN_MAX_AGE = None

# Special database overrides for dispatcher connections listening to pg_notify
Expand Down