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

Function getPreferredProxy

site/src/contexts/ProxyContext.tsx:218–243  ·  view source on GitHub ↗
(
	proxies: readonly Region[],
	selectedProxy?: Region,
	latencies?: Record<string, ProxyLatencyReport>,
	autoSelectBasedOnLatency = true,
)

Source from the content-addressed store, hash-verified

216 * If not, `primary` is always the best default.
217 */
218export const getPreferredProxy = (
219 proxies: readonly Region[],
220 selectedProxy?: Region,
221 latencies?: Record<string, ProxyLatencyReport>,
222 autoSelectBasedOnLatency = true,
223): PreferredProxy => {
224 // If a proxy is selected, make sure it is in the list of proxies. If it is not
225 // we should default to the primary.
226 selectedProxy = proxies.find(
227 (proxy) => selectedProxy && proxy.id === selectedProxy.id,
228 );
229
230 // If no proxy is selected, or the selected proxy is unhealthy default to the primary proxy.
231 if (!selectedProxy?.healthy) {
232 // Default to the primary proxy
233 selectedProxy = proxies.find((proxy) => proxy.name === "primary");
234
235 // If we have latencies, then attempt to use the best proxy by latency instead.
236 const best = selectByLatency(proxies, latencies);
237 if (autoSelectBasedOnLatency && best !== undefined) {
238 selectedProxy = best;
239 }
240 }
241
242 return computeUsableURLS(selectedProxy);
243};
244
245const selectByLatency = (
246 proxies: readonly Region[],

Callers 8

withProxyProviderFunction · 0.90
ProxyProviderFunction · 0.85

Calls 3

selectByLatencyFunction · 0.85
computeUsableURLSFunction · 0.85
findMethod · 0.80

Tested by

no test coverage detected