This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
6 changed files
with
94 additions
and
85 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 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 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 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,15 +1,18 @@ | ||
from threading import Semaphore | ||
from typing import Callable | ||
from __future__ import annotations | ||
|
||
from catalystwan.response import ManagerResponse | ||
from contextlib import AbstractContextManager | ||
from threading import Semaphore | ||
|
||
|
||
class RequestLimiter: | ||
def __init__(self, max_requests: int = 60): | ||
self.max_requests: int = max_requests | ||
self._semaphore = Semaphore(value=self.max_requests) | ||
class RequestLimiter(AbstractContextManager): | ||
def __init__(self, max_requests: int = 49): | ||
self._max_requests: int = max_requests | ||
self._semaphore: Semaphore = Semaphore(value=self._max_requests) | ||
|
||
def __enter__(self) -> RequestLimiter: | ||
self._semaphore.acquire() | ||
return self | ||
|
||
def send_request(self, delayed_request: Callable[[], ManagerResponse]) -> ManagerResponse: | ||
with self._semaphore: | ||
result = delayed_request() | ||
return result | ||
def __exit__(self, *exc_info) -> None: | ||
self._semaphore.release() | ||
return |
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 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