Open devonzara opened 4 months ago
You can (and very likely should) use contexts to achieve the desired behaviour.
What I did was something like this:
timeoutCtx, cancel := context.WithDeadline(parentCtx, time.Now().Add(30*time.Second))
artist, err := client.GetArtist(timeoutCtx, spotify.ID(event.SpotifyID))
cancel()
Then you can handle the error, which - when using withRetry - should be the latest error the client received for this call.
If you're very fancy you can then block any request to the Spotify API with the client id/secret combo. In my case I have a global service orchestrating things. I forked and played a bit with this package, exposing the retry-after header, and set a value in redis that marks the Spotify API as "invalid" until a specific RFC339 timestamp.
There's sadly no great way around this if you want up-to-date Data in a cached fashion.
In my case, I am requesting Spotify's API once every 24 hours for artists my users have interest in to cache them and do some checks (does the artist have a new release, do I need to update their profile picture ect), and once a Month for artists without any interest from my userbase.
I still get this issue with just ~10 active test users from time to time due to how much Spotify limits in-development Apps.
The method with context is not ideal, the program will be blocking for 30 seconds when it could already exit (return error). You can know the time of the Retry-After
header, you don't have to wait. It would not be that hard to implement a max retry time for the client. I will have a go at it, if this is a feature the maintainers would allow.
Situation During the early stages of development, before getting around to implementing caching & API throttling, I inadvertently triggered a re-render loop on the frontend. This caused an excessive number of requests to the backend, which in turn spammed the Spotify API.
Consequently, my app was suspended for approximately 24 hours and I began receiving the following responses:
Notice that the
retry-after
header is74623
and the response body is not in JSON format as expected. This causeszmb3/spotify
to throw a "spotify: couldn't decode error: (17) [Too many requests]" error.Suggestions
maxWait
(also suggested in #241) or similar mechanism for retries.retry-after
header value so the application can decide how to handle the situation.