From b38c265501b48aea53e62d7fdef8e6239243a52c Mon Sep 17 00:00:00 2001 From: Vit Habada Date: Mon, 18 Nov 2024 12:07:59 +0100 Subject: [PATCH] feat: allow specifying timeout for cluster metrics aggregation --- index.d.ts | 7 ++++++- lib/cluster.js | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 1fd1eac9..fa512fcf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -136,10 +136,11 @@ export class AggregatorRegistry< > extends Registry { /** * Gets aggregated metrics for all workers. + * @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation * @return {Promise} Promise that resolves with the aggregated * metrics. */ - clusterMetrics(): Promise; + clusterMetrics(options?: ClusterMetricsOptions): Promise; /** * Creates a new Registry instance from an array of metrics that were @@ -192,6 +193,10 @@ export enum MetricType { Summary, } +export type ClusterMetricsOptions = { + timeoutMs?: number; +}; + type CollectFunction = (this: T) => void | Promise; interface MetricObject { diff --git a/lib/cluster.js b/lib/cluster.js index 5cb707ed..08ebeab3 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -36,10 +36,11 @@ class AggregatorRegistry extends Registry { /** * Gets aggregated metrics for all workers. The optional callback and * returned Promise resolve with the same value; either may be used. + * @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation. * @return {Promise} Promise that resolves with the aggregated * metrics. */ - clusterMetrics() { + clusterMetrics(options = { timeoutMs: 5000 }) { const requestId = requestCtr++; return new Promise((resolve, reject) => { @@ -58,7 +59,7 @@ class AggregatorRegistry extends Registry { errorTimeout: setTimeout(() => { const err = new Error('Operation timed out.'); request.done(err); - }, 5000), + }, options.timeoutMs || 5000), }; requests.set(requestId, request);