Skip to content

Commit

Permalink
Small edit to add backoff_factor to client (#759)
Browse files Browse the repository at this point in the history
* Add backoff_factor

As defined in `urllib3` docs

* Update `Retry` to use `backoff_factor`

* Fix typo

* Fix linting

---------

Co-authored-by: Jason Munro <jmunro@lbl.gov>
  • Loading branch information
mkhorton and Jason Munro authored Mar 17, 2023
1 parent 858409f commit dd9c5db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mp_api/client/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ def _create_session(api_key, include_user_agent, headers):
platform_info = f"{platform.system()}/{platform.release()}"
session.headers["user-agent"] = f"{pymatgen_info} ({python_info} {platform_info})"

max_retry_num = MAPIClientSettings().MAX_RETRIES
settings = MAPIClientSettings()
max_retry_num = settings.MAX_RETRIES
retry = Retry(
total=max_retry_num,
read=max_retry_num,
connect=max_retry_num,
respect_retry_after_header=True,
status_forcelist=[429], # rate limiting
backoff_factor=settings.BACKOFF_FACTOR
)
adapter = HTTPAdapter(max_retries=retry)
session.mount("http://", adapter)
Expand Down
5 changes: 5 additions & 0 deletions mp_api/client/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class MAPIClientSettings(BaseSettings):
_MAX_RETRIES, description="Maximum number of retries for requests."
)

BACKOFF_FACTOR: float = Field(
0.1,
description="A backoff factor to apply between retry attempts. To disable, set to 0.",
)

MUTE_PROGRESS_BARS: bool = Field(
_MUTE_PROGRESS_BAR,
description="Whether to mute progress bars when data is retrieved.",
Expand Down

0 comments on commit dd9c5db

Please sign in to comment.