*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": { "dependencies": {
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git", "@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git", "@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git",
"backoff": "^2.5.0", "backoff.js": "^1.0.4",
"eventemitter3": "^5.0.0" "eventemitter3": "^5.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/backoff": "^2.5.2",
"@types/node": "^18.13.0", "@types/node": "^18.13.0",
"prettier": "^2.8.4", "prettier": "^2.8.4",
"pretty": "^2.0.0", "pretty": "^2.0.0",

View File

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