@@ -136,6 +136,9 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
136
136
proxiesResp ?. regions ?? [ ] ,
137
137
loadUserSelectedProxy ( ) ,
138
138
proxyLatencies ,
139
+ // Do not auto select based on latencies, as inconsistent latencies can cause this
140
+ // to behave poorly.
141
+ false ,
139
142
) ,
140
143
)
141
144
} , [ proxiesResp , proxyLatencies ] )
@@ -208,6 +211,7 @@ export const getPreferredProxy = (
208
211
proxies : Region [ ] ,
209
212
selectedProxy ?: Region ,
210
213
latencies ?: Record < string , ProxyLatencyReport > ,
214
+ autoSelectBasedOnLatency = true ,
211
215
) : PreferredProxy => {
212
216
// If a proxy is selected, make sure it is in the list of proxies. If it is not
213
217
// we should default to the primary.
@@ -219,37 +223,52 @@ export const getPreferredProxy = (
219
223
if ( ! selectedProxy || ! selectedProxy . healthy ) {
220
224
// By default, use the primary proxy.
221
225
selectedProxy = proxies . find ( ( proxy ) => proxy . name === "primary" )
226
+
222
227
// 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
247
231
}
248
232
}
249
233
250
234
return computeUsableURLS ( selectedProxy )
251
235
}
252
236
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
+
253
272
const computeUsableURLS = ( proxy ?: Region ) : PreferredProxy => {
254
273
if ( ! proxy ) {
255
274
// By default use relative links for the primary proxy.
0 commit comments