Skip to content

Commit a63c1b1

Browse files
committed
Allow some services to not use a connection pool to the DB
Some services managed by supervisor do need the DB during startup only to make at most a handfull of queries (if even). However, because they're using a connection pool behind the scenes, the connections are kept in that pool for some time unnecessarily. This commit adds a way to disable connection pooling with an environment variable. This env var is enabled for all services that currently use the DB during startup only.
1 parent b3858fd commit a63c1b1

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

config/supervisord.conf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ autorestart=unexpected
2525

2626
[program:log_event_loop]
2727
command=/code/manage.py log_loop
28-
environment=LOGGING_FILENAME=log_event_loop_%(ENV_SERVER_NUMBER)s.log
28+
environment=LOGGING_FILENAME=log_event_loop_%(ENV_SERVER_NUMBER)s.log,HLL_DB_DISABLE_CONNECTION_POOL=1
2929
startretries=1000000
3030
startsecs=1
3131
autostart=true
3232
autorestart=true
3333

3434
[program:log_stream]
3535
command=/code/manage.py log_stream
36-
environment=LOGGING_FILENAME=log_stream_%(ENV_SERVER_NUMBER)s.log
36+
environment=LOGGING_FILENAME=log_stream_%(ENV_SERVER_NUMBER)s.log,HLL_DB_DISABLE_CONNECTION_POOL=1
3737
startretries=5
3838
startsecs=0
3939
autostart=true
@@ -49,7 +49,7 @@ autorestart=true
4949

5050
[program:auto_settings]
5151
command=/code/manage.py auto_settings
52-
environment=LOGGING_FILENAME=auto_settings_%(ENV_SERVER_NUMBER)s.log
52+
environment=LOGGING_FILENAME=auto_settings_%(ENV_SERVER_NUMBER)s.log,HLL_DB_DISABLE_CONNECTION_POOL=1
5353
startretries=100
5454
startsecs=1
5555
autostart=true
@@ -70,7 +70,7 @@ autostart=true
7070

7171
[program:live_stats_refresh]
7272
command=/code/manage.py live_stats_loop
73-
environment=LOGGING_FILENAME=live_stats_loop_%(ENV_SERVER_NUMBER)s.log
73+
environment=LOGGING_FILENAME=live_stats_loop_%(ENV_SERVER_NUMBER)s.log,HLL_DB_DISABLE_CONNECTION_POOL=1
7474
startretries=100
7575
startsecs=1
7676
autostart=true

rcon/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import rcon.user_config
1616
import rcon.user_config.utils
1717
import rcon.watch_killrate
18-
from rcon import auto_settings, broadcast, maps, routines
18+
from rcon import auto_settings, broadcast, routines
1919
from rcon.automods import automod
2020
from rcon.blacklist import BlacklistCommandHandler
2121
from rcon.cache_utils import RedisCached, get_redis_pool, invalidates
@@ -30,7 +30,6 @@
3030
from rcon.user_config.auto_settings import AutoSettingsConfig
3131
from rcon.user_config.log_stream import LogStreamUserConfig
3232
from rcon.user_config.scoreboard import _port_legacy_scorebot_urls
33-
from rcon.user_config.vote_map import VoteMapUserConfig
3433
from rcon.user_config.webhooks import (
3534
BaseMentionWebhookUserConfig,
3635
BaseUserConfig,

rcon/models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any, Generator, List, Literal, Optional, Sequence, overload, TypedDict
88

99
import pydantic
10-
from sqlalchemy import TIMESTAMP, Enum, ForeignKey, String, create_engine, select, text, JSON
10+
from sqlalchemy import TIMESTAMP, Enum, ForeignKey, String, create_engine, select, text, JSON, Engine, NullPool, Pool
1111
from sqlalchemy.dialects.postgresql import JSONB
1212
from sqlalchemy.exc import InvalidRequestError, ProgrammingError
1313
from sqlalchemy.ext.hybrid import hybrid_property
@@ -70,7 +70,7 @@
7070

7171
PLAYER_ID = "player_id"
7272

73-
_ENGINE = None
73+
_ENGINE: Engine | None = None
7474

7575

7676
def get_engine():
@@ -84,7 +84,11 @@ def get_engine():
8484
logger.error(msg)
8585
raise ValueError(msg)
8686

87-
_ENGINE = create_engine(url, echo=False)
87+
pool: type[Pool] | None = None
88+
if os.getenv("HLL_DB_DISABLE_CONNECTION_POOL") is not None:
89+
pool = NullPool
90+
91+
_ENGINE = create_engine(url, poolclass=pool, echo=False)
8892
return _ENGINE
8993

9094

0 commit comments

Comments
 (0)