Skip to content

Commit 6ea32e4

Browse files
authored
fix: show current DERP name correctly in vscode (#8856)
1 parent aa7b267 commit 6ea32e4

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

cli/vscodessh.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ type sshNetworkStats struct {
243243
}
244244

245245
func collectNetworkStats(ctx context.Context, agentConn *codersdk.WorkspaceAgentConn, start, end time.Time, counts map[netlogtype.Connection]netlogtype.Counts) (*sshNetworkStats, error) {
246-
latency, p2p, _, err := agentConn.Ping(ctx)
246+
latency, p2p, pingResult, err := agentConn.Ping(ctx)
247247
if err != nil {
248248
return nil, err
249249
}
@@ -284,10 +284,26 @@ func collectNetworkStats(ctx context.Context, agentConn *codersdk.WorkspaceAgent
284284
uploadSecs := float64(totalTx) / dur.Seconds()
285285
downloadSecs := float64(totalRx) / dur.Seconds()
286286

287+
// Sometimes the preferred DERP doesn't match the one we're actually
288+
// connected with. Perhaps because the agent prefers a different DERP and
289+
// we're using that server instead.
290+
preferredDerpID := node.PreferredDERP
291+
if pingResult.DERPRegionID != 0 {
292+
preferredDerpID = pingResult.DERPRegionID
293+
}
294+
preferredDerp, ok := derpMap.Regions[preferredDerpID]
295+
preferredDerpName := fmt.Sprintf("Unnamed %d", preferredDerpID)
296+
if ok {
297+
preferredDerpName = preferredDerp.RegionName
298+
}
299+
if _, ok := derpLatency[preferredDerpName]; !ok {
300+
derpLatency[preferredDerpName] = 0
301+
}
302+
287303
return &sshNetworkStats{
288304
P2P: p2p,
289305
Latency: float64(latency.Microseconds()) / 1000,
290-
PreferredDERP: derpMap.Regions[node.PreferredDERP].RegionName,
306+
PreferredDERP: preferredDerpName,
291307
DERPLatency: derpLatency,
292308
UploadBytesSec: int64(uploadSecs),
293309
DownloadBytesSec: int64(downloadSecs),

0 commit comments

Comments
 (0)