vector-im-hydrogen-web/src/matrix/net/ExponentialRetryDelay.ts

135 lines
4.0 KiB
TypeScript
Raw Normal View History

2020-08-05 18:38:55 +02:00
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-11-16 09:51:47 +01:00
import {AbortError} from "../../utils/error";
2021-11-21 16:42:28 +01:00
import type {Timeout} from "../../platform/web/dom/Clock.js";
type TimeoutCreator = (ms: number) => Timeout;
const enum Default { start = 2000 }
2020-04-19 19:02:10 +02:00
export class ExponentialRetryDelay {
2021-11-21 16:42:28 +01:00
private readonly _start: number = Default.start;
private _current: number = Default.start;
private readonly _createTimeout: TimeoutCreator;
private readonly _max: number;
private _timeout?: Timeout;
constructor(createTimeout: TimeoutCreator) {
const start = 2000;
2020-04-19 19:02:10 +02:00
this._start = start;
this._current = start;
this._createTimeout = createTimeout;
this._max = 60 * 5 * 1000; //5 min
}
2021-11-21 16:42:28 +01:00
async waitForRetry(): Promise<void> {
2020-04-19 19:02:10 +02:00
this._timeout = this._createTimeout(this._current);
try {
await this._timeout.elapsed();
// only increase delay if we didn't get interrupted
const next = 2 * this._current;
this._current = Math.min(this._max, next);
} catch(err) {
// swallow AbortError, means abort was called
if (!(err instanceof AbortError)) {
throw err;
}
} finally {
2021-11-21 16:42:28 +01:00
this._timeout = undefined;
2020-04-19 19:02:10 +02:00
}
}
2021-11-21 16:42:28 +01:00
abort(): void {
2020-04-19 19:02:10 +02:00
if (this._timeout) {
this._timeout.abort();
}
}
2021-11-21 16:42:28 +01:00
reset(): void {
2020-04-19 19:02:10 +02:00
this._current = this._start;
this.abort();
}
2021-11-21 16:42:28 +01:00
get nextValue(): number {
2020-04-19 19:02:10 +02:00
return this._current;
}
}
import {Clock as MockClock} from "../../mocks/Clock.js";
2020-04-19 19:02:10 +02:00
export function tests() {
return {
"test sequence": async assert => {
const clock = new MockClock();
const retryDelay = new ExponentialRetryDelay(clock.createTimeout);
2020-04-19 19:02:10 +02:00
let promise;
assert.strictEqual(retryDelay.nextValue, 2000);
promise = retryDelay.waitForRetry();
clock.elapse(2000);
await promise;
assert.strictEqual(retryDelay.nextValue, 4000);
promise = retryDelay.waitForRetry();
clock.elapse(4000);
await promise;
assert.strictEqual(retryDelay.nextValue, 8000);
promise = retryDelay.waitForRetry();
clock.elapse(8000);
await promise;
assert.strictEqual(retryDelay.nextValue, 16000);
promise = retryDelay.waitForRetry();
clock.elapse(16000);
await promise;
assert.strictEqual(retryDelay.nextValue, 32000);
promise = retryDelay.waitForRetry();
clock.elapse(32000);
await promise;
assert.strictEqual(retryDelay.nextValue, 64000);
promise = retryDelay.waitForRetry();
clock.elapse(64000);
await promise;
assert.strictEqual(retryDelay.nextValue, 128000);
promise = retryDelay.waitForRetry();
clock.elapse(128000);
await promise;
assert.strictEqual(retryDelay.nextValue, 256000);
promise = retryDelay.waitForRetry();
clock.elapse(256000);
await promise;
assert.strictEqual(retryDelay.nextValue, 300000);
promise = retryDelay.waitForRetry();
clock.elapse(300000);
await promise;
assert.strictEqual(retryDelay.nextValue, 300000);
promise = retryDelay.waitForRetry();
clock.elapse(300000);
await promise;
},
}
}