Closed epsil closed 7 years ago
One option would be to have this built in, but we tried to keep the library as light as possible. What I have recommended in the past is to use something like https://github.com/JMPerez/promise-throttle. You can see an example of the library used together with the client-side Spotify JS wrapper on https://github.com/JMPerez/spotify-dedup/blob/b6091581e3700ccbb1e5a0e26dbb59422fa3d15f/app/scripts/main.js#L80.
Is there a way to throttle HTTP requests?
I note that this project depends on superagent in order to make HTTP requests. superagent has a plugin, superagent-throttle, which allows one to adjust how many requests can be sent in a given time interval.
Off the bat, there are a number of implementation possibilities:
The motivation for throttling is that Spotify's web API employs rate limits. The documentation doesn't say much about what these limits are, but describes them as follows:
If one oversteps one's bounds, one may receive an error like the following:
To offer a practical example: in my own project, a Spotify playlist generator, I access the web API rather frequently. To avoid overload, I currently wrap each HTTP request in a
setTimeout()
call with a delay set to 100 ms. I would very much like to make the jump to spotify-web-api-node and leave my own implementation behind, but I need to make sure that my request-intensive code will not cause problems down the line.This might be related to issue #140.