Skip to content

chore: disable auto proxy selection based on latency #8137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: disable auto pick proxy based on latency
  • Loading branch information
Emyrk committed Jun 21, 2023
commit f4f629134a4fd42f808ed67c95e4a4ab68c997e5
67 changes: 43 additions & 24 deletions site/src/contexts/ProxyContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
proxiesResp?.regions ?? [],
loadUserSelectedProxy(),
proxyLatencies,
// Do not auto select based on latencies, as inconsistent latencies can cause this
// to behave poorly.
false,
),
)
}, [proxiesResp, proxyLatencies])
Expand Down Expand Up @@ -208,6 +211,7 @@ export const getPreferredProxy = (
proxies: Region[],
selectedProxy?: Region,
latencies?: Record<string, ProxyLatencyReport>,
autoSelectBasedOnLatency = true,
): PreferredProxy => {
// If a proxy is selected, make sure it is in the list of proxies. If it is not
// we should default to the primary.
Expand All @@ -219,37 +223,52 @@ export const getPreferredProxy = (
if (!selectedProxy || !selectedProxy.healthy) {
// By default, use the primary proxy.
selectedProxy = proxies.find((proxy) => proxy.name === "primary")

// If we have latencies, then attempt to use the best proxy by latency instead.
if (latencies) {
const proxyMap = proxies.reduce((acc, proxy) => {
acc[proxy.id] = proxy
return acc
}, {} as Record<string, Region>)

const best = Object.keys(latencies)
.map((proxyId) => {
return {
id: proxyId,
...latencies[proxyId],
}
})
// If the proxy is not in our list, or it is unhealthy, ignore it.
.filter((latency) => proxyMap[latency.id]?.healthy)
.sort((a, b) => a.latencyMS - b.latencyMS)
.at(0)

// Found a new best, use it!
if (best) {
const bestProxy = proxies.find((proxy) => proxy.id === best.id)
// Default to w/e it was before
selectedProxy = bestProxy || selectedProxy
}
const best = selectByLatency(proxies, latencies)
if (autoSelectBasedOnLatency && best) {
selectedProxy = best
}
}

return computeUsableURLS(selectedProxy)
}

const selectByLatency = (
proxies: Region[],
latencies?: Record<string, ProxyLatencyReport>,
): Region | undefined => {
if (!latencies) {
return undefined
}

const proxyMap = proxies.reduce((acc, proxy) => {
acc[proxy.id] = proxy
return acc
}, {} as Record<string, Region>)

const best = Object.keys(latencies)
.map((proxyId) => {
return {
id: proxyId,
...latencies[proxyId],
}
})
// If the proxy is not in our list, or it is unhealthy, ignore it.
.filter((latency) => proxyMap[latency.id]?.healthy)
.sort((a, b) => a.latencyMS - b.latencyMS)
.at(0)

// Found a new best, use it!
if (best) {
const bestProxy = proxies.find((proxy) => proxy.id === best.id)
// Default to w/e it was before
return bestProxy
}

return undefined
}

const computeUsableURLS = (proxy?: Region): PreferredProxy => {
if (!proxy) {
// By default use relative links for the primary proxy.
Expand Down