Skip to content

Commit 51dfd24

Browse files
authored
Merge pull request lightspeed-core#942 from tisnik/lcore-1137-updated-docstrings-in-quota-package
LCORE-1137: updated docstrings in quota package
2 parents cd795b3 + c622634 commit 51dfd24

4 files changed

Lines changed: 258 additions & 23 deletions

File tree

src/quota/quota_exceed_error.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,22 @@ class QuotaExceedError(Exception):
99
def __init__(
1010
self, subject_id: str, subject_type: str, available: int, needed: int = 0
1111
) -> None:
12-
"""Construct exception object."""
12+
"""Construct exception object.
13+
14+
Initialize the QuotaExceedError with the subject identity and token counts.
15+
16+
Parameters:
17+
subject_id (str): Identifier of the subject (user id or cluster id).
18+
subject_type (str): Subject kind: "u" for user, "c" for cluster,
19+
any other value treated as unknown.
20+
available (int): Number of tokens currently available to the subject.
21+
needed (int): Number of tokens required; defaults to 0.
22+
23+
Attributes:
24+
subject_id (str): Copied from the `subject_id` parameter.
25+
available (int): Copied from the `available` parameter.
26+
needed (int): Copied from the `needed` parameter.
27+
"""
1328
message: str = ""
1429

1530
if needed == 0 and available <= 0:

src/quota/quota_limiter.py

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,37 @@ class QuotaLimiter(ABC):
5151

5252
@abstractmethod
5353
def available_quota(self, subject_id: str) -> int:
54-
"""Retrieve available quota for given user."""
54+
"""Retrieve available quota for given user.
55+
56+
Get the remaining quota for the specified subject.
57+
58+
Parameters:
59+
subject_id (str): Identifier of the subject (user or service) whose quota to retrieve.
60+
61+
Returns:
62+
available_quota (int): Number of quota units currently available for the subject.
63+
"""
5564

5665
@abstractmethod
5766
def revoke_quota(self) -> None:
58-
"""Revoke quota for given user."""
67+
"""Revoke quota for given user.
68+
69+
Revoke the quota for the limiter's target subject by setting its available quota to zero.
70+
71+
This operation removes or disables any remaining allowance so
72+
subsequent checks will report no available quota.
73+
"""
5974

6075
@abstractmethod
6176
def increase_quota(self) -> None:
62-
"""Increase quota for given user."""
77+
"""Increase quota for given user.
78+
79+
Increase the available quota for the limiter's subject according to its
80+
configured increase policy.
81+
82+
Updates persistent storage to add the configured quota increment to the
83+
subject's stored available quota.
84+
"""
6385

6486
@abstractmethod
6587
def ensure_available_quota(self, subject_id: str = "") -> None:
@@ -69,23 +91,59 @@ def ensure_available_quota(self, subject_id: str = "") -> None:
6991
def consume_tokens(
7092
self, input_tokens: int, output_tokens: int, subject_id: str = ""
7193
) -> None:
72-
"""Consume tokens by given user."""
94+
"""Consume tokens by given user.
95+
96+
Consume the specified input and output tokens from a subject's available quota.
97+
98+
Parameters:
99+
input_tokens (int): Number of input tokens to deduct from the subject's quota.
100+
output_tokens (int): Number of output tokens to deduct from the subject's quota.
101+
subject_id (str): Identifier of the subject (user or service) whose
102+
quota will be reduced. If omitted, applies to the default subject.
103+
"""
73104

74105
@abstractmethod
75106
def __init__(self) -> None:
76-
"""Initialize connection configuration(s)."""
107+
"""Initialize connection configuration(s).
108+
109+
Create a QuotaLimiter instance and initialize database connection configuration attributes.
110+
111+
Attributes:
112+
sqlite_connection_config (Optional[SQLiteDatabaseConfiguration]):
113+
SQLite connection configuration or `None` when not configured.
114+
postgres_connection_config
115+
(Optional[PostgreSQLDatabaseConfiguration]): PostgreSQL connection
116+
configuration or `None` when not configured.
117+
"""
77118
self.sqlite_connection_config: Optional[SQLiteDatabaseConfiguration] = None
78119
self.postgres_connection_config: Optional[PostgreSQLDatabaseConfiguration] = (
79120
None
80121
)
81122

