Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: set default workspace proxy based on latency#17812

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Emyrk merged 21 commits intomainfromstevenmasley/auto_select_first
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from7 commits
Commits
Show all changes
21 commits
Select commitHold shift + click to select a range
ab3f897
feat: default workspace proxy based on latency
EmyrkMay 13, 2025
fd172b7
always save the proxy
EmyrkMay 13, 2025
5de9d61
save the first latency report back
EmyrkMay 13, 2025
e0b9eb3
add comments
EmyrkMay 13, 2025
5427b61
unit test
EmyrkMay 13, 2025
dcf98d7
comment
EmyrkMay 13, 2025
93bc6b3
fmt
EmyrkMay 13, 2025
f5c0719
add latenciesLoaded to unit tests
EmyrkMay 14, 2025
f2b5d9c
fixup tests
EmyrkMay 14, 2025
b3102b7
fixup test fixtures
EmyrkMay 14, 2025
d07e472
fmt
EmyrkMay 14, 2025
abf49ae
remove console log, whoops
EmyrkMay 14, 2025
6755899
add comments
EmyrkMay 14, 2025
3383a80
Merge branch 'main' into stevenmasley/auto_select_first
EmyrkMay 14, 2025
fdb545c
Merge remote-tracking branch 'origin/main' into stevenmasley/auto_sel…
EmyrkMay 15, 2025
f5b76e4
Merge branch 'main' into stevenmasley/auto_select_first
EmyrkMay 19, 2025
a528411
Merge branch 'main' into stevenmasley/auto_select_first
EmyrkMay 23, 2025
8cc71ea
Merge remote-tracking branch 'origin/main' into stevenmasley/auto_sel…
EmyrkMay 27, 2025
9d9e7f3
revert to main
EmyrkMay 27, 2025
6036996
fix js test
EmyrkMay 27, 2025
01aebed
fix js test
EmyrkMay 27, 2025
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
10 changes: 7 additions & 3 deletionssite/src/contexts/ProxyContext.test.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,11 @@ import type * as ProxyLatency from "./useProxyLatency";
// here and not inside a unit test.
jest.mock("contexts/useProxyLatency", () => ({
useProxyLatency: () => {
return { proxyLatencies: hardCodedLatencies, refetch: jest.fn() };
return {
proxyLatencies: hardCodedLatencies,
refetch: jest.fn(),
loaded: true,
};
},
}));

Expand DownExpand Up@@ -261,11 +265,11 @@ describe("ProxyContextSelection", () => {
expUserProxyID: MockHealthyWildWorkspaceProxy.id,
},
],
//Latency behavior is disabled, so theprimary should be selected.
//First page load defers to theproxy by latency
[
"regions_default_low_latency",
{
expProxyID:MockPrimaryWorkspaceProxy.id,
expProxyID:MockHealthyWildWorkspaceProxy.id,
regions: MockWorkspaceProxies,
storageProxy: undefined,
latencies: {
Expand Down
46 changes: 40 additions & 6 deletionssite/src/contexts/ProxyContext.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,8 +122,11 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {

// Every time we get a new proxiesResponse, update the latency check
// to each workspace proxy.
const { proxyLatencies, refetch: refetchProxyLatencies } =
useProxyLatency(proxiesResp);
const {
proxyLatencies,
refetch: refetchProxyLatencies,
loaded: latenciesLoaded,
} = useProxyLatency(proxiesResp);

// updateProxy is a helper function that when called will
// update the proxy being used.
Expand All@@ -136,7 +139,8 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
loadUserSelectedProxy(),
proxyLatencies,
// Do not auto select based on latencies, as inconsistent latencies can cause this
// to behave poorly.
// to change on each call. updateProxy should be stable when selecting a proxy to
// prevent flickering.
false,
),
);
Expand All@@ -149,6 +153,34 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
updateProxy();
}, [proxiesResp, proxyLatencies]);

// This useEffect will auto select the best proxy if the user has not selected one.
// It must wait until all latencies are loaded to select based on latency. This does mean
// the first time a user loads the page, the proxy will "flicker" to the best proxy.
//
// Once the page is loaded, or the user selects a proxy, this will not run again.
// biome-ignore lint/correctness/useExhaustiveDependencies: Only update if the source data changes
useEffect(() => {
if (loadUserSelectedProxy() !== undefined) {
return; // User has selected a proxy, do not auto select.
}
if (!latenciesLoaded) {
// Wait until the latencies are loaded before
return;
}

const best = getPreferredProxy(
proxiesResp ?? [],
loadUserSelectedProxy(),
proxyLatencies,
true,
);

if (best?.proxy) {
saveUserSelectedProxy(best.proxy);
updateProxy();
}
}, [latenciesLoaded, proxiesResp, proxyLatencies]);

return (
<ProxyContext.Provider
value={{
Expand DownExpand Up@@ -214,14 +246,16 @@ export const getPreferredProxy = (

// If no proxy is selected, or the selected proxy is unhealthy default to the primary proxy.
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.
const best = selectByLatency(proxies, latencies);
if (autoSelectBasedOnLatency && best) {
selectedProxy = best;
}

// Use the primary proxy if we don't have any other options.
if (!selectedProxy) {
selectedProxy = proxies.find((proxy) => proxy.name === "primary");
}
}

return computeUsableURLS(selectedProxy);
Expand Down
5 changes: 5 additions & 0 deletionssite/src/contexts/useProxyLatency.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,6 +48,7 @@ export const useProxyLatency = (
// Until the new values are loaded, the old values will still be used.
refetch: () => Date;
proxyLatencies: Record<string, ProxyLatencyReport>;
loaded: boolean;
} => {
// maxStoredLatencies is the maximum number of latencies to store per proxy in local storage.
let maxStoredLatencies = 1;
Expand All@@ -73,6 +74,8 @@ export const useProxyLatency = (
new Date(new Date().getTime() - proxyIntervalSeconds * 1000).toISOString(),
);

const [loaded, setLoaded] = useState(false);
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is how I track when the latencies are done loading, since they all come back async


// Refetch will always set the latestFetchRequest to the current time, making all the cached latencies
// stale and triggering a refetch of all proxies in the list.
const refetch = () => {
Expand DownExpand Up@@ -231,6 +234,7 @@ export const useProxyLatency = (

// Local storage cleanup
garbageCollectStoredLatencies(proxies, maxStoredLatencies);
setLoaded(true);
});

return () => {
Expand All@@ -241,6 +245,7 @@ export const useProxyLatency = (
return {
proxyLatencies,
refetch,
loaded,
};
};

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp