*Switch to backoff.js

This commit is contained in:
Derrick Hammer 2023-02-17 09:25:58 -05:00
parent acbc5408c9
commit ff7266f05e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 12 additions and 18 deletions

View File

@ -6,11 +6,10 @@
"dependencies": {
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git",
"backoff": "^2.5.0",
"backoff.js": "^1.0.4",
"eventemitter3": "^5.0.0"
},
"devDependencies": {
"@types/backoff": "^2.5.2",
"@types/node": "^18.13.0",
"prettier": "^2.8.4",
"pretty": "^2.0.0",

View File

@ -4,13 +4,14 @@ import { DataFn, ErrTuple, hexToBuf } from "@siaweb/libweb";
import type { EventEmitter } from "eventemitter3";
import backoff, { Backoff } from "backoff";
// @ts-ignore
import Backoff from "backoff.js";
export class SwarmClient extends Client {
private useDefaultSwarm: boolean;
private id: number = 0;
private _autoReconnect: boolean;
private _connectBackoff: Backoff;
private _connectBackoff: any;
private _ready?: Promise<void>;
@ -18,10 +19,9 @@ export class SwarmClient extends Client {
super();
this.useDefaultSwarm = useDefaultDht;
this._autoReconnect = autoReconnect;
this._connectBackoff = backoff.fibonacci();
this._connectBackoff.on("ready", () => {
this.start();
this._connectBackoff = new Backoff({
strategy: "fibo",
maxAttempts: 0,
});
}
@ -57,19 +57,14 @@ export class SwarmClient extends Client {
await this._ready;
this._ready = undefined;
this._connectBackoff.reset();
}
async start(): Promise<void> {
const backoff = () => setImmediate(() => this._connectBackoff.backoff());
this._connectBackoff.run(() => this.init());
try {
await this.init();
} catch (e) {
this.logErr(e);
backoff();
return;
}
this._connectBackoff.on("retry", (error: any) => {
this.logErr(error);
});
await this.ready();
}
@ -84,7 +79,7 @@ export class SwarmClient extends Client {
);
await connect[1];
this._connectBackoff.backoff();
this.start();
}
public async addRelay(pubkey: string): Promise<void> {