hey I'm terrible with markdown, but the example bellow is in a lib, basically it does limit the number of active connections to a http API rest service to the a defined number (8 in this case), so a the http request is resolved, it start another connection keeping always 8 active connections to the API server; it will completely hide the connection/handshake delay while guarantee no 429 error (too many requests); from my experience fastest safe approach as you can know the maximum number of calls per second of the API service
// this is the asyncLimit adjusted constasyncLimit=(fn,n)=>{constpendingPromises=newSet();returnasyncfunction(...args){while(pendingPromises.size>=n){awaitPromise.race(pendingPromises);}constp=fn.apply(this,args);constr=p.catch(()=>{});pendingPromises.add(r);awaitr;pendingPromises.delete(r);returnp;};};// native node.js https module to connect to shopify serversconsthttps=require('https')exports.httpRequest=function(method,path,body=null){constreqOpt={method:method,path:'/admin'+path,hostname:'xxxxxxxxxxxxxxxxxxxx.myshopify.com',headers:{"Content-Type":"application/json","X-Shopify-Access-Token":"xxxxxxxxxxxxxxxxxxxx",'Cookie':'',"Cache-Control":"no-cache"}}if(body)reqOpt.headers['Content-Length']=Buffer.byteLength(body);returnnewPromise((resolve,reject)=>{constclientRequest=https.request(reqOpt,incomingMessage=>{letresponse={statusCode:incomingMessage.statusCode,headers:incomingMessage.headers,body:[]};letchunks=""incomingMessage.on('data',chunk=>{chunks+=chunk;});incomingMessage.on('end',()=>{if(chunks){try{response.body=JSON.parse(chunks);}catch(error){reject(error)}}resolve(response);});});clientRequest.on('error',error=>{reject(error);});if(body){clientRequest.write(body)}clientRequest.end();});}// the number 8 bellow can be changed to match the REST API service limits// assume that this amount will call at once and will be replaced dynamically, hence// if the service limit it 20 calls per second, be aware that 8 calls will hit the service at once// using 40% of the maximum (avoid going higher)exports.ratedhttpRequest=asyncLimit(exports.httpRequest,8);
so, assigned the same delay for all of those...like 2 seconds... I did expect to see it process 2, schedule + 2; so, the result would be observable every 2 seconds and everytime 2 task
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I implemented this whole thing with a working code, let me know if I am doing something incorrectly.
Note: I am using setTimeout for async calls so promise failure will not happen.
using the corrections from @kusha and the data and functino of your code it did work as expected!
hurray!
exceptional implementation to deal with REST API
Can you post your working snippet here?
hey I'm terrible with markdown, but the example bellow is in a lib, basically it does limit the number of active connections to a http API rest service to the a defined number (8 in this case), so a the http request is resolved, it start another connection keeping always 8 active connections to the API server; it will completely hide the connection/handshake delay while guarantee no 429 error (too many requests); from my experience fastest safe approach as you can know the maximum number of calls per second of the API service
hey, tried your code, did not work...
so, assigned the same delay for all of those...like 2 seconds... I did expect to see it process 2, schedule + 2; so, the result would be observable every 2 seconds and everytime 2 task