API 服务限制我或我的巨型 for 循环对于我的 POST 请求来说太快了?
An API service restricting me or my giant for loop too fast for my POST request?
我有一个客户想要将 8000 件商品推送到他的 shopify 商店。
我编写了这段代码,但我遇到了一个问题:除非我将传出连接限制在大约 1-2 个,否则项目 posting 会以未定义的方式响应并失败....我的 loop/post req 可能太快了,但我试图减慢它的所有方法都失败了。
- 这里API他们设置的限制
2 calls per second, with room for a burst of 40 calls at once.
我正在使用 node.js 和 ms sql 插件。来自 ms sql 的数据通过流很好地到达并被推送到我的数组 rowPush,然后我循环遍历它(8000)以通过单播发送 post 请求。
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);
});
有一个节点模块可以限制函数调用:throttle-function。
现在你正在充斥系统。我已经幸运地使用了如下图所示的模式制作了数百件物品。简而言之,每个产品仅在上一次调用 returns 时才发送。如果你真的需要节流,你可以尝试注释掉的 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();
我有一个客户想要将 8000 件商品推送到他的 shopify 商店。
我编写了这段代码,但我遇到了一个问题:除非我将传出连接限制在大约 1-2 个,否则项目 posting 会以未定义的方式响应并失败....我的 loop/post req 可能太快了,但我试图减慢它的所有方法都失败了。
- 这里API他们设置的限制
2 calls per second, with room for a burst of 40 calls at once.
我正在使用 node.js 和 ms sql 插件。来自 ms sql 的数据通过流很好地到达并被推送到我的数组 rowPush,然后我循环遍历它(8000)以通过单播发送 post 请求。
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);
});
有一个节点模块可以限制函数调用:throttle-function。
现在你正在充斥系统。我已经幸运地使用了如下图所示的模式制作了数百件物品。简而言之,每个产品仅在上一次调用 returns 时才发送。如果你真的需要节流,你可以尝试注释掉的 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();