Skip to content

Commit

Permalink
downloader: adjust queue concurrency
Browse files Browse the repository at this point in the history
Adjust queue concurrency based off number of links finished in last time period
Close #11
  • Loading branch information
myfreeer committed Sep 18, 2019
1 parent 1efcd9b commit ce0eec2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
32 changes: 32 additions & 0 deletions adjust-concurrency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

/**
* @param {Downloader} downloader
*/
const adjust = (downloader) => {
if (!downloader.firstPeriodCount) {
downloader.firstPeriodCount = Object.keys(downloader.downloadedLinks).length;
downloader.lastPeriodTotalCount =
downloader.currentPreiodCount =
downloader.lastPeriodCount =
downloader.firstPeriodCount;
return;
}
const total = Object.keys(downloader.downloadedLinks).length;
downloader.lastPeriodCount = downloader.currentPreiodCount;
downloader.currentPreiodCount = total - downloader.lastPeriodTotalCount;
downloader.lastPeriodTotalCount = total;
if (downloader.currentPreiodCount < 2) {
downloader.queue.concurrency += 8;
} else if (downloader.currentPreiodCount < downloader.lastPeriodCount >> 1) {
downloader.queue.concurrency += 4;
}
if (downloader.currentPreiodCount < downloader.firstPeriodCount >> 2) {
downloader.queue.concurrency += 2;
}
if (downloader.currentPreiodCount > downloader.firstPeriodCount) {
downloader.queue.concurrency -= 4;
}
console.info('concurrency', downloader.queue.concurrency);
};

module.exports = adjust;
11 changes: 11 additions & 0 deletions downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ class Downloader {
}
start() {
this.finished = 0;
if (this.adjustConcurrencyTimer) {
clearInterval(this.adjustConcurrencyTimer);
}
if (this.options.adjustConcurrencyPeriod > 0) {
this.adjustConcurrencyTimer = setInterval(() => {
this.options.adjustConcurrencyFunc(this);
}, this.options.adjustConcurrencyPeriod);
}
this.queue.onIdle().then(() => {
this.finished = 1;
if (typeof this.options.onSuccess === 'function') {
Expand All @@ -107,6 +115,9 @@ class Downloader {
}

stop() {
if (this.adjustConcurrencyTimer) {
clearInterval(this.adjustConcurrencyTimer);
}
this.queue.pause();
}
}
Expand Down
2 changes: 2 additions & 0 deletions mdn-local.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface Options {
onSuccess?: Function;
onError?: Function;
detectIncompleteHtml?: '</html>' | '</body>' | string;
adjustConcurrencyPeriod: number;
adjustConcurrencyFunc?(downloader: any): void;
}

interface Encoding {
Expand Down
11 changes: 7 additions & 4 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const adjust = require('./adjust-concurrency');
const defaultOptions = {
req: {
retry : 20,
Expand All @@ -11,8 +12,8 @@ const defaultOptions = {
connect: 400,
secureConnect: 500,
send: 700,
response: 35000,
request: 40000
response: 20000,
request: 23000
}
},
encoding: {
Expand Down Expand Up @@ -59,7 +60,7 @@ const defaultOptions = {
/**
* @type number
*/
concurrency: 16,
concurrency: 12,
/**
* @type number
*/
Expand All @@ -74,6 +75,8 @@ const defaultOptions = {
/**
* @type * | function
*/
onError: null
onError: null,
adjustConcurrencyPeriod: 60000,
adjustConcurrencyFunc: adjust
};
module.exports = defaultOptions;

0 comments on commit ce0eec2

Please sign in to comment.