我有一个客户想要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();
有一个节点模块可以对函数调用进行节流:节流函数。