Faster WebSocket reconnects

This commit is contained in:
Fedor Indutny 2021-06-09 15:28:54 -07:00 committed by GitHub
parent 3cac4a19e1
commit 17e6ec468e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 940 additions and 677 deletions

33
ts/util/BackOff.ts Normal file
View file

@ -0,0 +1,33 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export class BackOff {
private count = 0;
constructor(private readonly timeouts: ReadonlyArray<number>) {}
public get(): number {
return this.timeouts[this.count];
}
public getAndIncrement(): number {
const result = this.get();
if (!this.isFull()) {
this.count += 1;
}
return result;
}
public reset(): void {
this.count = 0;
}
public isFull(): boolean {
return this.count === this.timeouts.length - 1;
}
public getIndex(): number {
return this.count;
}
}