Skip to content

Commit

Permalink
chore(async-gitlab): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-devv committed Nov 7, 2024
1 parent ffeb379 commit 00d253a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 71 deletions.
3 changes: 1 addition & 2 deletions integrations/async-gitlab/gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ async def get_resources(
resource_type: ObjectKind,
query_params: Optional[Dict[str, str]] = None
) -> AsyncGenerator[list[dict[str, Any]], None]:
limiter = await self.limiter()
async with limiter:
async with self.limiter:
async for resources in self.get_paginated_resources(
resource_type=resource_type,
query_params=query_params
Expand Down
39 changes: 0 additions & 39 deletions integrations/async-gitlab/gitlab/helpers/quota.py

This file was deleted.

39 changes: 9 additions & 30 deletions integrations/async-gitlab/gitlab/helpers/ratelimiter.py
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

0 comments on commit 00d253a

Please sign in to comment.