Skip to content

Commit 900d037

Browse files
committed
chore(endpoint-cache): expose getEndpoint as a separate API
In future, the callee may want to get all unexpired endpoints and add their own logic as more weights and priorities are added.
1 parent ce386ff commit 900d037

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

packages/endpoint-cache/src/EndpointCache.spec.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,28 @@ describe(EndpointCache.name, () => {
103103
expect(set).toHaveBeenCalledWith(key, []);
104104
});
105105

106-
it("returns one of the un-expired endpoints", () => {
107-
expect(mockEndpoints.map((endpoint) => endpoint.Address)).toContain(endpointCache.get(key));
108-
verifyHasAndGetCalls();
109-
expect(set).not.toHaveBeenCalled();
110-
});
111-
112-
it("returns un-expired endpoint", () => {
113-
jest.spyOn(Date, "now").mockImplementation(() => now + 90 * 1000);
114-
expect(endpointCache.get(key)).toEqual(mockEndpoints[1].Address);
115-
verifyHasAndGetCalls();
116-
expect(set).not.toHaveBeenCalled();
117-
});
106+
describe("getEndpoint", () => {
107+
it("returns one of the un-expired endpoints", () => {
108+
expect(mockEndpoints.map((endpoint) => endpoint.Address)).toContain(endpointCache.getEndpoint(key));
109+
verifyHasAndGetCalls();
110+
expect(set).not.toHaveBeenCalled();
111+
});
118112

119-
[0, 1].forEach((index) => {
120-
it(`returns un-expired endpoint at index ${index}`, () => {
121-
jest.spyOn(Math, "floor").mockImplementation(() => index);
122-
expect(mockEndpoints.map((endpoint) => endpoint.Address)).toContain(endpointCache.get(key));
113+
it("returns un-expired endpoint", () => {
114+
jest.spyOn(Date, "now").mockImplementation(() => now + 90 * 1000);
115+
expect(endpointCache.getEndpoint(key)).toEqual(mockEndpoints[1].Address);
123116
verifyHasAndGetCalls();
124117
expect(set).not.toHaveBeenCalled();
125118
});
119+
120+
[0, 1].forEach((index) => {
121+
it(`returns un-expired endpoint at index ${index}`, () => {
122+
jest.spyOn(Math, "floor").mockImplementation(() => index);
123+
expect(mockEndpoints.map((endpoint) => endpoint.Address)).toContain(endpointCache.getEndpoint(key));
124+
verifyHasAndGetCalls();
125+
expect(set).not.toHaveBeenCalled();
126+
});
127+
});
126128
});
127129
});
128130

packages/endpoint-cache/src/EndpointCache.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { LRUCache } from "mnemonist";
22

33
import { Endpoint } from "./Endpoint";
44

5-
interface EndpointWithExpiry extends Pick<Endpoint, "Address"> {
5+
export interface EndpointWithExpiry extends Pick<Endpoint, "Address"> {
66
Expires: number;
77
}
88

@@ -13,16 +13,23 @@ 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);
16+
/**
17+
* Returns an un-expired endpoint for the given key.
18+
*
19+
* @param endpointsWithExpiry
20+
* @returns
21+
*/
22+
getEndpoint(key: string) {
23+
const endpointsWithExpiry = this.get(key);
24+
if (!endpointsWithExpiry || endpointsWithExpiry.length === 0) {
25+
return undefined;
26+
}
27+
const endpoints = endpointsWithExpiry.map((endpoint) => endpoint.Address);
2128
return endpoints[Math.floor(Math.random() * endpoints.length)];
2229
}
2330

2431
/**
25-
* Returns an un-expired endpoint for the given key.
32+
* Returns un-expired endpoints for the given key.
2633
*
2734
* @param key
2835
* @returns
@@ -32,16 +39,19 @@ export class EndpointCache {
3239
return;
3340
}
3441

35-
const endpointsWithExpiry = this.cache.get(key);
36-
if (!endpointsWithExpiry) {
42+
const value = this.cache.get(key);
43+
if (!value) {
3744
return;
3845
}
3946

40-
const endpoint = this.getEndpoint(endpointsWithExpiry);
41-
if (endpoint === undefined) {
47+
const now = Date.now();
48+
const endpointsWithExpiry = value.filter((endpoint) => now < endpoint.Expires);
49+
if (endpointsWithExpiry.length === 0) {
4250
this.delete(key);
51+
return undefined;
4352
}
44-
return endpoint;
53+
54+
return endpointsWithExpiry;
4555
}
4656

4757
/**

0 commit comments

Comments
 (0)