-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(util-endpoint): add endpoint ruleset cache
- Loading branch information
Showing
8 changed files
with
398 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@smithy/util-endpoints": minor | ||
--- | ||
|
||
add endpoint ruleset cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { EndpointCache } from "./EndpointCache"; | ||
|
||
describe(EndpointCache.name, () => { | ||
const endpoint1: any = {}; | ||
const endpoint2: any = {}; | ||
|
||
it("should store and retrieve items", () => { | ||
const cache = new EndpointCache({ | ||
size: 50, | ||
}); | ||
|
||
expect(cache.get({ A: "b", B: "b" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.get({ A: "b", B: "b" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.get({ A: "b", B: "b", C: "c" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.get({ A: "b", B: "b", C: "c" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.get({ A: "b", B: "b", C: "cc" }, () => endpoint2)).toBe(endpoint2); | ||
expect(cache.get({ A: "b", B: "b", C: "cc" }, () => endpoint2)).toBe(endpoint2); | ||
|
||
expect(cache.size()).toEqual(3); | ||
}); | ||
|
||
it("should accept a custom parameter list", () => { | ||
const cache = new EndpointCache({ | ||
size: 50, | ||
params: ["A", "B"], | ||
}); | ||
|
||
expect(cache.get({ A: "b", B: "b" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.get({ A: "b", B: "b", C: "c" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.get({ A: "b", B: "b", C: "cc" }, () => endpoint2)).toBe(endpoint1); | ||
|
||
expect(cache.size()).toEqual(1); | ||
}); | ||
|
||
it("should be an LRU cache", () => { | ||
const cache = new EndpointCache({ | ||
size: 5, | ||
params: ["A", "B"], | ||
}); | ||
|
||
for (let i = 0; i < 50; ++i) { | ||
cache.get({ A: "b", B: "b" + i }, () => endpoint1); | ||
} | ||
|
||
const size = cache.size(); | ||
expect(size).toBeLessThan(16); | ||
expect(cache.get({ A: "b", B: "b49" }, () => endpoint2)).toBe(endpoint1); | ||
expect(cache.size()).toEqual(size); | ||
|
||
expect(cache.get({ A: "b", B: "b1" }, () => endpoint2)).toBe(endpoint2); | ||
expect(cache.size()).toEqual(size + 1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { EndpointParams, EndpointV2 } from "@smithy/types"; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Cache for endpoint ruleSet resolution. | ||
*/ | ||
export class EndpointCache { | ||
private capacity: number = 50; | ||
private data = new Map<string, EndpointV2>(); | ||
private parameters: string[] = []; | ||
|
||
/** | ||
* @param [params] - list of params to consider as part of the cache key. | ||
* | ||
* If the params list is not populated, all object keys will be considered. | ||
* This may be out of order depending on how the object is created and arrives to this class. | ||
*/ | ||
public constructor({ size, params }: { size?: number; params?: string[] }) { | ||
this.capacity = size ?? 50; | ||
if (params) { | ||
this.parameters = params; | ||
} | ||
} | ||
|
||
/** | ||
* @param endpointParams - query for endpoint. | ||
* @param resolver - provider of the value if not present. | ||
* @returns endpoint corresponding to the query. | ||
*/ | ||
public get(endpointParams: EndpointParams, resolver: () => EndpointV2): EndpointV2 { | ||
const key = this.hash(endpointParams); | ||
if (!this.data.has(key)) { | ||
if (this.data.size > this.capacity + 10) { | ||
const keys = this.data.keys(); | ||
let i = 0; | ||
while (true) { | ||
const { value, done } = keys.next(); | ||
this.data.delete(value); | ||
if (done || ++i > 10) { | ||
break; | ||
} | ||
} | ||
} | ||
this.data.set(key, resolver()); | ||
} | ||
return this.data.get(key)!; | ||
} | ||
|
||
public size() { | ||
return this.data.size; | ||
} | ||
|
||
private hash(endpointParams: EndpointParams): string { | ||
let buffer = ""; | ||
const params = this.parameters.length ? this.parameters : Object.keys(endpointParams); | ||
for (const param of params) { | ||
buffer += endpointParams[param] ?? "" + "|"; | ||
} | ||
return buffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.