- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: Auto select workspace proxy based on lowest latency#7515
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
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
159538c1305931d1c9f3fc739f24ae26777523e492877c0c98d266435c534faa3703e1c210e8da358258ec860c33197eb1ebb6a76549093eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -32,9 +32,6 @@ export interface ProxyContextValue { | ||
| // The value `proxy` should always be used instead of `userProxy`. `userProxy` is only exposed | ||
| // so the caller can determine if the proxy being used is the user's selected proxy, or if it | ||
| // was auto selected based on some other criteria. | ||
Kira-Pilot marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| userProxy?: Region | ||
| // proxies is the list of proxies returned by coderd. This is fetched async. | ||
| @@ -79,27 +76,18 @@ export const ProxyContext = createContext<ProxyContextValue | undefined>( | ||
| * ProxyProvider interacts with local storage to indicate the preferred workspace proxy. | ||
| */ | ||
| export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => { | ||
| const dashboard = useDashboard() | ||
| const experimentEnabled = dashboard?.experiments.includes("moons") | ||
| // Using a useState so the caller always has the latest user saved | ||
| // proxy. | ||
| const [userSavedProxy, setUserSavedProxy] = useState(loadUserSelectedProxy()) | ||
| // Load the initial state from local storage. | ||
| const [proxy, setProxy] = useState<PreferredProxy>( | ||
| computeUsableURLS(userSavedProxy), | ||
| ) | ||
| const queryKey = ["get-proxies"] | ||
| const { | ||
| data: proxiesResp, | ||
| @@ -109,57 +97,39 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => { | ||
| } = useQuery({ | ||
| queryKey, | ||
| queryFn: getWorkspaceProxies, | ||
| }) | ||
| // Every time we get a new proxiesResponse, update the latency check | ||
| // to each workspace proxy. | ||
| const proxyLatencies = useProxyLatency(proxiesResp) | ||
| // updateProxy is a helper function that when called will | ||
| // update the proxy being used. | ||
| const updateProxy = useCallback(() => { | ||
| // Update the saved user proxy for the caller. | ||
| setUserSavedProxy(loadUserSelectedProxy()) | ||
Kira-Pilot marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| setProxy( | ||
| getPreferredProxy( | ||
| proxiesResp?.regions ?? [], | ||
| // For some reason if I use 'userSavedProxy' here, | ||
| // the tests fail. It does not load "undefined" after a | ||
| // clear, but takes the previous value. | ||
| loadUserSelectedProxy(), | ||
Member
| ||
| proxyLatencies, | ||
| ), | ||
| ) | ||
| }, [userSavedProxy, proxiesResp, proxyLatencies]) | ||
Emyrk marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| // This useEffect ensures the proxy to be used is updated whenever the state changes. | ||
| // This includes proxies being loaded, latencies being calculated, and the user selecting a proxy. | ||
| useEffect(() => { | ||
| updateProxy() | ||
| }, [proxiesResp, proxyLatencies]) | ||
| return ( | ||
| <ProxyContext.Provider | ||
| value={{ | ||
| userProxy:userSavedProxy, | ||
| proxyLatencies: proxyLatencies, | ||
| proxy: experimentEnabled | ||
| ? proxy | ||
| @@ -177,13 +147,13 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => { | ||
| setProxy: (proxy: Region) => { | ||
| // Save to local storage to persist the user's preference across reloads | ||
| saveUserSelectedProxy(proxy) | ||
| //Update theselected proxy | ||
| updateProxy() | ||
| }, | ||
| clearProxy: () => { | ||
| // Clear the user's selection from local storage. | ||
| clearUserSelectedProxy() | ||
| updateProxy() | ||
| }, | ||
| }} | ||
| > | ||