-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffeb379
commit 00d253a
Showing
3 changed files
with
10 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,14 @@ | ||
from typing import final | ||
from aiolimiter import AsyncLimiter | ||
from loguru import logger | ||
from gitlab.helpers.quota import GitLabAPIQuota | ||
|
||
_DEFAULT_RATE_LIMIT_TIME_PERIOD: float = 60.0 | ||
_PERCENTAGE_OF_QUOTA: float = 0.2 | ||
_DEFAULT_RATE_LIMIT_QUOTA: int = 60 | ||
|
||
_DEFAULT_RATE_LIMIT_TIME_PERIOD: float = 60.0 # Time period in seconds | ||
_PERCENTAGE_OF_QUOTA: float = 0.2 # Percentage of the quota to use | ||
|
||
class GitLabRateLimiter(GitLabAPIQuota): | ||
""" | ||
GitLabAPIRateLimiter manages rate limits for GitLab API requests. | ||
It inherits from GitLabAPIQuota and leverages a rate limiter to control the rate of API requests based on the specified quota. | ||
""" | ||
class GitLabRateLimiter: | ||
def __init__(self, quota: int = _DEFAULT_RATE_LIMIT_QUOTA, time_period: float = _DEFAULT_RATE_LIMIT_TIME_PERIOD): | ||
effective_quota_limit = int(max(round(quota * _PERCENTAGE_OF_QUOTA, 1), 1)) | ||
self.limiter = AsyncLimiter(max_rate=effective_quota_limit, time_period=time_period) | ||
|
||
time_period: float = _DEFAULT_RATE_LIMIT_TIME_PERIOD | ||
|
||
async def default_rate_limiter(self) -> AsyncLimiter: | ||
quota = int(max(round(self._default_quota * _PERCENTAGE_OF_QUOTA, 1), 1)) | ||
logger.info(f"Using {quota} as the rate limit for API requests.") | ||
return AsyncLimiter(max_rate=quota, time_period=self.time_period) | ||
|
||
async def register(self) -> AsyncLimiter: | ||
quota = await self._get_quota() | ||
effective_quota_limit: int = int(max(round(quota * _PERCENTAGE_OF_QUOTA, 1), 1)) | ||
logger.info(f"Effective quota limit for rate limiting: {effective_quota_limit}.") | ||
limiter = AsyncLimiter(max_rate=effective_quota_limit, time_period=self.time_period) | ||
return limiter | ||
|
||
@final | ||
def quota_name(self) -> str: | ||
return "GitLab API Rate Limit" | ||
|
||
async def limiter(self) -> AsyncLimiter: | ||
return await self.register() | ||
async def get_limiter(self) -> AsyncLimiter: | ||
return self.limiter |