From b38c265501b48aea53e62d7fdef8e6239243a52c Mon Sep 17 00:00:00 2001 From: Vit Habada Date: Mon, 18 Nov 2024 12:07:59 +0100 Subject: [PATCH 1/2] 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); From 15a93896682498429b86adfd879b66e7f90ff16a Mon Sep 17 00:00:00 2001 From: vitush93 Date: Mon, 10 Mar 2025 18:43:48 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Mickael Bourgois <35171168+BourgoisMickael@users.noreply.github.com> --- index.d.ts | 2 ++ lib/cluster.js | 1 + 2 files changed, 3 insertions(+) diff --git a/index.d.ts b/index.d.ts index fa512fcf..59aae08a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -137,6 +137,7 @@ export class AggregatorRegistry< /** * Gets aggregated metrics for all workers. * @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation + * @param {number} [options.timeoutMs=5000] Timeout to wait for all workers to respond before error. * @return {Promise} Promise that resolves with the aggregated * metrics. */ @@ -194,6 +195,7 @@ export enum MetricType { } export type ClusterMetricsOptions = { + /** Timeout to wait for all workers to respond before error. */ timeoutMs?: number; }; diff --git a/lib/cluster.js b/lib/cluster.js index 08ebeab3..119890f9 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -37,6 +37,7 @@ 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. + * @param {number} [options.timeoutMs=5000] Timeout to wait for all workers to respond before error. * @return {Promise} Promise that resolves with the aggregated * metrics. */