src/vs/workbench/services/remote/common/remoteAgentService.ts

159 LOC · 99 covered · 60 uncovered · 2 ranges · 1145 concepts · 1 introducers · 695 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- remoteAgentService.ts ×2
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
7 > import { RemoteAgentConnectionContext, IRemoteAgentEnvironment } from '../../../../platform/remote/common/remoteAgentEnvironment.js';
8 > import { IChannel, IServerChannel } from '../../../../base/parts/ipc/common/ipc.js';
9 > import { IDiagnosticInfoOptions, IDiagnosticInfo } from '../../../../platform/diagnostics/common/diagnostics.js';
10 > import { Event } from '../../../../base/common/event.js';
11 > import { PersistentConnectionEvent } from '../../../../platform/remote/common/remoteAgentConnection.js';
12 > import { ITelemetryData, TelemetryLevel } from '../../../../platform/telemetry/common/telemetry.js';
13 > import { timeout } from '../../../../base/common/async.js';
14 >
15 > export const IRemoteAgentService = createDecorator<IRemoteAgentService>('remoteAgentService');
16 >
17 > export interface IRemoteAgentService {
18 > readonly _serviceBrand: undefined;
19 >
20 > getConnection(): IRemoteAgentConnection | null;
21 > /**
22 > * Get the remote environment. In case of an error, returns `null`.
23 > */
24 > getEnvironment(): Promise<IRemoteAgentEnvironment | null>;
25 > /**
26 > * Get the remote environment. Can return an error.
27 > */
28 > getRawEnvironment(): Promise<IRemoteAgentEnvironment | null>;
29 > /**
30 > * Get exit information for a remote extension host.
31 > */
32 > getExtensionHostExitInfo(reconnectionToken: string): Promise<IExtensionHostExitInfo | null>;
33 >
34 > /**
35 > * Gets the round trip time from the remote extension host. Note that this
36 > * may be delayed if the extension host is busy.
37 > */
38 > getRoundTripTime(): Promise<number | undefined>;
39 >
40 > /**
41 > * Gracefully ends the current connection, if any.
42 > */
43 > endConnection(): Promise<void>;
44 >
45 > getDiagnosticInfo(options: IDiagnosticInfoOptions): Promise<IDiagnosticInfo | undefined>;
46 > updateTelemetryLevel(telemetryLevel: TelemetryLevel): Promise<void>;
47 > logTelemetry(eventName: string, data?: ITelemetryData): Promise<void>;
48 > flushTelemetry(): Promise<void>;
49 > }
50 >
51 > export interface IExtensionHostExitInfo {
52 > code: number;
53 > signal: string;
54 > }
55 >
56 > export interface IRemoteAgentConnection {
57 > readonly remoteAuthority: string;
58 >
59 > readonly onReconnecting: Event<void>;
60 > readonly onDidStateChange: Event<PersistentConnectionEvent>;
61 >
62 > end(): Promise<void>;
63 > dispose(): void;
64 > getChannel<T extends IChannel>(channelName: string): T;
65 > withChannel<T extends IChannel, R>(channelName: string, callback: (channel: T) => Promise<R>): Promise<R>;
66 > registerChannel<T extends IServerChannel<RemoteAgentConnectionContext>>(channelName: string, channel: T): void;
67 > getInitialConnectionTimeMs(): Promise<number>;
68 > updateGraceTime(graceTime: number): void;
69 > }
70 >
71 > export interface IRemoteConnectionLatencyMeasurement {
72 >
73 > readonly initial: number | undefined;
74 > readonly current: number;
75 > readonly average: number;
76 >
77 > readonly high: boolean;
78 > }
79 >
80 > export const remoteConnectionLatencyMeasurer = new class {
81 >
82 > readonly maxSampleCount = 5;
83 > readonly sampleDelay = 2000;
84 >
85 > readonly initial: number[] = [];
86 > readonly maxInitialCount = 3;
87 >
88 > readonly average: number[] = [];
89 > readonly maxAverageCount = 100;
90 >
91 > readonly highLatencyMultiple = 2;
92 > readonly highLatencyMinThreshold = 500;
93 > readonly highLatencyMaxThreshold = 1500;
94 >
95 > lastMeasurement: IRemoteConnectionLatencyMeasurement | undefined = undefined;
96 > get latency() { return this.lastMeasurement; }
97 >
98 > async measure(remoteAgentService: IRemoteAgentService): Promise<IRemoteConnectionLatencyMeasurement | undefined> {
99 let currentLatency = Infinity;
100
101 // Measure up to samples count
102 for (let i = 0; i < this.maxSampleCount; i++) {
103 const rtt = await remoteAgentService.getRoundTripTime();
104 if (rtt === undefined) {
105 return undefined;
106 }
107
108 currentLatency = Math.min(currentLatency, rtt / 2 /* we want just one way, not round trip time */);
109 await timeout(this.sampleDelay);
110 }
111
112 // Keep track of average latency
113 this.average.push(currentLatency);
114 if (this.average.length > this.maxAverageCount) {
115 this.average.shift();
116 }
117
118 // Keep track of initial latency
119 let initialLatency: number | undefined = undefined;
120 if (this.initial.length < this.maxInitialCount) {
121 this.initial.push(currentLatency);
122 } else {
123 initialLatency = this.initial.reduce((sum, value) => sum + value, 0) / this.initial.length;
124 }
125
126 // Remember as last measurement
127 this.lastMeasurement = {
128 initial: initialLatency,
129 current: currentLatency,
130 average: this.average.reduce((sum, value) => sum + value, 0) / this.average.length,
131 high: (() => {
132
133 // based on the initial, average and current latency, try to decide
134 // if the connection has high latency
135 // Some rules:
136 // - we require the initial latency to be computed
137 // - we only consider latency above highLatencyMinThreshold as potentially high
138 // - we require the current latency to be above the average latency by a factor of highLatencyMultiple
139 // - but not if the latency is actually above highLatencyMaxThreshold
140
141 if (typeof initialLatency === 'undefined') {
142 return false;
143 }
144
145 if (currentLatency > this.highLatencyMaxThreshold) {
146 return true;
147 }
148
149 if (currentLatency > this.highLatencyMinThreshold && currentLatency > initialLatency * this.highLatencyMultiple) {
150 return true;
151 }
152
153 return false;
154 })()
155 };
156
157 return this.lastMeasurement;
158 }