Skip to content

Commit

Permalink
Merge pull request #294 from openedx/alangsto/fix-rate-limiting
Browse files Browse the repository at this point in the history
fix: handle rate limiting when retrieving enterprise customer details
  • Loading branch information
alangsto committed Mar 16, 2022
2 parents 2570f9d + ddbc347 commit 06de1c6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Unreleased
----------

=========================
[4.2.2] - 2022-03-16
---------------------
* Update error handling for rate limit exceptions. Moved handling to source of errors.

[4.2.1] - 2022-03-15
---------------------
* Added error handling for rate limit exceptions
Expand Down
2 changes: 1 addition & 1 deletion enterprise_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Enterprise data api application. This Django app exposes API endpoints used by enterprises.
"""

__version__ = "4.2.1"
__version__ = "4.2.2"

default_app_config = "enterprise_data.apps.EnterpriseDataAppConfig" # pylint: disable=invalid-name
24 changes: 17 additions & 7 deletions enterprise_data/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


import logging
import time

from edx_django_utils.cache import TieredCache
from edx_rest_api_client.client import EdxRestApiClient
Expand Down Expand Up @@ -84,16 +85,25 @@ def get_enterprise_customer(self, user, enterprise_id):
if cached_response.is_found:
return cached_response.value

endpoint = getattr(self, 'enterprise-customer')
endpoint = endpoint(enterprise_id)

try:
endpoint = getattr(self, 'enterprise-customer')
endpoint = endpoint(enterprise_id)
response = endpoint.get()
except (HttpClientError, HttpServerError) as exc:
LOGGER.warning(
"[Data Overview Failure] Unable to retrieve Enterprise Customer details. "
f"User: {user.username}, Enterprise: {enterprise_id}, Exception: {exc}"
)
raise exc
if exc.response.status_code == 429:
LOGGER.info(
"[Data Overview Info] Rate limiting encountered while retrieving Enterprise Customer details, "
"will try again in 10 seconds."
)
time.sleep(10)
response = endpoint.get()
else:
LOGGER.warning(
"[Data Overview Failure] Unable to retrieve Enterprise Customer details. "
f"User: {user.username}, Enterprise: {enterprise_id}, Exception: {exc}"
)
raise exc

TieredCache.set_all_tiers(cache_key, response, DEFAULT_REPORTING_CACHE_TIMEOUT)

Expand Down
13 changes: 1 addition & 12 deletions enterprise_reporting/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
from datetime import datetime
from functools import wraps
from urllib.parse import parse_qs, urlparse
import time

from edx_rest_api_client.client import EdxRestApiClient
from edx_rest_api_client.exceptions import HttpClientError


class EdxOAuth2APIClient:
Expand Down Expand Up @@ -131,16 +129,7 @@ def traverse_pagination(response, endpoint):
next_page = response.get('next')
while next_page:
querystring = parse_qs(urlparse(next_page).query, True)
try:
response = endpoint.get(**querystring)
except HttpClientError as exc:
# If we get HTTP429, we should pause for a period of time before re-requesting one more time.
# This status code is expected because of rate limiting on the endpoint
if exc.response.status_code == 429:
time.sleep(5)
response = endpoint.get(**querystring)
else:
raise exc
response = endpoint.get(**querystring)
results += response.get('results', [])
next_page = response.get('next')

Expand Down

0 comments on commit 06de1c6

Please sign in to comment.