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

Added limts.js #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Added limts.js #74

wants to merge 1 commit into from

Conversation

mocanew
Copy link

@mocanew mocanew commented Feb 17, 2017

Requests are now throttled so that we won't get 429 errors (Too Many Requests) from TMDB.

Requests are now throttled so that we won't get 429 errors (Too Many Requests) from TMDB.
@impronunciable
Copy link
Owner

Pretty cool! @rontav I'll take a look during the weekend

@mocanew
Copy link
Author

mocanew commented Feb 18, 2017

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.

@daviestar
Copy link

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?

@daviestar
Copy link

daviestar commented Mar 4, 2017

Lately my tests have been failing with node-rate-limiter, so this morning I played around with limits.js as I hadn't heard of it before seeing it here. It's a great little utility that seems more fit-for-purpose than node-rate-limiter which is designed primarily for the API owner rather than the API-consumer.

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)

@daviestar
Copy link

daviestar commented Mar 4, 2017

More complex promise example which will only call requestToken once as you spam the function, which allows for more accurate throttling (similar to PR #38):

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)

@impronunciable
Copy link
Owner

Sorry, I've been busy. I'll try to see this asap

@mocanew
Copy link
Author

mocanew commented Mar 5, 2017

@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.

@mocanew
Copy link
Author

mocanew commented Mar 5, 2017

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)

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.

3 participants