7eggs / node-toggl-api

Toggl API for Node.js
http://7eggs.github.io/node-toggl-api/
MIT License
121 stars 44 forks source link

Rate limiting. #15

Open LiamKarlMitchell opened 5 years ago

LiamKarlMitchell commented 5 years ago

The Toggl API docs mention. For rate limiting we have implemented a Leaky bucket. When a limit has been hit the request will get a HTTP 429 response and it's the task of the client to sleep/wait until bucket is empty. Limits will and can change during time, but a safe window will be 1 request per second. Limiting is applied per api token per IP, meaning two users from the same IP will get their rate allocated separately.

I have not encountered this limit yet, however I wonder if it could be useful to implement an option to throttle rapid requests? Or would the view be that this be implemented in application code not the module?

Kind regards,

LiamKarlMitchell commented 5 years ago

Okay, I made many updates in quick succession and get this 429 Too Many Requests error.

I think it should delay and retry when encountered.

code:429
data:<html>\r\n<head><title>429 Too Many Requests</title></head>\r\n<body>\r\n<center><h1>429 Too Many Requests</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n"
isOperational:true
stack:"Error: Unknown API error\n    at Request.onresponse [as _callback] client.js:138:16)

function delay (ms, result) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve(result)
    }, ms)
  })
}

if (errorUpdate.code === 429) {
  await delay(1000)
  // How to retry somewhere in client.js 's onresponse
}