Skip to content

Commit

Permalink
Rate limiting added to core client (#315)
Browse files Browse the repository at this point in the history
* Rate limiting added to core client

* Ratelimit added to reqs
  • Loading branch information
munrojm committed Jul 4, 2021
1 parent 8f9c178 commit 1a895a6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ maggma==0.29.4
requests==2.25.1
monty==2021.6.10
emmet-core==0.4.1
ratelimit==2.2.1
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"monty",
"emmet-core",
"maggma",
"ratelimit",
],
extras_require={
"server": [
Expand Down
6 changes: 6 additions & 0 deletions src/mp_api/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from emmet.core.utils import jsanitize
from maggma.api.utils import api_sanitize

from mp_api.core.ratelimit import check_limit

try:
from pymatgen.core import __version__ as pmg_version # type: ignore
except ImportError: # pragma: no cover
Expand Down Expand Up @@ -148,6 +150,8 @@ def _post_resource(
available.
"""

check_limit()

payload = jsanitize(body)

try:
Expand Down Expand Up @@ -223,6 +227,8 @@ def _query_resource(
available.
"""

check_limit()

if criteria:
criteria = {k: v for k, v in criteria.items() if v is not None}
else:
Expand Down
11 changes: 11 additions & 0 deletions src/mp_api/core/ratelimit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ratelimit import sleep_and_retry, limits
from mp_api.core.settings import MAPISettings


@sleep_and_retry
@limits(calls=MAPISettings().requests_per_min, period=60)
def check_limit():
"""
Empty function for enabling global rate limiting.
"""
return
4 changes: 4 additions & 0 deletions src/mp_api/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ class MAPISettings(BaseSettings):

db_version: str = Field("2021_04_26", description="Database version")

requests_per_min: int = Field(
60, description="Number of requests per minute to for rate limit."
)

class Config:
env_prefix = "mapi_"

0 comments on commit 1a895a6

Please sign in to comment.