-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[supervisor] Frontend: re-try + track "checkReady" (#20175)
* [ws-proxy] Log whenever we can't connect to an upstream * [protocol] Fix linter error * [supervisor] Frontend: re-connect every 5s + track behavior * [supervisor] Frontend: Guard connection re-try with feature flag "supervisor_check_ready_retry"
- Loading branch information
Showing
10 changed files
with
258 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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,59 @@ | ||
/** | ||
* Copyright (c) 2020 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. | ||
*/ | ||
|
||
import * as chai from "chai"; | ||
const expect = chai.expect; | ||
import { suite, test } from "@testdeck/mocha"; | ||
import { Timeout } from "./timeout"; | ||
|
||
@suite() | ||
export class TimeoutSpec { | ||
@test | ||
async testSimpleRun() { | ||
const timeout = new Timeout(1); | ||
timeout.start(); | ||
await timeout.await(); | ||
expect(timeout.signal()?.aborted).to.be.true; | ||
} | ||
|
||
@test | ||
async testSimpleRunNotStarted() { | ||
const timeout = new Timeout(1); | ||
await timeout.await(); | ||
expect(timeout.signal()).to.be.undefined; | ||
} | ||
|
||
@test | ||
async testRestart() { | ||
const timeout = new Timeout(20); | ||
timeout.start(); | ||
await timeout.await(); | ||
expect(timeout.signal()?.aborted).to.be.true; | ||
|
||
timeout.restart(); | ||
expect(timeout.signal()).to.not.be.undefined; | ||
expect(timeout.signal()?.aborted).to.be.false; | ||
await timeout.await(); | ||
expect(timeout.signal()?.aborted).to.be.true; | ||
} | ||
|
||
@test | ||
async testClear() { | ||
const timeout = new Timeout(1000); | ||
timeout.restart(); | ||
timeout.clear(); | ||
expect(timeout.signal()).to.be.undefined; | ||
} | ||
|
||
@test | ||
async testAbortCondition() { | ||
const timeout = new Timeout(1, () => false); // will never trigger abort | ||
timeout.start(); | ||
await new Promise((resolve) => setTimeout(resolve, 50)); | ||
expect(timeout.signal()).to.not.be.undefined; | ||
expect(timeout.signal()?.aborted).to.be.false; | ||
} | ||
} |
This file contains 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,80 @@ | ||
/** | ||
* 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 restartable timeout, based on an AbortController + setTimeout. | ||
* | ||
* Note: When cleared/reset, the AbortController is _NOT_ aborted. | ||
*/ | ||
export class Timeout { | ||
private _timer: NodeJS.Timeout | undefined; | ||
private _abortController: AbortController | undefined; | ||
|
||
/** | ||
* @param timeout The timeout in milliseconds. | ||
* @param abortCondition An optional condition evaluated on timeout: If set, the abort is only emitted if it evaluates to true. | ||
*/ | ||
constructor(readonly timeout: number, readonly abortCondition?: () => boolean) {} | ||
|
||
/** | ||
* Starts a new timeout (and clears the old one). Identical to `reset`. | ||
*/ | ||
public start() { | ||
this.restart(); | ||
} | ||
|
||
/** | ||
* Starts a new timeout (and clears the old one). | ||
*/ | ||
public restart() { | ||
this.clear(); | ||
|
||
const abortController = new AbortController(); | ||
this._abortController = abortController; | ||
this._timer = setTimeout(() => { | ||
if (this.abortCondition && !this.abortCondition()) { | ||
return; | ||
} | ||
|
||
abortController.abort(); | ||
}, this.timeout); | ||
} | ||
|
||
/** | ||
* Clears the current timeout. | ||
*/ | ||
public clear() { | ||
if (this._timer) { | ||
clearTimeout(this._timer); | ||
this._timer = undefined; | ||
} | ||
if (this._abortController) { | ||
this._abortController = undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Convenience method to await the timeout. | ||
* @returns | ||
*/ | ||
public async await(): Promise<boolean> { | ||
const abortController = this._abortController; | ||
if (!abortController) { | ||
return false; | ||
} | ||
|
||
return new Promise((resolve) => { | ||
abortController.signal.addEventListener("abort", () => resolve(true)); | ||
}); | ||
} | ||
|
||
/** | ||
* @returns The AbortSignal of the current timeout, if one is active. | ||
*/ | ||
public signal(): AbortSignal | undefined { | ||
return this._abortController?.signal; | ||
} | ||
} |
This file contains 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 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 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 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 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.