Skip to content

Commit bc9f054

Browse files
committed
feat(endpoint-cache): return un-expired endpoint on get
1 parent d9e7489 commit bc9f054

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/endpoint-cache/src/EndpointCache.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,26 @@ export class EndpointCache {
1313
this.cache = new LRUCache(capacity);
1414
}
1515

16+
private getEndpoint(endpointsWithExpiry: EndpointWithExpiry[]) {
17+
const now = Date.now();
18+
const endpoints = endpointsWithExpiry
19+
.filter((endpoint) => now > endpoint.Expires)
20+
.map((endpoint) => endpoint.Address);
21+
return endpoints[Math.floor(Math.random() * endpoints.length)];
22+
}
23+
1624
get(key: string) {
17-
return this.cache.get(key);
25+
const endpointsWithExpiry = this.cache.get(key);
26+
27+
if (!endpointsWithExpiry) {
28+
return;
29+
}
30+
31+
const endpoint = this.getEndpoint(endpointsWithExpiry);
32+
if (endpoint === undefined) {
33+
this.remove(key);
34+
}
35+
return endpoint;
1836
}
1937

2038
set(key: string, endpoints: Endpoint[]) {

0 commit comments

Comments
 (0)