有关更多信息,请参阅文档: https://caolan.github.io/async/docs.html#parallelLimit https://caolan.github.io/async/docs.html#series http://bluebirdjs.com/docs/api/promise.map.html http://bluebirdjs.com/docs/api/promise.mapseries.html当然,还有更多的方法可以做到,但是那是最直接的.理想情况下,如果您想充分利用每小时200个请求的限制,那么您应该自己排队请求,并以与该限制相对应的特定间隔发出请求.有时候,如果您在一小时内没有做很多请求,那么您就不需要延迟了,有时候是这样.在这里,您真正应该做的是将所有请求集中排入队列,并按照与已用完的部分相对应的间隔清空队列,直到达到您应该跟踪的极限,但这很棘手.I have more than 2000 user in my database , when I try to broadcast a message to all users, it barely sends about 200 request then my server stops and I get an error as below : { Error: connect ETIMEDOUT 31.13.88.4:443at Object.exports._errnoException (util.js:1026:11)at exports._exceptionWithHostPort (util.js:1049:20)at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)code: 'ETIMEDOUT',errno: 'ETIMEDOUT',syscall: 'connect,address: '31.13.88.4',port: 443 }Sometimes I get another error that says : Error!: Error: socket hang upThis is my request : function callSendAPI(messageData) { request({ uri: 'https://graph.facebook.com/v2.6/me/messages', qs: { access_token: '#####' }, method: 'POST', json: messageData }, function (error, response, body) { if (!error && response.statusCode == 200) { var recipientId = body.recipient_id; var messageId = body.message_id; if (messageId) { console.log("Successfully sent message with id %s to recipient %s", messageId, recipientId); } else { console.log("Successfully called Send API for recipient %s", recipientId); } } else { console.error("Failed calling Send API"); console.log(error) } });}I have triedsetTimeout to make the the API calling wait for a while: setTimeout(function(){callSendAPI(data)},200);Can anyone help if he/she faced a similar error ?EDITEDI'm using Messenger Platform which support high rate of calls to the Send API and it is not limited with 200 calls . 解决方案 You may be hitting Facebook API limits. To throttle the requests you should send every request after some interval from the previous one. You didn't include where you're iterating over all users but I suspect that you maybe do it in a loop and if you use setTimeout to delay every request with flat 200ms delay then you have all requests done at the same time like you did before - just 200ms later.What you can do is:You can use setTimeout and add variable delay for every request (not recommended)You can use Async module's series or parallelLimit (using callbacks)You can use Bluebird's Promise.mapSeries or Promise.map with concurrency limit (using promises)The 1 is not recommended because it will still be fire-and-forget (unless you add more complexity to that) and you still risk that you have too much concurrency and go over limit because you only control when the requests start, not how many of outstanding requests are there.The 2 and 3 are mostly the same but differ by using callbacks or promises. In your example you're using callbacks but your callSendAPI doesn't take its own callback which it should if you want option 2 to work - or, alternatively, it should return a promise if you want option 3 to work.For more info see the docs:https://caolan.github.io/async/docs.html#parallelLimithttps://caolan.github.io/async/docs.html#serieshttp://bluebirdjs.com/docs/api/promise.map.htmlhttp://bluebirdjs.com/docs/api/promise.mapseries.htmlOf course there are more ways to do it but those are the most straightforward.Ideally, if you want to fully utilize the 200 requests per hour limit then you should queue the requests yourself and make the requests at certain intervals that correspond to that limit. Sometimes if you didn't do a lot of requests in an hour then you won't need delays, sometime you will. What you should really do here is to queue all requests centrally and empty the queue at intervals corresponding to the already used up portion to the limit which you should track yourself - but that can be tricky. 这篇关于从Node.js向API发送许多请求会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-24 23:15