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

Print warnings when a rate/request limit is reached #1134

Merged
merged 11 commits into from
Jun 25, 2024
Merged

Conversation

dieser-niko
Copy link
Member

@dieser-niko dieser-niko commented Jun 11, 2024

I'm writing this sentence a fifth time. Does what the title says.

This is still a draft because I'm researching how to implement this so we can get the retry-after value right away.
Currently it only prints a warning (whose text could be improved as well)

@dieser-niko
Copy link
Member Author

dieser-niko commented Jun 11, 2024

After going a frame up, I found that Retry.increment was getting the full response object, including the header.
Increment is used in several places (urllib3.connectionpool.HTTPConnectionPool.urlopen). The first one is on a redirect.

It might actually be important that we don't check for the Retry-After header until the second stage (when we actually check if it's a retry).
I can't think of a better way than to override Retry.increment, where I'll check with Retry.is_retry whether it's a retry or not.

I feel that it can still fail, but the worst case would be a double entry in the logs, which would require a very niche setting for that to happen.

@dieser-niko
Copy link
Member Author

If anyone wants to try this for themselves, just install

git+https://github.com/spotipy-dev/spotipy@retry-warning

I use this snippet to get a persistent rate limit after a few seconds:

import time

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import threading

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id="SPOTIPY_CLIENT_ID",
                                                           client_secret="SPOTIPY_CLIENT_SECRET"))


def do():
    sp.album_tracks("2Kh43m04B1UkVcpcRa1Zug")


while True:
    print(time.time())
    threading.Thread(target=do, daemon=True).start()
    time.sleep(0.01)

If you wait long enough, you'll probably get a request limit too, but nothing will change except the wait time.

@dieser-niko dieser-niko marked this pull request as ready for review June 15, 2024 21:34
@dieser-niko dieser-niko merged commit 66ad716 into master Jun 25, 2024
12 checks passed
@dieser-niko dieser-niko deleted the retry-warning branch June 25, 2024 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants