Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: connection to DB #401

Merged
merged 1 commit into from
Feb 20, 2025
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
19 changes: 19 additions & 0 deletions ols/src/quota/quota_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from abc import ABC, abstractmethod

import psycopg2

from ols.app.models.config import PostgresConfig


class QuotaLimiter(ABC):
"""Abstract class that is parent for all quota limiter implementations."""
Expand All @@ -21,3 +25,18 @@ def increase_quota(self) -> None:
@abstractmethod
def consume_tokens(self, input_tokens: int, output_tokens: int) -> None:
"""Consume tokens by given user."""

# pylint: disable=W0201
def connect(self, config: PostgresConfig) -> None:
"""Initialize connection to database."""
self.connection = psycopg2.connect(
host=config.host,
port=config.port,
user=config.user,
password=config.password,
dbname=config.dbname,
sslmode=config.ssl_mode,
# sslrootcert=config.ca_cert_path,
gssencmode=config.gss_encmode,
)
self.connection.autocommit = True
15 changes: 2 additions & 13 deletions ols/src/quota/user_quota_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import logging
from datetime import datetime

import psycopg2

from ols.app.models.config import PostgresConfig
from ols.src.quota.quota_exceed_error import QuotaExceedError
from ols.src.quota.quota_limiter import QuotaLimiter
Expand Down Expand Up @@ -57,17 +55,8 @@ def __init__(
self.increase_by = increase_by

# initialize connection to DB
self.connection = psycopg2.connect(
host=config.host,
port=config.port,
user=config.user,
password=config.password,
dbname=config.dbname,
sslmode=config.ssl_mode,
sslrootcert=config.ca_cert_path,
gssencmode=config.gss_encmode,
)
self.connection.autocommit = True
self.connect(config)

try:
self._initialize_tables()
except Exception as e:
Expand Down
Loading