Skip to content

Commit

Permalink
add new settings to config
Browse files Browse the repository at this point in the history
  • Loading branch information
schelv committed Jan 11, 2024
1 parent 750cf34 commit 6e8a1a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions githubkit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Config:
follow_redirects: bool
timeout: httpx.Timeout
http_cache: bool
max_nr_concurrent_requests: int
max_nr_rate_limit_retry_attempts: int

def dict(self) -> Dict[str, Any]:
return asdict(self)
Expand Down Expand Up @@ -67,6 +69,8 @@ def get_config(
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
max_nr_concurrent_requests: int = 100,
max_nr_rate_limit_retry_attempts: int = 3,
) -> Config:
return Config(
build_base_url(base_url),
Expand All @@ -75,4 +79,6 @@ def get_config(
follow_redirects,
build_timeout(timeout),
http_cache,
max_nr_concurrent_requests,
max_nr_rate_limit_retry_attempts
)
27 changes: 19 additions & 8 deletions githubkit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def __init__(
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
max_nr_concurrent_requests: int = 100,
max_nr_rate_limit_retry_attempts: int = 3,
):
...

Expand All @@ -106,6 +108,8 @@ def __init__(
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
max_nr_concurrent_requests: int = 100,
max_nr_rate_limit_retry_attempts: int = 3,
):
...

Expand All @@ -122,6 +126,8 @@ def __init__(
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
max_nr_concurrent_requests: int = 100,
max_nr_rate_limit_retry_attempts: int = 3,
):
...

Expand All @@ -138,6 +144,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
max_nr_concurrent_requests: int = 100,
max_nr_rate_limit_retry_attempts: int = 3,
):
auth = auth or UnauthAuthStrategy() # type: ignore
self.auth: A = ( # type: ignore
Expand All @@ -152,12 +159,14 @@ def __init__(
follow_redirects,
timeout,
http_cache,
max_nr_concurrent_requests,
max_nr_rate_limit_retry_attempts
)

self.rate_limit_free = Event()
self.rate_limit_free.set()

self.concurrent_request_semaphore = BoundedSemaphore(max_nr_concurrent_requests)
self.concurrent_request_semaphore = BoundedSemaphore(self.config.max_nr_concurrent_requests)

self.__sync_client: ContextVar[Optional[httpx.Client]] = ContextVar(
"sync_client", default=None
Expand Down Expand Up @@ -464,22 +473,24 @@ async def arequest(
return self._check(raw_resp, response_model, error_models)
except RateLimitExceeded as error:

Check warning on line 474 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L474

Added line #L474 was not covered by tests
# block all new requests and try again.
if retry_attempt_nr > 3:
if retry_attempt_nr > self.config.max_nr_rate_limit_retry_attempts:
raise error

Check warning on line 477 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L476-L477

Added lines #L476 - L477 were not covered by tests

start_time = datetime.now()
rate_limit_duration = error.retry_after

Check warning on line 480 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L479-L480

Added lines #L479 - L480 were not covered by tests

# print(f"retry request for {url} after {rate_limit_duration} seconds.")
print(
f"Started rate limit timer at {start_time} for {rate_limit_duration} seconds."
)
# print(
# f"Started rate limit timer at {start_time} for
# {rate_limit_duration} seconds."
# )
self.rate_limit_free.clear()
await sleep(error.retry_after.seconds)
self.rate_limit_free.set()

Check warning on line 489 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L487-L489

Added lines #L487 - L489 were not covered by tests
print(
f"rate limit that started at {start_time} stopped at {datetime.now()}"
)
# print(
# f"rate limit that started at {start_time} stopped
# at {datetime.now()}"
# )

return await self.arequest(

Check warning on line 495 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L495

Added line #L495 was not covered by tests
method,
Expand Down

0 comments on commit 6e8a1a1

Please sign in to comment.