-
Notifications
You must be signed in to change notification settings - Fork 55
LCORE-739: quota limiter stub code #692
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| """Any exception that can occur when user does not have enough tokens available.""" | ||
|
|
||
| # pylint: disable=line-too-long) | ||
|
|
||
|
|
||
| class QuotaExceedError(Exception): | ||
| """Any exception that can occur when user does not have enough tokens available.""" | ||
|
|
||
| def __init__( | ||
| self, subject_id: str, subject_type: str, available: int, needed: int = 0 | ||
| ) -> None: | ||
| """Construct exception object.""" | ||
| message: str = "" | ||
|
|
||
| if needed == 0 and available <= 0: | ||
| match subject_type: | ||
| case "u": | ||
| message = f"User {subject_id} has no available tokens" | ||
| case "c": | ||
| message = "Cluster has no available tokens" | ||
| case _: | ||
| message = f"Unknown subject {subject_id} has no available tokens" | ||
| else: | ||
| match subject_type: | ||
| case "u": | ||
| message = f"User {subject_id} has {available} tokens, but {needed} tokens are needed" # noqa: E501 | ||
| case "c": | ||
| message = f"Cluster has {available} tokens, but {needed} tokens are needed" | ||
| case _: | ||
| message = f"Unknown subject {subject_id} has {available} tokens, but {needed} tokens are needed" # noqa: E501 | ||
|
|
||
| # call the base class constructor with the parameters it needs | ||
| super().__init__(message) | ||
|
|
||
| # custom attributes | ||
| self.subject_id = subject_id | ||
| self.available = available | ||
| self.needed = needed | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """Abstract class that is parent for all quota limiter implementations.""" | ||
|
|
||
| import logging | ||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class QuotaLimiter(ABC): | ||
| """Abstract class that is parent for all quota limiter implementations.""" | ||
|
|
||
| @abstractmethod | ||
| def available_quota(self, subject_id: str) -> int: | ||
| """Retrieve available quota for given user.""" | ||
|
|
||
| @abstractmethod | ||
| def revoke_quota(self) -> None: | ||
| """Revoke quota for given user.""" | ||
|
Comment on lines
+18
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align docstrings with method signatures. Both If these methods should operate on a specific subject, apply this diff: @abstractmethod
-def revoke_quota(self) -> None:
- """Revoke quota for given user."""
+def revoke_quota(self, subject_id: str = "") -> None:
+ """Revoke quota for given subject."""
@abstractmethod
-def increase_quota(self) -> None:
- """Increase quota for given user."""
+def increase_quota(self, subject_id: str = "") -> None:
+ """Increase quota for given subject."""Otherwise, update docstrings to remove the subject reference: - """Revoke quota for given user."""
+ """Revoke quota."""- """Increase quota for given user."""
+ """Increase quota."""Also applies to: 22-23 🤖 Prompt for AI Agents |
||
|
|
||
| @abstractmethod | ||
| def increase_quota(self) -> None: | ||
| """Increase quota for given user.""" | ||
|
|
||
| @abstractmethod | ||
| def ensure_available_quota(self, subject_id: str = "") -> None: | ||
| """Ensure that there's available quota left.""" | ||
|
|
||
| @abstractmethod | ||
| def consume_tokens( | ||
| self, input_tokens: int, output_tokens: int, subject_id: str = "" | ||
| ) -> None: | ||
| """Consume tokens by given user.""" | ||
|
|
||
| @abstractmethod | ||
| def __init__(self) -> None: | ||
| """Initialize connection config.""" | ||
|
|
||
| @abstractmethod | ||
| def _initialize_tables(self) -> None: | ||
| """Initialize tables and indexes.""" | ||
|
|
||
| # pylint: disable=W0201 | ||
| def connect(self) -> None: | ||
| """Initialize connection to database.""" | ||
|
|
||
| def connected(self) -> bool: | ||
| """Check if connection to cache is alive.""" | ||
| return True | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Quota limiter factory class.""" | ||
|
|
||
| import logging | ||
|
|
||
| from models.config import QuotaHandlersConfiguration | ||
|
|
||
| from quota.quota_limiter import QuotaLimiter | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # pylint: disable=too-few-public-methods | ||
|
|
||
|
|
||
| class QuotaLimiterFactory: | ||
| """Quota limiter factory class.""" | ||
|
|
||
| @staticmethod | ||
| def quota_limiters(config: QuotaHandlersConfiguration) -> list[QuotaLimiter]: | ||
| """Create instances of quota limiters based on loaded configuration. | ||
|
|
||
| Returns: | ||
| List of instances of 'QuotaLimiter', | ||
| """ | ||
| limiters: list[QuotaLimiter] = [] | ||
|
|
||
| limiters_config = config.limiters | ||
| if limiters_config is None: | ||
| logger.warning("Quota limiters are not specified in configuration") | ||
| return limiters | ||
|
|
||
| return limiters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix line length violations flagged by pipeline.
Lines 24 and 28 exceed the 100-character limit despite having
# noqa: E501comments, indicating the linter is still flagging them.Apply this diff to resolve the violations:
Also applies to: 28-28
🧰 Tools
🪛 GitHub Actions: Python linter
[error] 24-24: Pylint: Line too long (119/100).
🤖 Prompt for AI Agents