Faster WebSocket reconnects
This commit is contained in:
parent
3cac4a19e1
commit
17e6ec468e
25 changed files with 940 additions and 677 deletions
33
ts/util/BackOff.ts
Normal file
33
ts/util/BackOff.ts
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue