File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
packages/endpoint-cache/src Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,26 @@ export class EndpointCache {
13
13
this . cache = new LRUCache ( capacity ) ;
14
14
}
15
15
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
+
16
24
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 ;
18
36
}
19
37
20
38
set ( key : string , endpoints : Endpoint [ ] ) {
You can’t perform that action at this time.
0 commit comments