Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 7 additions & 11 deletions components/dashboard/src/service/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { VerificationService } from "@gitpod/public-api/lib/gitpod/v1/verificati
import { JsonRpcInstallationClient } from "./json-rpc-installation-client";
import { InstallationService } from "@gitpod/public-api/lib/gitpod/v1/installation_connect";
import { JsonRpcUserClient } from "./json-rpc-user-client";
import { Timeout } from "@gitpod/gitpod-protocol/lib/util/timeout";

const transport = createConnectTransport({
baseUrl: `${window.location.protocol}//${window.location.host}/public-api`,
Expand Down Expand Up @@ -274,24 +275,17 @@ export function stream<Response>(
const abort = new AbortController();
(async () => {
while (!abort.signal.aborted) {
const connectionTimeout = new Timeout(10_000);
try {
let dataReceived = false;
let attemptTimeoutId: NodeJS.Timeout | null;
attemptTimeoutId = setTimeout(() => {
if (!dataReceived) {
throw new Error("Attempt Timeout: No initial data received within 10 seconds");
}
}, 10_000);
connectionTimeout.start();

for await (const response of factory({
signal: abort.signal,
signal: AbortSignal.any([abort.signal, connectionTimeout.signal()!]),
Copy link
Member Author

Choose a reason for hiding this comment

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

TIL learned about AbortSignal.any, kind of made my day 😆

// GCP timeout is 10 minutes, we timeout 3 mins earlier
// to avoid unknown network errors and reconnect gracefully
timeoutMs: 7 * 60 * 1000,
})) {
dataReceived = true;
attemptTimeoutId && clearTimeout(attemptTimeoutId);
attemptTimeoutId = null;
connectionTimeout.clear(); // connection is alive now, clear timeout

backoff = BASE_BACKOFF;
cb(response);
Expand All @@ -314,6 +308,8 @@ export function stream<Response>(
backoff = Math.min(2 * backoff, MAX_BACKOFF);
console.error(e);
}
} finally {
connectionTimeout.clear();
}
const jitter = Math.random() * 0.3 * backoff;
const delay = backoff + jitter;
Expand Down
55 changes: 55 additions & 0 deletions components/gitpod-protocol/src/util/timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2024 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

/**
* A resettable timeout, based on an AbortController + setTimeout.
*/
export class Timeout {
private _timer: NodeJS.Timeout | undefined;
private _abortController: AbortController | undefined;

constructor(readonly timeout: number, readonly abortCondition?: () => boolean) {}

/**
* Starts a new timeout (and clears the old one). Identical to `reset`.
*/
public start() {
this.reset();
}

/**
* Starts a new timeout (and clears the old one).
*/
public reset() {
this.clear();

const abortController = new AbortController();
this._abortController = abortController;
this._timer = setTimeout(() => {
if (this.abortCondition && this.abortCondition()) {
return;
}

abortController.abort();
}, this.timeout);
}

public clear() {
if (this._timer) {
clearTimeout(this._timer);
this._timer = undefined;
}
if (this._abortController) {
this._abortController = undefined;
}
}

public signal(): AbortSignal | undefined {
return this._abortController?.signal;
}
}

export class CombinedAbortSignal extends AbortSignal {}
Loading