-
Notifications
You must be signed in to change notification settings - Fork 42
feat: add network slowness detection with configurable threshold #891
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
36e8672
feat: add network slowness detection with configurable thresholds (#887)
EhabY 9e4101d
fix: drop throughput-based slowness detection, polish tooltip
EhabY 8b7a5ee
refactor: simplify network status rendering and address PR feedback
EhabY 6648a7e
Address review comments
EhabY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import prettyBytes from "pretty-bytes"; | ||
| import * as vscode from "vscode"; | ||
|
|
||
| import type { NetworkInfo } from "./sshProcess"; | ||
|
|
||
| /** Number of consecutive polls required to trigger or clear a warning */ | ||
| const WARNING_DEBOUNCE_THRESHOLD = 2; | ||
|
|
||
| const WARNING_BACKGROUND = new vscode.ThemeColor( | ||
| "statusBarItem.warningBackground", | ||
| ); | ||
|
|
||
| const CODER_CONNECT_TEXT = "$(globe) Coder Connect"; | ||
| const CODER_CONNECT_TOOLTIP = markdown( | ||
| "$(cloud) Connected using Coder Connect. Detailed network stats aren't collected for this connection type.", | ||
| ); | ||
|
|
||
| interface NetworkThresholds { | ||
| latencyMs: number; | ||
| } | ||
|
|
||
| function connectionSummary(network: NetworkInfo): string { | ||
| if (network.p2p) { | ||
| return "$(zap) Directly connected peer-to-peer."; | ||
| } | ||
| return `$(broadcast) Connected via ${network.preferred_derp} relay. Will switch to peer-to-peer when available.`; | ||
| } | ||
|
|
||
| function buildStatusText(network: NetworkInfo, isStale: boolean): string { | ||
| const label = network.p2p ? "Direct" : network.preferred_derp; | ||
| const staleMarker = isStale ? "~" : ""; | ||
| return `$(globe) ${label} (${staleMarker}${network.latency.toFixed(2)}ms)`; | ||
| } | ||
|
|
||
| /** | ||
| * Manages network status bar presentation. | ||
| * Warning state is debounced over consecutive polls to avoid flicker. | ||
| */ | ||
| export class NetworkStatusReporter { | ||
| private warningCounter = 0; | ||
| private isWarningActive = false; | ||
|
|
||
| constructor(private readonly statusBarItem: vscode.StatusBarItem) {} | ||
|
|
||
| update(network: NetworkInfo, isStale: boolean): void { | ||
| // Coder Connect doesn't populate latency/throughput, so we show a dedicated | ||
| // message and skip the slowness machinery entirely. | ||
| if (network.using_coder_connect) { | ||
| this.warningCounter = 0; | ||
| this.isWarningActive = false; | ||
| this.statusBarItem.text = CODER_CONNECT_TEXT; | ||
| this.statusBarItem.tooltip = CODER_CONNECT_TOOLTIP; | ||
| this.statusBarItem.backgroundColor = undefined; | ||
| this.statusBarItem.command = undefined; | ||
| this.statusBarItem.show(); | ||
| return; | ||
| } | ||
|
|
||
| const thresholds: NetworkThresholds = { | ||
| latencyMs: vscode.workspace | ||
| .getConfiguration("coder") | ||
| .get<number>("networkThreshold.latencyMs", 250), | ||
| }; | ||
| const isSlow = | ||
| thresholds.latencyMs > 0 && network.latency > thresholds.latencyMs; | ||
| this.updateWarningState(isSlow); | ||
|
|
||
| this.statusBarItem.text = buildStatusText(network, isStale); | ||
| this.statusBarItem.tooltip = this.buildTooltip( | ||
| network, | ||
| thresholds, | ||
| isStale, | ||
| ); | ||
| this.statusBarItem.backgroundColor = this.isWarningActive | ||
| ? WARNING_BACKGROUND | ||
| : undefined; | ||
| this.statusBarItem.command = this.isWarningActive | ||
| ? "coder.pingWorkspace" | ||
| : undefined; | ||
| this.statusBarItem.show(); | ||
| } | ||
|
|
||
| private updateWarningState(isSlow: boolean): void { | ||
| if (isSlow) { | ||
| this.warningCounter = Math.min( | ||
| this.warningCounter + 1, | ||
| WARNING_DEBOUNCE_THRESHOLD, | ||
| ); | ||
| } else { | ||
| this.warningCounter = Math.max(this.warningCounter - 1, 0); | ||
| } | ||
|
|
||
| if (this.warningCounter >= WARNING_DEBOUNCE_THRESHOLD) { | ||
| this.isWarningActive = true; | ||
| } else if (this.warningCounter === 0) { | ||
| this.isWarningActive = false; | ||
| } | ||
| } | ||
|
|
||
| private buildTooltip( | ||
| network: NetworkInfo, | ||
| thresholds: NetworkThresholds, | ||
| isStale: boolean, | ||
| ): vscode.MarkdownString { | ||
| const fmt = (bytesPerSec: number) => | ||
| prettyBytes(bytesPerSec * 8, { bits: true }) + "/s"; | ||
|
|
||
| const sections: string[] = []; | ||
| if (this.isWarningActive) { | ||
| sections.push("$(warning) **Slow connection detected**"); | ||
| } | ||
| sections.push(connectionSummary(network)); | ||
|
|
||
| const thresholdSuffix = | ||
| thresholds.latencyMs > 0 ? ` (threshold: ${thresholds.latencyMs}ms)` : ""; | ||
| const metrics = [ | ||
| `Latency: ${network.latency.toFixed(2)}ms${thresholdSuffix}`, | ||
| `Download: ${fmt(network.download_bytes_sec)}`, | ||
| `Upload: ${fmt(network.upload_bytes_sec)}`, | ||
| ]; | ||
| // Two trailing spaces + \n = hard line break (tight rows within a section). | ||
| sections.push(metrics.join(" \n")); | ||
|
|
||
| if (this.isWarningActive) { | ||
| sections.push( | ||
| "[$(pulse) Run latency test](command:coder.pingWorkspace) · " + | ||
| "[$(gear) Configure threshold](command:workbench.action.openSettings?%22coder.networkThreshold%22)", | ||
| ); | ||
| } | ||
|
|
||
| if (isStale) { | ||
| sections.push( | ||
| "$(history) Readings are stale; waiting for a fresh sample.", | ||
| ); | ||
| } | ||
|
|
||
| // Blank line between sections = paragraph break. | ||
| return markdown(sections.join("\n\n")); | ||
| } | ||
| } | ||
|
|
||
| function markdown(value: string): vscode.MarkdownString { | ||
| const md = new vscode.MarkdownString(value); | ||
| md.isTrusted = true; | ||
| md.supportThemeIcons = true; | ||
| return md; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.