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

Commit

Permalink
Merge pull request #1867 from matrix-org/luke/fix-group-request-concu…
Browse files Browse the repository at this point in the history
…rrency

Prevent error responses wedging group request concurrency limit
  • Loading branch information
dbkr authored May 2, 2018
2 parents 1413890 + 2dfb314 commit 3d478c3
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/stores/GroupStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,24 @@ function checkBacklog() {

// Limit the maximum number of ongoing promises returned by fn to LIMIT and
// use a FIFO queue to handle the backlog.
function limitConcurrency(fn) {
return new Promise((resolve, reject) => {
const item = () => {
ongoingRequestCount++;
resolve();
};
if (ongoingRequestCount >= LIMIT) {
// Enqueue this request for later execution
backlogQueue.push(item);
} else {
item();
}
})
.then(fn)
.then((result) => {
async function limitConcurrency(fn) {
if (ongoingRequestCount >= LIMIT) {
// Enqueue this request for later execution
await new Promise((resolve, reject) => {
backlogQueue.push(resolve);
});
}

ongoingRequestCount++;
try {
return await fn();
} catch (err) {
// We explicitly do not handle the error here, but let it propogate.
throw err;
} finally {
ongoingRequestCount--;
checkBacklog();
return result;
});
}
}

/**
Expand Down

0 comments on commit 3d478c3

Please sign in to comment.