-
Notifications
You must be signed in to change notification settings - Fork 70
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
Added limts.js #74
base: master
Are you sure you want to change the base?
Added limts.js #74
Conversation
Requests are now throttled so that we won't get 429 errors (Too Many Requests) from TMDB.
Pretty cool! @rontav I'll take a look during the weekend |
Also, I would recommend that we should use an object instead of multiple arguments for passing settings, but that would require a new dependency to do it easily. |
just to add my 2 cents.. i have an app that uses multiple api's to grab metadata, and have implemented node-rate-limiter to these api's (including this one). if you decide to add this functionality, can there be a way to disable it for users who implement limiting externally? |
Lately my tests have been failing with Maybe this kind of thing is more suited to the docs than inside the lib? Here's a simple wrapper: const _tmdb = require('moviedb')('your-key')
const limits = require('limits.js')
const throttle = limits().within(1000, 3)
const tmdb = (m, q, cb) => {
throttle.push(() => _tmdb[m](q, cb))
}
tmdb('searchMovie', {query: 'The Martian', year: 2015}, (err, response) => {
if(err) return console.error(err)
console.log(response)
}) Using promises: const tmdbPromise = (m, q) => new Promise((res, rej) => {
throttle.push(() => _tmdb[m](q, (err, data) => err ? rej(err) : res(data)))
})
tmdbPromise('searchMovie', {query: 'The Martian', year: 2015})
.then(console.log)
.catch(console.error) |
More complex promise example which will only call const _tmdb = require('moviedb')('your-key')
const limits = require('limits.js')
const throttle = limits().within(10 * 1000, 39)
const queue = []
let waitingForToken = false
let haveToken = false
const getToken = () => new Promise((res, rej) => {
if(haveToken) return res()
if(waitingForToken) return queue.push(res)
waitingForToken = true
_tmdb.requestToken((err) => {
throttle.push(() => {})
if(err) return rej(err)
haveToken = true
waitingForToken = false
res()
queue.map(cb => cb())
queue.length = 0
})
})
const tmdb = (m, q) => getToken().then(() => (
new Promise((res, rej) => {
throttle.push(() => _tmdb[m](q, (err, data) => err ? rej(err) : res(data)))
}))
)
tmdb('searchMovie', {query: 'The Martian', year: 2015})
.then(console.log)
.catch(console.error) |
Sorry, I've been busy. I'll try to see this asap |
@daviestar I think that the limiter should be in the lib (and on by default) since it is a limit imposed by TheMovieDB Also, I don't really understand how the requests sequence works with TMDB. I only implemented what worked for me. |
I would like to change the arguments to an object argument, but that would imply changes to the browser versions too and I don't really understand how those are concatenated. If @impronunciable would to tell me how he made the current versions, I could make a npm task to automate it (when I will have some free time). (#75) |
Requests are now throttled so that we won't get 429 errors (Too Many Requests) from TMDB.