Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hard-limit search concurrency to 10 #1103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions app/assets/javascripts/services/queriesSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,21 +785,41 @@ angular.module('QuepidApp')
return querySearchableDeferred.promise;
};

this.pAll = async function(queue, concurrency) {
let index = 0;
const results = [];

const worker = async () => {
while (index < queue.length) {
const curIndex = index++;
const promise = queue[curIndex]();
await promise;
results[curIndex] = promise;
}
};

const workers = [];
for (let workerIdx = 0; workerIdx < concurrency; workerIdx++) {
workers.push(worker());
}
await Promise.all(workers);
return Promise.all(results);
}

this.searchAll = function() {
let promises = [];
let scorePromises = [];

angular.forEach(this.queries, function(query) {
let promise = query.search().then( () => {
let searchPromiseFn = () => query.search().then(() => {
scorePromises.push(query.score());
});

promises.push(promise);
promises.push(searchPromiseFn);
});

// Holy nested promises batman
return $q.all(promises).then( () => {
return this.pAll(promises, 10).then( () => {
$q.all(scorePromises).then( () => {
/*
* Why are we calling scoreAll after we called score() above?
Expand Down