mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 08:27:08 +00:00
Limit number of simultaneous requests to the API
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
"dependencies": {
|
||||
"@deck.gl/aggregation-layers": "^9.1.13",
|
||||
"@deck.gl/geo-layers": "^9.1.13",
|
||||
"@dougal/concurrency": "file:../../../modules/@dougal/concurrency",
|
||||
"@dougal/organisations": "file:../../../modules/@dougal/organisations",
|
||||
"@dougal/user": "file:../../../modules/@dougal/user",
|
||||
"@mdi/font": "^7.2.96",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
const ConcurrencyLimiter = require('@dougal/concurrency');
|
||||
|
||||
/** Make an API request
|
||||
*
|
||||
@@ -18,6 +19,9 @@
|
||||
* to `true`).
|
||||
*/
|
||||
async function api ({state, getters, commit, dispatch}, [resource, init = {}, cb, opts = {}]) {
|
||||
|
||||
const limiter = api.limiter || (api.limiter = new ConcurrencyLimiter(state.maxConcurrent));
|
||||
|
||||
try {
|
||||
commit("queueRequest");
|
||||
if (init && init.hasOwnProperty("body")) {
|
||||
@@ -63,7 +67,7 @@ async function api ({state, getters, commit, dispatch}, [resource, init = {}, cb
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
res = await fetch(url, init);
|
||||
res = await limiter.enqueue(async () => await fetch(url, init));
|
||||
}
|
||||
|
||||
if (cache && !isCached) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const state = () => ({
|
||||
apiUrl: "/api",
|
||||
requestsCount: 0
|
||||
requestsCount: 0,
|
||||
maxConcurrent: 15
|
||||
});
|
||||
|
||||
export default state;
|
||||
|
||||
Reference in New Issue
Block a user