Skip to content

Commit ab3b9e9

Browse files
authored
Enhance CR listing with manual timeout handling (#1454)
1 parent 51a47b0 commit ab3b9e9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

appmonitoring/ts/src/K8sWatcher.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,27 @@ export class K8sWatcher {
5252

5353
logger.info(`Listing CRs, resourceVersion=${latestResourceVersion}...`, operationId, requestMetadata);
5454

55+
const listTimeoutSeconds = 30;
5556
let crsResult: ListResponse;
5657
try {
57-
crsResult = <ListResponse>await k8sApi.listClusterCustomObject({
58+
const listPromise: Promise<ListResponse> = k8sApi.listClusterCustomObject({
5859
group: K8sWatcher.crdApiGroup,
5960
version: K8sWatcher.crdApiVersion,
6061
plural: K8sWatcher.crdNamePlural,
6162
resourceVersion: latestResourceVersion ?? undefined,
62-
timeoutSeconds: 30
63+
timeoutSeconds: listTimeoutSeconds
6364
});
6465

66+
const timeoutPromise = new Promise<ListResponse>((resolve, reject) => {
67+
setTimeout(() =>
68+
{
69+
reject(new Error("List hung, manual timeout is used"));
70+
},
71+
3 * listTimeoutSeconds * 1000);
72+
});
73+
74+
crsResult = <ListResponse>await Promise.race([listPromise, timeoutPromise]);
75+
6576
logger.addHeartbeatMetric(HeartbeatMetrics.CRsListCallSucceededCount, 1, "200");
6677
} catch(e) {
6778
logger.addHeartbeatMetric(HeartbeatMetrics.CRsListCallFailedCount, 1, e?.statusCode ?? 0);

0 commit comments

Comments
 (0)