-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
🎉 Source Facebook Marketing: improve sleeps time in rate limit handler #10698
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): | ||||||||||
|
||||||||||
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 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.
Show resolved
Hide resolved
|
||||||||||
max_pause_interval = max(max_pause_interval, pause_interval) | ||||||||||
vladimir-remar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
if max_usage > self.call_rate_threshold: | ||||||||||
if max_usage >= self.call_rate_threshold: | ||||||||||
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.
Suggested change
|
||||||||||
max_pause_interval = max(max_pause_interval, self.pause_interval_minimum_at_max_usage) | ||||||||||
self.sleep_time_handler(usage=max_usage, pause_interval=max_pause_interval) | ||||||||||
elif self.min_call_rate_threslold <= max_usage < self.call_rate_threshold: | ||||||||||
max_pause_interval = max(max_pause_interval, self.pause_interval_minimum) | ||||||||||
logger.warning(f"Utilization is too high ({max_usage})%, pausing for {max_pause_interval}") | ||||||||||
sleep(max_pause_interval.total_seconds()) | ||||||||||
self.sleep_time_handler(usage=max_usage, pause_interval=max_pause_interval) | ||||||||||
|
||||||||||
else: | ||||||||||
headers = response.headers() | ||||||||||
usage, pause_interval = self._parse_call_rate_header(headers) | ||||||||||
if usage > self.call_rate_threshold or pause_interval: | ||||||||||
if usage >= self.call_rate_threshold or pause_interval: | ||||||||||
pause_interval = max(pause_interval, self.pause_interval_minimum_at_max_usage) | ||||||||||
self.sleep_time_handler(usage=usage, pause_interval=pause_interval) | ||||||||||
elif self.min_call_rate_threslold <= usage < self.call_rate_threshold: | ||||||||||
pause_interval = max(pause_interval, self.pause_interval_minimum) | ||||||||||
logger.warning(f"Utilization is too high ({usage})%, pausing for {pause_interval}") | ||||||||||
sleep(pause_interval.total_seconds()) | ||||||||||
self.sleep_time_handler(usage=usage, pause_interval=pause_interval) | ||||||||||
|
||||||||||
def _update_insights_throttle_limit(self, response: FacebookResponse): | ||||||||||
""" | ||||||||||
|
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.