Skip to content

Instantly share code, notes, and snippets.

@benbai123
Last active August 26, 2016 09:14
Show Gist options
  • Save benbai123/abe18cbf183ce181433e4e9abf11a7a2 to your computer and use it in GitHub Desktop.
Save benbai123/abe18cbf183ce181433e4e9abf11a7a2 to your computer and use it in GitHub Desktop.
var _handleTradeWaitingQueue = [], // 處理的 queue
concurrentTradeLimit = 300, // 同時處理上限
concurrentTradeCount = 0; // 處理計數
function afterTradeHandle () {
// 計數小於上限 且 有未處理項目才處理
if (concurrentTradeCount < concurrentTradeLimit
&& _handleTradeWaitingQueue.length) {
concurrentTradeCount++;
handle(_handleTradeWaitingQueue.shift());
}
}
function handle(dd) {
...
// 受限制的項目
else if (dd.c == 5) {
if (/* 不處理的條件*/) {
concurrentTradeCount--;
afterTradeHandle();
return;
}
....trade(..., function (data) {
// callback 中, 基本吃 memory 的應該都結束了
concurrentTradeCount--;
afterTradeHandle();
...
});
}
...
}
....on('data', function (dataStr) {
...
if (_.isUndefined(dd.c) == false) {
//console.log('dd: ' + JSON.stringify(dd));
if (dd.c == 5) { // 如果是受限制項目
// 計數小於上限 而且沒有等待中的項目 才處理
if (concurrentTradeCount < concurrentTradeLimit
&& !_handleTradeWaitingQueue.length) {
concurrentTradeCount++;
handle(dd);
} else {
// 不然丟進 queue 等待
_handleTradeWaitingQueue.push(dd);
}
} else {
handle(dd);
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment