Skip to content
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

Automatically backoff when rate limited #38

Open
iloveitaly opened this issue Apr 5, 2022 · 2 comments
Open

Automatically backoff when rate limited #38

iloveitaly opened this issue Apr 5, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@iloveitaly
Copy link
Contributor

Enhancement description

Add option to automatically use a backoff when you've hit a rate limit.

The problem it solves

Intermittent failures due to rate limiting.

Alternatives

Re-running the script.

Use case / screenshots

I've hit 429s on this this project: https://github.com/iloveitaly/todoist-scheduler/commits/master

Additional information

@iloveitaly iloveitaly added the enhancement New feature or request label Apr 5, 2022
@iloveitaly
Copy link
Contributor Author

Ran into this again, would be great if this was built in.

@iloveitaly
Copy link
Contributor Author

Here's how I did it:

# backoff 5xx errors
def patch_todoist_api():
    import todoist_api_python.http_requests
    import backoff
    import requests

    patch_targets = ["delete", "get", "json", "post"]
    for target in patch_targets:
        original_function = getattr(todoist_api_python.http_requests, target)

        setattr(
            todoist_api_python.http_requests,
            f"original_{target}",
            original_function,
        )

        patched_function = backoff.on_exception(
            backoff.expo, requests.exceptions.HTTPError
        )(original_function)

        setattr(
            todoist_api_python.http_requests,
            target,
            patched_function,
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant