Skip to content

Commit

Permalink
chore: instrument requests deduplicator (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e authored Aug 28, 2023
1 parent 22740dc commit f94fc10
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/helpers/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
6 changes: 4 additions & 2 deletions src/helpers/requestDeduplicator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { requestDeduplicatorSize } from './metrics';
import { sha256 } from './utils';

const ongoingRequests = new Map();
Expand All @@ -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);
}

0 comments on commit f94fc10

Please sign in to comment.