-
Notifications
You must be signed in to change notification settings - Fork 85
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 (#1385)
* feat(util-endpoint): add endpoint ruleset cache * handle shorthand argv * handle delimiter in cache key segment * no caching for missing param list
- Loading branch information
Showing
8 changed files
with
467 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,83 @@ | ||
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, | ||
params: ["A", "B", "C"], | ||
}); | ||
|
||
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("bypasses caching if param values include the cache key delimiter", () => { | ||
const cache = new EndpointCache({ | ||
size: 50, | ||
params: ["A", "B"], | ||
}); | ||
|
||
expect(cache.get({ A: "b", B: "aaa|;aaa" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.size()).toEqual(0); | ||
}); | ||
|
||
it("bypasses caching if param list is empty", () => { | ||
const cache = new EndpointCache({ | ||
size: 50, | ||
params: [], | ||
}); | ||
|
||
expect(cache.get({ A: "b", B: "b" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.size()).toEqual(0); | ||
}); | ||
|
||
it("bypasses caching if no param list is supplied", () => { | ||
const cache = new EndpointCache({ | ||
size: 50, | ||
}); | ||
|
||
expect(cache.get({ A: "b", B: "b" }, () => endpoint1)).toBe(endpoint1); | ||
expect(cache.size()).toEqual(0); | ||
}); | ||
|
||
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,78 @@ | ||
import type { EndpointParams, EndpointV2 } from "@smithy/types"; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Cache for endpoint ruleSet resolution. | ||
*/ | ||
export class EndpointCache { | ||
private capacity: number; | ||
private data = new Map<string, EndpointV2>(); | ||
private parameters: string[] = []; | ||
|
||
/** | ||
* @param [size] - desired average maximum capacity. A buffer of 10 additional keys will be allowed | ||
* before keys are dropped. | ||
* @param [params] - list of params to consider as part of the cache key. | ||
* | ||
* If the params list is not populated, no caching will happen. | ||
* 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 (key === false) { | ||
return resolver(); | ||
} | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* @returns cache key or false if not cachable. | ||
*/ | ||
private hash(endpointParams: EndpointParams): string | false { | ||
let buffer = ""; | ||
const { parameters } = this; | ||
if (parameters.length === 0) { | ||
return false; | ||
} | ||
for (const param of parameters) { | ||
const val = String(endpointParams[param] ?? ""); | ||
if (val.includes("|;")) { | ||
return false; | ||
} | ||
buffer += val + "|;"; | ||
} | ||
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.