82123
@abstractmethod
83124
def _initialize_tables(self) -> None:
84-
"""Initialize tables and indexes."""
125+
"""Initialize tables and indexes.
126+
127+
Create any database tables and indexes required by the quota limiter implementation.
128+
129+
Implementations must ensure the database schema and indexes needed for
130+
storing and querying quota state exist; calling this method when the
131+
schema already exists should be safe (idempotent). Raise an exception
132+
on irrecoverable initialization failures.
133+
"""
85134

86135
# pylint: disable=W0201
87136
def connect(self) -> None:
88-
"""Initialize connection to database."""
137+
"""Initialize connection to database.
138+
139+
Establish the configured database connection, initialize required
140+
tables, and enable autocommit.
141+
142+
If a PostgreSQL or SQLite configuration is present, a connection to
143+
that backend will be created, then _initialize_tables() will be called
144+
to prepare storage. If table initialization fails, the connection is
145+
closed and the original exception is propagated.
146+
"""
89147
logger.info("Initializing connection to quota limiter database")
90148
if self.postgres_connection_config is not None:
91149
self.connection = connect_pg(self.postgres_connection_config)
@@ -102,7 +160,13 @@ def connect(self) -> None:
102160
self.connection.autocommit = True
103161

104162
def connected(self) -> bool:
105-
"""Check if connection to quota limiter database is alive."""
163+
"""Check if connection to quota limiter database is alive.
164+
165+
Determine whether the storage connection is alive.
166+
167+
Returns:
168+
`true` if the connection is alive, `false` otherwise.
169+
"""
106170
if self.connection is None:
107171
logger.warning("Not connected, need to reconnect later")
108172
return False

src/quota/quota_limiter_factory.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ class QuotaLimiterFactory:
2121
def quota_limiters(config: QuotaHandlersConfiguration) -> list[QuotaLimiter]:
2222
"""Create instances of quota limiters based on loaded configuration.
2323
24+
Parameters:
25+
config (QuotaHandlersConfiguration): Configuration containing
26+
storage settings and limiter definitions.
27+
2428
Returns:
25-
List of instances of 'QuotaLimiter',
29+
list[QuotaLimiter]: List of initialized quota limiter instances.
30+
Returns an empty list if storage configuration or limiter
31+
definitions are missing.
2632
"""
2733
limiters: list[QuotaLimiter] = []
2834

@@ -56,7 +62,24 @@ def create_limiter(
5662
initial_quota: int,
5763
increase_by: int,
5864
) -> QuotaLimiter:
59-
"""Create selected quota limiter."""
65+
"""Create selected quota limiter.
66+
67+
Instantiate a quota limiter instance for the given limiter type.
68+
69+
Parameters:
70+
configuration (QuotaHandlersConfiguration): Configuration used to
71+
initialize the limiter.
72+
limiter_type (str): Identifier of the limiter to create; expected values are
73+
`constants.USER_QUOTA_LIMITER` or `constants.CLUSTER_QUOTA_LIMITER`.
74+
initial_quota (int): Starting quota value assigned to the limiter.
75+
increase_by (int): Amount by which the quota increases when replenished.
76+
77+
Returns:
78+
QuotaLimiter: A configured quota limiter instance of the requested type.
79+
80+
Raises:
81+
ValueError: If `limiter_type` is not a recognized limiter identifier.
82+
"""
6083
match limiter_type:
6184
case constants.USER_QUOTA_LIMITER:
6285
return UserQuotaLimiter(configuration, initial_quota, increase_by)

0 commit comments

Comments
 (0)