MCPcopy Index your code
hub / github.com/coder/coder / selectByLatency

Function selectByLatency

site/src/contexts/ProxyContext.tsx:245–269  ·  view source on GitHub ↗
(
	proxies: readonly Region[],
	latencies?: Record<string, ProxyLatencyReport>,
)

Source from the content-addressed store, hash-verified

243};
244
245const selectByLatency = (
246 proxies: readonly Region[],
247 latencies?: Record<string, ProxyLatencyReport>,
248): Region | undefined => {
249 if (!latencies) {
250 return undefined;
251 }
252
253 const proxyMap = Object.fromEntries(proxies.map((it) => [it.id, it]));
254 const best = Object.entries(latencies)
255 .map(([proxyId, latency]) => ({ id: proxyId, ...latency }))
256 // If the proxy is not in our list, or it is unhealthy, ignore it.
257 .filter((latency) => proxyMap[latency.id]?.healthy)
258 .sort((a, b) => a.latencyMS - b.latencyMS)
259 .at(0);
260
261 // Found a new best, use it!
262 if (best) {
263 const bestProxy = proxies.find((proxy) => proxy.id === best.id);
264 // Default to w/e it was before
265 return bestProxy;
266 }
267
268 return undefined;
269};
270
271const computeUsableURLS = (proxy?: Region): PreferredProxy => {
272 if (!proxy) {

Callers 1

getPreferredProxyFunction · 0.85

Calls 2

filterMethod · 0.80
findMethod · 0.80

Tested by

no test coverage detected