一个API服务限制我或我的巨型循环对于我的POST请求来说太快了
An API service restricting me or my giant for loop too fast for my POST request?
我有一个客户想要8000件商品推送到他的shopify商店。
我编写了这个代码,但我有一个问题:除非我将传出连接限制在大约1-2个,否则发布的项目将以未定义和失败。。。。我的loop/post-req可能太快了,但我试图放慢速度的所有方法都失败了。
- 这里是他们设置的API限制
每秒2个呼叫,同时可容纳40个呼叫。
我使用的是node.js和ms-sql插件。来自ms-sql的数据通过流很好地到达,并被推送到我的数组rowPush,然后我循环通过它(8000),通过单播发送post-req。
sql.connect(userConfig, function(err) {
if (err) {
console.log("you done screwed up the dang connection to SQL " + err)
};
var request = new sql.Request();
request.stream = true;
request.verbose = true;
request.query('SELECT intProductID, Stock, strPurDesc, Vendor, Brand, intPurchasePrice, strBarCode FROM V_ProductList ORDER BY intProductID');
var rowPush = [];
//row is the object that returns from MySQL database.
request.on('row', function(row) {
rowPush.push(row);
});
request.on('error', function(err) {
console.log('err occured ' + err);
});
request.on('done', function(returnValue) {
//my for loop for looping through every item in rowPush.
for (i =0; i < rowPush.length; i++ ) {
var newProduct = {
"product": {
"title": rowPush[i].strPurDesc,
"id": rowPush[i].intProductID,
"vendor": rowPush[i].Vendor,
"product_type": rowPush[i].Brand,
"variants": [
{
//"id": 1044399237,
//"product_id": 1071559589,
"inventory_management":"shopify",
"inventory_quantity": rowPush[i].Stock,
"barcode": rowPush[i].strBarCode,
"price": rowPush[i].intPurchasePrice,
"taxable" : true,
}
]
}
};
//console.log(JSON.stringify(newProduct));
var sendNewItem = function (){
unirest.post('https://5b848c9ca0e184f13140629d9c2d34ca:b8b14f71ad82a7d8f00520f8a4f5f571@teststoresrh.myshopify.com/admin/products.json')
//.header('Accept', 'application/json')
.set('Content-Type', 'application/json')
.send(newProduct)
.end(function (response) {
console.log(response.body);
});
}
if (rowPush[i].Vendor) {
sendNewItem();
};
//sendNewItem();
}
});
console.log(rowPush[8210]);
});
//});
sql.on('error', function(err) {
console.log("you done screwed up the dang connection to SQL " + err);
});
现在您正在淹没系统。我很幸运地用了几百件像下面这样的图案。简而言之,每个产品只有在上一次呼叫返回时才会发送。如果你真的需要节流,你可以尝试注释掉的setTimeout行,而不是上面的行
var https = require('https');
var cred = new Buffer(5b848c9ca0e184f13140629d9c2d34ca:b8b14f71ad82a7d8f00520f8a4f5f571").toString('base64');
var headers = {Authorization: "Basic "+cred, "Content-Type": "application/json"};
var options = {
host: 'teststoresrh.myshopify.com',
port: 443,
path: '/admin/products.json',
method: 'POST',
headers: headers
};
var rowPush = [];
// fill rowPush from sql
function sendProduct(){
if(!rowPush.length) return;
var row = rowPush.shift(); // FIFO
var newProduct = (function(){
// just like now but with row instead of rowPush[i];
})();
// Setup the request. The options parameter is
// the object we defined above.
var req = https.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
console.log(data);
});
res.on('end', function() {
var resultObject = JSON.parse(responseString);
sendProduct();
//setTimeout(sendProduct, 500);
});
});
req.on('error', function(e) {
// TODO: handle error.
console.log(e);
});
req.write(JSON.stringify(newProduct));
req.end();
}
sendProduct();
有一个节点模块可以对函数调用进行节流:节流函数。
相关文章:
- 为什么我的$http.post返回一个400错误
- 为什么当我指定POST时,即使我已经尝试了类型和方法选项,我的ajax代码仍然会触发GET请求
- 为什么我的代码循环无限提交而不回显$post-nametest_value
- jQuery/PHP-POST-Forbidden(403)当试图在我的JSON数据中发送完整的URL时
- 如果我只使用POST,那么我的MVC JSON操作怎么能用GET调用呢
- 我的jQuery POST请求有什么问题
- 为什么 jQuery.post 最终会变成 PHP 的 _POST 美元,而我的香草 JS 帖子却没有
- 我将如何形成正确的 POST 请求以LinkedIn将临时身份验证代码交换为访问令牌,SSL3 是原因吗?
- 为什么 $.post 似乎不能在我的网站上的 iPad/iPhone 上运行
- 即使我在 POST 上收到 200 响应,_POST 美元的数据也是空的
- JQuery.post 从服务器获取cookie,但不在我的计算机上设置它们
- 我的$.post函数没有't返回任何数据
- 为什么location.reload()会破坏我的$.post
- 一个API服务限制我或我的巨型循环对于我的POST请求来说太快了
- 为什么Flask没有收到我的POST Blob
- 为什么我的post函数不能访问它上面的变量?
- 为什么我的POST返回[对象对象]
- 为什么node.js会附加'0'我的POST请求
- 为什么我的 POST 变量没有到达 this.form.submit();
- 为什么我的post请求PostgreSQL DB保存我的UTC DATETIME作为本地时间