Skip to content

Commit 5639541

Browse files
authored
chore: disable auto proxy selection based on latency (#8137)
* chore: disable auto pick proxy based on latency * Remove latency pulled from storage
1 parent 82415a6 commit 5639541

File tree

3 files changed

+48
-53
lines changed

3 files changed

+48
-53
lines changed

site/src/contexts/ProxyContext.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ describe("ProxyContextSelection", () => {
264264
expUserProxyID: MockHealthyWildWorkspaceProxy.id,
265265
},
266266
],
267-
// Latency behavior
267+
// Latency behavior is disabled, so the primary should be selected.
268268
[
269269
"regions_default_low_latency",
270270
{
271-
expProxyID: MockHealthyWildWorkspaceProxy.id,
271+
expProxyID: MockPrimaryWorkspaceProxy.id,
272272
regions: MockWorkspaceProxies,
273273
storageProxy: undefined,
274274
latencies: {

site/src/contexts/ProxyContext.tsx

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
136136
proxiesResp?.regions ?? [],
137137
loadUserSelectedProxy(),
138138
proxyLatencies,
139+
// Do not auto select based on latencies, as inconsistent latencies can cause this
140+
// to behave poorly.
141+
false,
139142
),
140143
)
141144
}, [proxiesResp, proxyLatencies])
@@ -208,6 +211,7 @@ export const getPreferredProxy = (
208211
proxies: Region[],
209212
selectedProxy?: Region,
210213
latencies?: Record<string, ProxyLatencyReport>,
214+
autoSelectBasedOnLatency = true,
211215
): PreferredProxy => {
212216
// If a proxy is selected, make sure it is in the list of proxies. If it is not
213217
// we should default to the primary.
@@ -219,37 +223,52 @@ export const getPreferredProxy = (
219223
if (!selectedProxy || !selectedProxy.healthy) {
220224
// By default, use the primary proxy.
221225
selectedProxy = proxies.find((proxy) => proxy.name === "primary")
226+
222227
// If we have latencies, then attempt to use the best proxy by latency instead.
223-
if (latencies) {
224-
const proxyMap = proxies.reduce((acc, proxy) => {
225-
acc[proxy.id] = proxy
226-
return acc
227-
}, {} as Record<string, Region>)
228-
229-
const best = Object.keys(latencies)
230-
.map((proxyId) => {
231-
return {
232-
id: proxyId,
233-
...latencies[proxyId],
234-
}
235-
})
236-
// If the proxy is not in our list, or it is unhealthy, ignore it.
237-
.filter((latency) => proxyMap[latency.id]?.healthy)
238-
.sort((a, b) => a.latencyMS - b.latencyMS)
239-
.at(0)
240-
241-
// Found a new best, use it!
242-
if (best) {
243-
const bestProxy = proxies.find((proxy) => proxy.id === best.id)
244-
// Default to w/e it was before
245-
selectedProxy = bestProxy || selectedProxy
246-
}
228+
const best = selectByLatency(proxies, latencies)
229+
if (autoSelectBasedOnLatency && best) {
230+
selectedProxy = best
247231
}
248232
}
249233

250234
return computeUsableURLS(selectedProxy)
251235
}
252236

237+
const selectByLatency = (
238+
proxies: Region[],
239+
latencies?: Record<string, ProxyLatencyReport>,
240+
): Region | undefined => {
241+
if (!latencies) {
242+
return undefined
243+
}
244+
245+
const proxyMap = proxies.reduce((acc, proxy) => {
246+
acc[proxy.id] = proxy
247+
return acc
248+
}, {} as Record<string, Region>)
249+
250+
const best = Object.keys(latencies)
251+
.map((proxyId) => {
252+
return {
253+
id: proxyId,
254+
...latencies[proxyId],
255+
}
256+
})
257+
// If the proxy is not in our list, or it is unhealthy, ignore it.
258+
.filter((latency) => proxyMap[latency.id]?.healthy)
259+
.sort((a, b) => a.latencyMS - b.latencyMS)
260+
.at(0)
261+
262+
// Found a new best, use it!
263+
if (best) {
264+
const bestProxy = proxies.find((proxy) => proxy.id === best.id)
265+
// Default to w/e it was before
266+
return bestProxy
267+
}
268+
269+
return undefined
270+
}
271+
253272
const computeUsableURLS = (proxy?: Region): PreferredProxy => {
254273
if (!proxy) {
255274
// By default use relative links for the primary proxy.

site/src/contexts/useProxyLatency.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,8 @@ const proxyLatenciesReducer = (
2424
state: Record<string, ProxyLatencyReport>,
2525
action: ProxyLatencyAction,
2626
): Record<string, ProxyLatencyReport> => {
27-
// TODO: We should probably not read from local storage on every action.
28-
const history = loadStoredLatencies()
29-
const proxyHistory = history[action.proxyID] || []
30-
const minReport = proxyHistory.reduce((min, report) => {
31-
if (min.latencyMS === 0) {
32-
// Not yet set, so use the new report.
33-
return report
34-
}
35-
if (min.latencyMS < report.latencyMS) {
36-
return min
37-
}
38-
return report
39-
}, {} as ProxyLatencyReport)
40-
41-
if (
42-
minReport.latencyMS > 0 &&
43-
minReport.latencyMS < action.report.latencyMS
44-
) {
45-
// The new report is slower then the min report, so use the min report.
46-
return {
47-
...state,
48-
[action.proxyID]: minReport,
49-
}
50-
}
51-
52-
// Use the new report
27+
// Always return the new report. We have some saved latencies, but until we have a better
28+
// way to utilize them, we will ignore them for all practical purposes.
5329
return {
5430
...state,
5531
[action.proxyID]: action.report,
@@ -65,7 +41,7 @@ export const useProxyLatency = (
6541
proxyLatencies: Record<string, ProxyLatencyReport>
6642
} => {
6743
// maxStoredLatencies is the maximum number of latencies to store per proxy in local storage.
68-
let maxStoredLatencies = 8
44+
let maxStoredLatencies = 1
6945
// The reason we pull this from local storage is so for development purposes, a user can manually
7046
// set a larger number to collect data in their normal usage. This data can later be analyzed to come up
7147
// with some better magic numbers.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy