- Notifications
You must be signed in to change notification settings - Fork 4.9k
🎉 Source Facebook Marketing: improve sleeps time in rate limit handler #10698
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
Changes from 1 commit
e56e28a 4a24396 e924de1 d17c6e8 67e83b2 3dba30e 5e90b3c 3505db1 0f5a9e4 778bf52 6f24d62 905e556 c768b2c 1fa8aa6 913abac 29a8330 263922b File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| | @@ -31,7 +31,9 @@ class MyFacebookAdsApi(FacebookAdsApi): | |||||||||
| """Custom Facebook API class to intercept all API calls and handle call rate limits""" | ||||||||||
| | ||||||||||
| call_rate_threshold = 95 # maximum percentage of call limit utilization | ||||||||||
| min_call_rate_threslold = 90 # minimum percentage of call limit utilization | ||||||||||
| pause_interval_minimum = pendulum.duration(minutes=1) # default pause interval if reached or close to call rate limit | ||||||||||
| pause_interval_minimum_at_max_usage = pendulum.duration(minutes=5) | ||||||||||
| | ||||||||||
| @dataclass | ||||||||||
| class Throttle: | ||||||||||
| | @@ -87,6 +89,12 @@ def _parse_call_rate_header(headers): | |||||||||
| | ||||||||||
| return usage, pause_interval | ||||||||||
| | ||||||||||
| @staticmethod | ||||||||||
| def sleep_time_handler(usage, pause_interval: pendulum.duration): | ||||||||||
| | ||||||||||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the definition of the custom pause interval according to Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alafanechere Thanks, I did some changes, tell me what you think. | ||||||||||
| logger.warning(f"Utilization is too high ({usage})%, pausing for {pause_interval}") | ||||||||||
| sleep(pause_interval.total_seconds()) | ||||||||||
| | ||||||||||
| def handle_call_rate_limit(self, response, params): | ||||||||||
| if "batch" in params: | ||||||||||
| max_usage = 0 | ||||||||||
| | @@ -104,17 +112,22 @@ def handle_call_rate_limit(self, response, params): | |||||||||
| max_usage = max(max_usage, usage) | ||||||||||
vladimir-remar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||||||
| max_pause_interval = max(max_pause_interval, pause_interval) | ||||||||||
vladimir-remar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||||||
| | ||||||||||
| if max_usage > self.call_rate_threshold: | ||||||||||
| if max_usage >= self.call_rate_threshold: | ||||||||||
| ||||||||||
| if max_usage >= self.call_rate_threshold: | |
| max_rate, max_sleep_duration = self. min_max_rate_sleep_time["max"] | |
| min_rate, min_sleep_duration = self. min_max_rate_sleep_time["min"] | |
| if max_usage >= max_rate: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.