diff --git a/src/helpers/metrics.ts b/src/helpers/metrics.ts index 230ed981..2ab48449 100644 --- a/src/helpers/metrics.ts +++ b/src/helpers/metrics.ts @@ -109,3 +109,8 @@ new client.Gauge({ }); } }); + +export const requestDeduplicatorSize = new client.Gauge({ + name: 'request_deduplicator_size', + help: 'Total number of items in the deduplicator queue' +}); diff --git a/src/helpers/requestDeduplicator.ts b/src/helpers/requestDeduplicator.ts index 55e21964..3fc33d93 100644 --- a/src/helpers/requestDeduplicator.ts +++ b/src/helpers/requestDeduplicator.ts @@ -1,3 +1,4 @@ +import { requestDeduplicatorSize } from './metrics'; import { sha256 } from './utils'; const ongoingRequests = new Map(); @@ -7,15 +8,16 @@ export default async function serve(id, action, args) { if (!ongoingRequests.has(key)) { const requestPromise = action(...args) .then(result => { - ongoingRequests.delete(key); return result; }) .catch(e => { - ongoingRequests.delete(key); throw e; + }).finally(() => { + ongoingRequests.delete(key); }); ongoingRequests.set(key, requestPromise); } + requestDeduplicatorSize.set(ongoingRequests.size); return ongoingRequests.get(key); }