Skip to content

Commit

Permalink
Merge pull request #9 from teams-notifier/feat/reduce-default-db-min-…
Browse files Browse the repository at this point in the history
…pool-size

feat: reduce minimum db pool size to 1
  • Loading branch information
babs authored Jan 21, 2025
2 parents 17c747a + 9210e63 commit ec886ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 7 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
dotenv.load_dotenv()
""" Bot Configuration """

__all__ = ["DefaultConfig", "config"]


class DefaultConfig:
"""Bot Configuration"""
Expand All @@ -25,6 +27,8 @@ class DefaultConfig:
APP_TYPE = os.environ.get("MICROSOFT_APP_TYPE", "MultiTenant")
APP_TENANTID = os.environ.get("MICROSOFT_APP_TENANT_ID", "")
DATABASE_URL = os.environ.get("DATABASE_URL", "")
DATABASE_POOL_MIN_SIZE = int(os.environ.get("DATABASE_POOL_MIN_SIZE", "1"))
DATABASE_POOL_MAX_SIZE = int(os.environ.get("DATABASE_POOL_MAX_SIZE", "10"))

def get_credentials(self) -> AppCredentials:
if self.APP_PASSWORD:
Expand All @@ -49,3 +53,6 @@ def get_credentials(self) -> AppCredentials:
certificate_thumbprint=cert_thumbprint,
certificate_private_key=privkey,
)


config = DefaultConfig()
16 changes: 11 additions & 5 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import asyncpg.connect_utils

import config
from config import config
from config import DefaultConfig

log = logging.getLogger(__name__)

Expand All @@ -24,16 +25,21 @@ def __init__(


class DatabaseLifecycleHandler:
def __init__(self, dsn: str):
def __init__(
self,
conf: DefaultConfig,
):
self._pool: asyncpg.Pool | None = None
self.dsn = dsn
self._config: DefaultConfig = conf

async def connect(self):
log.debug("connecting to database")
self._pool = await asyncpg.create_pool(
dsn=self.dsn,
dsn=self._config.DATABASE_URL,
server_settings={"application_name": "notiteams-activity-api"},
connection_class=NoResetConnection,
min_size=self._config.DATABASE_POOL_MIN_SIZE,
max_size=self._config.DATABASE_POOL_MAX_SIZE,
)

# Simple check at startup, will validate database resolution and creds
Expand All @@ -49,4 +55,4 @@ async def acquire(self) -> asyncpg.pool.PoolAcquireContext:
return self._pool.acquire()


database = DatabaseLifecycleHandler(config.DefaultConfig.DATABASE_URL)
database = DatabaseLifecycleHandler(config)

0 comments on commit ec886ea

Please sign in to comment.