Skip to content
Merged
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
11 changes: 10 additions & 1 deletion brood/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@
BROOD_DB_URI_READ_ONLY,
BROOD_POOL_SIZE,
BROOD_DB_STATEMENT_TIMEOUT_MILLIS,
BROOD_DB_POOL_RECYCLE_SECONDS,
)


def create_brood_engine(url: Optional[str], pool_size: int, statement_timeout: int):
def create_brood_engine(
url: Optional[str],
pool_size: int,
statement_timeout: int,
pool_recycle: int = BROOD_DB_POOL_RECYCLE_SECONDS,
):
# Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool
# Statement timeout: https://stackoverflow.com/a/44936982
return create_engine(
url=url,
pool_size=pool_size,
pool_recycle=pool_recycle,
connect_args={"options": f"-c statement_timeout={statement_timeout}"},
)

Expand All @@ -28,6 +35,7 @@ def create_brood_engine(url: Optional[str], pool_size: int, statement_timeout: i
url=BROOD_DB_URI,
pool_size=BROOD_POOL_SIZE,
statement_timeout=BROOD_DB_STATEMENT_TIMEOUT_MILLIS,
pool_recycle=BROOD_DB_POOL_RECYCLE_SECONDS,
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Expand All @@ -52,6 +60,7 @@ def yield_db_session_from_env() -> Session:
url=BROOD_DB_URI_READ_ONLY,
pool_size=BROOD_POOL_SIZE,
statement_timeout=BROOD_DB_STATEMENT_TIMEOUT_MILLIS,
pool_recycle=BROOD_DB_POOL_RECYCLE_SECONDS,
)
RO_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=RO_engine)

Expand Down
10 changes: 10 additions & 0 deletions brood/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
f"BROOD_DB_STATEMENT_TIMEOUT_MILLIS must be an integer: {BROOD_DB_STATEMENT_TIMEOUT_MILLIS_RAW}"
)

BROOD_DB_POOL_RECYCLE_SECONDS_RAW = os.environ.get("BROOD_DB_POOL_RECYCLE_SECONDS")
BROOD_DB_POOL_RECYCLE_SECONDS = 1800
try:
if BROOD_DB_POOL_RECYCLE_SECONDS_RAW is not None:
BROOD_DB_POOL_RECYCLE_SECONDS = int(BROOD_DB_POOL_RECYCLE_SECONDS_RAW)
except:
raise ValueError(
f"BROOD_DB_POOL_RECYCLE_SECONDS must be an integer: {BROOD_DB_POOL_RECYCLE_SECONDS_RAW}"
)

# Bots
BOT_INSTALLATION_TOKEN = os.environ.get("BUGOUT_BOT_INSTALLATION_TOKEN")
BOT_INSTALLATION_TOKEN_HEADER_RAW = os.environ.get(
Expand Down