From b976234f8c4e037ab845ea4df065e6fdcc9a7e20 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Tue, 16 Nov 2021 16:59:50 -0700 Subject: [PATCH] Fix incorrect padding in the access token expiration datetime --- simplipy/api.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/simplipy/api.py b/simplipy/api.py index 5e063d4b..dbe49bae 100644 --- a/simplipy/api.py +++ b/simplipy/api.py @@ -39,10 +39,11 @@ def get_expiration_datetime(expires_in_seconds: int) -> datetime: """Get a token expiration datetime as an offset of UTC now + a number of seconds. - Note that we pad the value to ensure the token doesn't expire without us knowing. + Note that we add some padding to the datetime. This ensures that multiple tasks + attempting to refresh within microseconds of one another are turned away. """ return datetime.utcnow() + ( - timedelta(seconds=expires_in_seconds - DEFAULT_EXPIRATION_PADDING) + timedelta(seconds=expires_in_seconds + DEFAULT_EXPIRATION_PADDING) ) @@ -175,7 +176,7 @@ async def _async_handle_on_backoff(self, _: dict[str, Any]) -> None: # been refreshed within the expiration window (and we lock the attempt so # other requests can't try it at the same time): async with self._backoff_refresh_lock: - if datetime.utcnow() < self._access_token_expire_dt: + if datetime.utcnow() <= self._access_token_expire_dt: return LOGGER.info("401 detected; attempting refresh token")