Skip to content

Commit

Permalink
chore(deps): upgrades pyrate-limiter (#1653)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjvankampen committed Feb 21, 2024
1 parent 307f53f commit b61941b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies = [
"gql[requests,websockets] >= 3.5.0b5, < 4.0.0",
"filelock >= 3.0.0, < 4.0.0",
"pip-system-certs >= 4.0.0, < 5.0.0; platform_system=='Windows'",
"pyrate-limiter >= 2, < 3",
"pyrate-limiter >= 3, < 4",
]
urls = { homepage = "https://github.com/kili-technology/kili-python-sdk" }

Expand Down
7 changes: 4 additions & 3 deletions src/kili/core/graphql/graphql_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from gql.transport.requests import RequestsHTTPTransport
from gql.transport.requests import log as gql_requests_logger
from graphql import DocumentNode, print_schema
from pyrate_limiter import Duration, Limiter, RequestRate
from pyrate_limiter import Duration, Limiter, Rate
from tenacity import (
retry,
retry_all,
Expand All @@ -39,7 +39,7 @@
# they need to be shared between all instances of Kili client within the same process

# rate limiter to avoid sending too many queries to the backend
_limiter = Limiter(RequestRate(MAX_CALLS_PER_MINUTE, Duration.MINUTE))
_limiter = Limiter(Rate(MAX_CALLS_PER_MINUTE, Duration.MINUTE), max_delay=120 * 1000)

# mutex to avoid multiple threads sending queries to the backend at the same time
_execute_lock = threading.Lock()
Expand Down Expand Up @@ -303,7 +303,8 @@ def _execute_with_retries(
def _raw_execute(
self, document: DocumentNode, variables: Optional[Dict], **kwargs
) -> Dict[str, Any]:
with _limiter.ratelimit("GraphQLClient.execute", delay=True), _execute_lock:
_limiter.try_acquire("GraphQLClient.execute")
with _execute_lock:
return self._gql_client.execute(
document=document,
variable_values=variables,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_graphql_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pytest_mock
from gql import Client
from gql.transport import exceptions
from pyrate_limiter import Duration, Limiter, RequestRate
from pyrate_limiter import Duration, Limiter, Rate

from kili.adapters.http_client import HttpClient
from kili.core.constants import MAX_CALLS_PER_MINUTE
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_rate_limiting(mocker: pytest_mock.MockerFixture):
mocker.patch("kili.core.graphql.graphql_client.gql", side_effect=lambda x: x)
mocker.patch(
"kili.core.graphql.graphql_client._limiter",
new=Limiter(RequestRate(MAX_CALLS_PER_MINUTE, Duration.SECOND * 5)),
new=Limiter(Rate(MAX_CALLS_PER_MINUTE, Duration.SECOND * 5), max_delay=120 * 1000),
)
client = GraphQLClient(
endpoint="",
Expand Down

0 comments on commit b61941b

Please sign in to comment.