Skip to content

Commit bf409e2

Browse files
committed
fix: wait for parallel endpoint discovery operation to complete
1 parent 7547f30 commit bf409e2

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export type updateDiscoveredEndpointInCacheOptions = {
99
identifiers?: Map<String, String>;
1010
};
1111

12+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
13+
1214
export const updateDiscoveredEndpointInCache = async (
1315
config: EndpointDiscoveryClientResolvedConfig,
1416
options: updateDiscoveredEndpointInCacheOptions
@@ -19,12 +21,15 @@ export const updateDiscoveredEndpointInCache = async (
1921
const { commandName, identifiers } = options;
2022
const cacheKey = await getCacheKey(commandName, client?.config, { identifiers });
2123

22-
const endpoints = endpointCache.get(cacheKey);
23-
if (endpoints && endpoints.length === 1 && endpoints[0].Address === "") {
24-
// endpoint operation is being made but response not yet received
25-
// or endpoint operation just failed in 1 minute.
26-
return;
27-
} else if (endpoints && endpoints.length > 0) {
24+
let endpoints = endpointCache.get(cacheKey);
25+
26+
// Wait for other endpoint operations to complete before making new calls.
27+
while (endpoints && endpoints.length === 1 && endpoints[0].Address === "") {
28+
await sleep(1000);
29+
endpoints = endpointCache.get(cacheKey);
30+
}
31+
32+
if (endpoints && endpoints.length > 0) {
2833
// Endpoint record is present in cache.
2934
return;
3035
} else {

0 commit comments

Comments
 (0)