Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
fix: control sync frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Nov 8, 2017
1 parent bfb29f8 commit 820ae23
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions sync/changes_stream_syncer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const lastSeqFile = path.join(config.dataDir, '.cnpmjs.org.last_seq.txt');
let _STREAM_ID = 0;

module.exports = function* sync() {
const pedding = [];
const since = yield getLastSequence();
const streamId = _STREAM_ID++;
let changesCount = 0;
Expand All @@ -26,12 +27,24 @@ module.exports = function* sync() {
changes.on('data', change => {
changesCount++;
logger.syncInfo('stream#%d get change#%d: %j', streamId, changesCount, change);
syncPackage(change);
pedding.push(change);
// syncPackage(change);
});

const timer = setInterval(function() {
for (var i = 0; i < 100; i++) {
var change = pedding.shift();
if (!change) {
break;
}
syncPackage(change);
}
}, 5000);

try {
yield changes.await('error');
} catch (err) {
clearInterval(timer);
// make sure changes steam is destroy
changes.destroy();
err.message += `, stream#${streamId}, changesCount#${changesCount}`;
Expand All @@ -50,7 +63,6 @@ function syncPackage(change) {
logger.syncInfo('%s:%s PUT %s error: %s, retry after 5s',
change.seq, change.id, url, err);
logger.syncError(err);
syncPackage(change);
setTimeout(() => syncPackage(change), 5000);
} else {
saveLastSequence(change.seq);
Expand Down

0 comments on commit 820ae23

Please sign in to comment.