From 964e158a5f0c2325d2ccf9db33fe3b6570e69463 Mon Sep 17 00:00:00 2001 From: Mathias Biilmann Christensen Date: Sun, 4 Sep 2016 20:55:05 +0200 Subject: [PATCH] Fix semaphore implementation for only doing 10 concurrent fetches at a time --- src/backends/backend.js | 1 + src/backends/github/implementation.js | 11 +++++++---- src/backends/netlify-git/implementation.js | 11 +++++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/backends/backend.js b/src/backends/backend.js index c5af5be5..d7d52a15 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -49,6 +49,7 @@ class Backend { entries(collection, page, perPage) { return this.implementation.entries(collection, page, perPage).then((response) => { + console.log("Got %s entries", response.entries.length); return { pagination: response.pagination, entries: response.entries.map(this.entryWithFormat(collection)) diff --git a/src/backends/github/implementation.js b/src/backends/github/implementation.js index a11d8ee6..9cb11f45 100644 --- a/src/backends/github/implementation.js +++ b/src/backends/github/implementation.js @@ -36,12 +36,15 @@ export default class GitHub { const sem = semaphore(MAX_CONCURRENT_DOWNLOADS); const promises = []; files.map((file) => { - sem.take(() => { - promises.push(this.api.readFile(file.path, file.sha).then((data) => { + promises.push(new Promise((resolve, reject) => { + return sem.take(() => this.api.readFile(file.path, file.sha).then((data) => { + resolve(createEntry(file.path, file.path.split('/').pop().replace(/\.[^\.]+$/, ''), data)); sem.leave(); - return createEntry(file.path, file.path.split('/').pop().replace(/\.[^\.]+$/, ''), data); + }).catch((err) => { + sem.leave(); + reject(err); })); - }); + })); }); return Promise.all(promises); }).then((entries) => ({ diff --git a/src/backends/netlify-git/implementation.js b/src/backends/netlify-git/implementation.js index c3dc2ac8..cd52f5ff 100644 --- a/src/backends/netlify-git/implementation.js +++ b/src/backends/netlify-git/implementation.js @@ -33,12 +33,15 @@ export default class NetlifyGit { const sem = semaphore(MAX_CONCURRENT_DOWNLOADS); const promises = []; files.map((file) => { - sem.take(() => { - promises.push(this.api.readFile(file.path, file.sha).then((data) => { + promises.push(new Promise((resolve, reject) => { + return sem.take(() => this.api.readFile(file.path, file.sha).then((data) => { + resolve(createEntry(file.path, file.path.split('/').pop().replace(/\.[^\.]+$/, ''), data)); sem.leave(); - return createEntry(file.path, file.path.split('/').pop().replace(/\.[^\.]+$/, ''), data); + }).catch((err) => { + sem.leave(); + reject(err); })); - }); + })); }); return Promise.all(promises); }).then((entries) => ({