-
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
Merged
alafanechere
merged 17 commits into
airbytehq:master
from
vladimir-remar:source_facebook_marketing_improve_rate_limit_sleeps
Mar 25, 2022
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e56e28a
update: improve sleeps time in rate limit handle caller
vladimir-remar 4a24396
update: improve sleep time calculation
vladimir-remar e924de1
Merge branch 'master' into source_facebook_marketing_improve_rate_lim…
vladimir-remar d17c6e8
update connector version in source_definitions and docs
vladimir-remar 67e83b2
fix conflicts
vladimir-remar 3dba30e
fix conflicts
vladimir-remar 5e90b3c
update: improve handle_call_rate_limit
vladimir-remar 3505db1
fix conflitcs
vladimir-remar 0f5a9e4
update Dockerfile
vladimir-remar 778bf52
fix: formating
vladimir-remar 6f24d62
fix: sleep time in seconds
vladimir-remar 905e556
Merge branch 'master' into source_facebook_marketing_improve_rate_lim…
alafanechere c768b2c
unit tests
alafanechere 1fa8aa6
fix _compute_pause_interval add comparison with time interval
vladimir-remar 913abac
change unit test according to change
alafanechere 29a8330
Merge branch 'master' into source_facebook_marketing_improve_rate_lim…
alafanechere 263922b
update source_specs and source_definitions
alafanechere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -30,8 +30,8 @@ class FacebookAPIException(Exception): | |||||||||||||||
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 | ||||||||||||||||
pause_interval_minimum = pendulum.duration(minutes=1) # default pause interval if reached or close to call rate limit | ||||||||||||||||
MAX_RATE, MAX_PAUSE_INTERVAL = (95, pendulum.duration(minutes=5)) | ||||||||||||||||
MIN_RATE, MIN_PAUSE_INTERVAL = (90, pendulum.duration(minutes=1)) | ||||||||||||||||
|
||||||||||||||||
@dataclass | ||||||||||||||||
class Throttle: | ||||||||||||||||
|
@@ -87,11 +87,17 @@ def _parse_call_rate_header(headers): | |||||||||||||||
|
||||||||||||||||
return usage, pause_interval | ||||||||||||||||
|
||||||||||||||||
def compute_pause_interval(self, usage, pause_interval): | ||||||||||||||||
"""The sleep time will be calculated based on usage consumed.""" | ||||||||||||||||
if usage >= self.MAX_RATE: | ||||||||||||||||
return max(self.MAX_PAUSE_INTERVAL, pause_interval) | ||||||||||||||||
return self.MIN_PAUSE_INTERVAL | ||||||||||||||||
|
||||||||||||||||
def handle_call_rate_limit(self, response, params): | ||||||||||||||||
if "batch" in params: | ||||||||||||||||
max_usage = 0 | ||||||||||||||||
max_pause_interval = self.pause_interval_minimum | ||||||||||||||||
usage = 0 | ||||||||||||||||
pause_interval = self.MIN_PAUSE_INTERVAL | ||||||||||||||||
|
||||||||||||||||
if "batch" in params: | ||||||||||||||||
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. nit:
Suggested change
|
||||||||||||||||
for record in response.json(): | ||||||||||||||||
# there are two types of failures: | ||||||||||||||||
# 1. no response (we execute batch until all inner requests has response) | ||||||||||||||||
|
@@ -100,21 +106,17 @@ def handle_call_rate_limit(self, response, params): | |||||||||||||||
if "headers" not in record: | ||||||||||||||||
continue | ||||||||||||||||
headers = {header["name"].lower(): header["value"] for header in record["headers"]} | ||||||||||||||||
usage, pause_interval = self._parse_call_rate_header(headers) | ||||||||||||||||
max_usage = max(max_usage, usage) | ||||||||||||||||
max_pause_interval = max(max_pause_interval, pause_interval) | ||||||||||||||||
|
||||||||||||||||
if 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()) | ||||||||||||||||
usage_from_response, pause_interval_from_response = self._parse_call_rate_header(headers) | ||||||||||||||||
usage = max(usage, usage_from_response) | ||||||||||||||||
pause_interval = max(pause_interval_from_response, pause_interval) | ||||||||||||||||
else: | ||||||||||||||||
headers = response.headers() | ||||||||||||||||
usage, pause_interval = self._parse_call_rate_header(headers) | ||||||||||||||||
if usage > self.call_rate_threshold or pause_interval: | ||||||||||||||||
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()) | ||||||||||||||||
|
||||||||||||||||
if usage >= self.MIN_RATE: | ||||||||||||||||
sleep_time = self.compute_pause_interval(usage=usage, pause_interval=pause_interval) | ||||||||||||||||
logger.warning(f"Utilization is too high ({usage})%, pausing for {sleep_time}") | ||||||||||||||||
sleep(sleep_time).total_seconds() | ||||||||||||||||
vladimir-remar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
def _update_insights_throttle_limit(self, response: FacebookResponse): | ||||||||||||||||
""" | ||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the usage is lower than the max rate but the pause interval is higher than the default minimum one: don't we want to be conservative and pause for the time the API requests?
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.
for sure, the idea is to pause the requests taking into account the time interval that the response returns.