Skip to content

feat(675): Add retry strategy support for all API calls made to Facebook. #691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

PacificGilly
Copy link

Related to #675, there are occasions where an API call will fail due to server-side issues. It's common practice to retry API calls when they're safe, e.g., GET or POST-type calls with idempotency. Also if too many requests are being made and a 429 is being raised, we want to re-send the request again in a safe manner at the correct time, according to any HTTP headers.

So, adding in a Retry strategy that works well with the FacebookSession class and is written in a consistent form to other features you can specify, like API_VERSION or Timeout.

To make it easier for end users to use this feature, there is a default Retry strategy that can be toggled on via the Ads App Config settings. This default strategy is also partially customisable. Finally, advanced users can specify their own Retry strategy from scratch, but all require the Retry class from urllib3.

This is needed since the addition of a Retry strategy which is provided by the `urllib3` library. This supports both PY2 (if we're really honestly still supporting this) and PY3 and was added since v1.9.0, so just pinning this to that version just in case someone is using an ancient (11+ years old) version of urllib3.
To provide consistency with how an API call can be configured in this library, wanted to add the ability to configure a blanket retry strategy at the config level if a user wants all API calls (not POST etc.) to have a retry strategy.
The alternative way to handle retries is to specify your own at the session level. This provides full control over how to do retries, but more at the owners own risk (as we've already provided them with a default solution).
Add in the injection point to the FacebookAdsApi to allow manually passing in a custom retry strategy which is then passed into the FacebookSession and finally into the requests.Session object.
Uses a new test library, responses, which allows you to better test making multiple API calls and therefore testing your Retry strategy actually works.
@mamo3gr
Copy link

mamo3gr commented Apr 2, 2025

@stcheng @satwikareddy3 @nestorhdz-meta @shuaiwa-meta @cnukaus @rodrigobeckmann (contributors in 12 months. sorry for noisy broadcasting)
This PR is great and would save many users, including me. Would you mind reviewing, or redirecting to this request to the proper persons?

@facebook-github-bot
Copy link

@stcheng has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@mamo3gr
Copy link

mamo3gr commented Jun 15, 2025

@stcheng has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@stcheng Thank you for importing this pull request. There seems to be something wrong the internal tests but we can't see the reports. If it does matter to merge, please deal with the error or show us the report. Otherwise, please close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants