Limit the number of concurrent downloads

Doesn't actually help!
This commit is contained in:
Slava Kim
2014-09-26 17:21:48 -07:00
parent 57e5cae447
commit 9600d92ca7

View File

@@ -1,4 +1,12 @@
var DEBUG_TAG = 'METEOR CORDOVA DEBUG ';
// This constant was picked by testing on iOS 7.1
// We limit the number of concurrent downloads because iOS gets angry on the
// application when a certain limit is exceeded and starts timing-out the
// connections in 1-2 minutes which makes the whole HCP really slow.
var MAX_NUM_CONCURRENT_DOWNLOADS = 15;
var MAX_RETRY_COUNT = 5;
var autoupdateVersionCordova = __meteor_runtime_config__.autoupdateVersionCordova || "unknown";
// The collection of acceptable client versions.
@@ -81,14 +89,19 @@ var onNewVersion = function () {
manifest.push({ url: '/index.html?' + Random.id() });
var downloads = 0;
_.each(manifest, function (item) {
if (item.url) downloads++;
});
var versionPrefix = localPathPrefix + version;
var afterAllFilesDownloaded = _.after(downloads, function () {
var queue = [];
_.each(manifest, function (item) {
if (! item.url) return;
var url = item.url;
url = url.replace(/\?.+$/, '');
queue.push(url);
});
var afterAllFilesDownloaded = _.after(queue.length, function () {
writeFile(versionPrefix, 'manifest.json',
JSON.stringify(program, undefined, 2),
function (err) {
@@ -118,12 +131,8 @@ var onNewVersion = function () {
});
});
_.each(manifest, function (item) {
if (! item.url) return;
var url = item.url;
url = url.replace(/\?.+$/, '');
var dowloadUrl = function (url) {
console.log(DEBUG_TAG + "start dowloading " + url);
// Add a cache buster to ensure that we don't cache an old asset.
var uri = encodeURI(urlPrefix + url + '?' + Random.id());
@@ -132,20 +141,27 @@ var onNewVersion = function () {
var tryDownload = function () {
ft.download(uri, versionPrefix + url, function (entry) {
if (entry) {
console.log(DEBUG_TAG + "done dowloading " + url);
// start downloading next queued url
if (queue.length)
dowloadUrl(queue.shift());
afterAllFilesDownloaded();
}
}, function (err) {
// It failed, try again if we have tried less than 5 times.
if (tries++ < 5) {
if (tries++ < MAX_RETRY_COUNT) {
tryDownload();
} else {
console.log(DEBUG_TAG + 'fail source: ', error.source);
console.log(DEBUG_TAG + 'fail target: ', error.target);
console.log(DEBUG_TAG + "failed dowloading " + url);
}
});
};
tryDownload();
};
_.times(Math.min(MAX_NUM_CONCURRENT_DOWNLOADS, queue.length), function () {
dowloadUrl(queue.shift());
});
});
};