Skip to content

Commit

Permalink
Make Websocket retry configurable and expose websocket stats (#126)
Browse files Browse the repository at this point in the history
* [integritee-worker] allow passing in the timeouts in the websocket connection

* [integritee-worker] add some more diagnostic options

* [integritee-worker] better worker constructor

* v0.18.1
  • Loading branch information
clangenb authored Feb 1, 2025
1 parent 629159e commit dac61d4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"publishConfig": {
"directory": "build"
},
"version": "0.18.0"
"version": "0.18.1"
}
4 changes: 2 additions & 2 deletions packages/node-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
},
"sideEffects": false,
"type": "module",
"version": "0.18.0",
"version": "0.18.1",
"main": "index.js",
"dependencies": {
"@encointer/types": "^0.18.0",
"@encointer/types": "^0.18.1",
"@polkadot/api": "^11.2.1",
"tslib": "^2.6.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.18.0",
"version": "0.18.1",
"main": "index.js",
"scripts": {
"generate:defs": "node --experimental-specifier-resolution=node --loader ts-node/esm ../../node_modules/.bin/polkadot-types-from-defs --package @encointer/types/interfaces --input ./src/interfaces",
Expand Down
2 changes: 1 addition & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"sideEffects": false,
"type": "module",
"types": "./index.d.ts",
"version": "0.18.0",
"version": "0.18.1",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.18.9",
Expand Down
8 changes: 4 additions & 4 deletions packages/worker-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
"sideEffects": false,
"type": "module",
"types": "./index.d.ts",
"version": "0.18.0",
"version": "0.18.1",
"main": "index.js",
"dependencies": {
"@encointer/node-api": "^0.18.0",
"@encointer/types": "^0.18.0",
"@encointer/util": "^0.18.0",
"@encointer/node-api": "^0.18.1",
"@encointer/types": "^0.18.1",
"@encointer/util": "^0.18.1",
"@peculiar/webcrypto": "^1.4.6",
"@polkadot/api": "^11.2.1",
"@polkadot/keyring": "^12.6.2",
Expand Down
4 changes: 3 additions & 1 deletion packages/worker-api/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ export interface ISubmittableGetter<W extends IWorkerBase, Type> {
}

export interface WorkerOptions {
autoConnectMs?: number,
timeout?: number,
createWebSocket?: (url: string) => WebSocket;
keyring?: Keyring;
types?: RegistryTypes;
createWebSocket?: (url: string) => WebSocket;
}

export interface TrustedGetterArgs {
Expand Down
29 changes: 26 additions & 3 deletions packages/worker-api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import BN from "bn.js";
import {WsProvider} from "./rpc-provider/src/index.js";
import {Keyring} from "@polkadot/keyring";
import type {Hash} from "@polkadot/types/interfaces/runtime";
import type {EndpointStats, ProviderStats} from "@encointer/worker-api/rpc-provider/src/types.js";

const RETRY_DELAY = 2_500;
const DEFAULT_TIMEOUT_MS = 60 * 1000;

export class Worker implements IWorkerBase {

Expand All @@ -37,14 +41,17 @@ export class Worker implements IWorkerBase {

#ws: WsProvider;

constructor(url: string, options: WorkerOptions = {} as WorkerOptions) {
constructor(
endpoint: string | string[],
options: WorkerOptions = {} as WorkerOptions
) {
this.#registry = new TypeRegistry();
this.#keyring = (options.keyring || undefined);

// We want to pass arguments to NodeJS' websocket implementation into the provider
// in our integration tests, so that we can accept the workers self-signed
// certificate. Hence, we inject the factory function.
this.#ws = new WsProvider(url, 100, undefined, undefined, undefined, options.createWebSocket);
this.#ws = new WsProvider(endpoint, options.autoConnectMs || RETRY_DELAY, undefined, options.timeout || DEFAULT_TIMEOUT_MS, undefined, options.createWebSocket);

if (options.types != undefined) {
this.#registry.register(encointerOptions({types: options.types}).types as RegistryTypes);
Expand All @@ -61,6 +68,23 @@ export class Worker implements IWorkerBase {
return this.#ws.disconnect()
}

public async connect(): Promise<void> {
// retry is after `autoConnectMs` from the constructor.
return this.#ws.connectWithRetry()
}

public get isConnected(): boolean {
return this.#ws.isConnected
}

public get wsStats(): ProviderStats {
return this.#ws.stats
}

public get endpointStats(): EndpointStats {
return this.#ws.endpointStats
}

public async encrypt(data: Uint8Array): Promise<Vec<u8>> {
const dataBE = new BN(data);
const dataArrayBE = new Uint8Array(dataBE.toArray());
Expand All @@ -85,7 +109,6 @@ export class Worker implements IWorkerBase {
this.#keyring = keyring;
}


public registry(): TypeRegistry {
return this.#registry
}
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -755,18 +755,18 @@ __metadata:
languageName: node
linkType: hard

"@encointer/node-api@npm:^0.18.0, @encointer/node-api@workspace:packages/node-api":
"@encointer/node-api@npm:^0.18.1, @encointer/node-api@workspace:packages/node-api":
version: 0.0.0-use.local
resolution: "@encointer/node-api@workspace:packages/node-api"
dependencies:
"@encointer/types": "npm:^0.18.0"
"@encointer/types": "npm:^0.18.1"
"@polkadot/api": "npm:^11.2.1"
"@polkadot/util-crypto": "npm:^12.6.2"
tslib: "npm:^2.6.2"
languageName: unknown
linkType: soft

"@encointer/types@npm:^0.18.0, @encointer/types@workspace:packages/types":
"@encointer/types@npm:^0.18.1, @encointer/types@workspace:packages/types":
version: 0.0.0-use.local
resolution: "@encointer/types@workspace:packages/types"
dependencies:
Expand All @@ -781,7 +781,7 @@ __metadata:
languageName: unknown
linkType: soft

"@encointer/util@npm:^0.18.0, @encointer/util@workspace:packages/util":
"@encointer/util@npm:^0.18.1, @encointer/util@workspace:packages/util":
version: 0.0.0-use.local
resolution: "@encointer/util@workspace:packages/util"
dependencies:
Expand All @@ -798,9 +798,9 @@ __metadata:
version: 0.0.0-use.local
resolution: "@encointer/worker-api@workspace:packages/worker-api"
dependencies:
"@encointer/node-api": "npm:^0.18.0"
"@encointer/types": "npm:^0.18.0"
"@encointer/util": "npm:^0.18.0"
"@encointer/node-api": "npm:^0.18.1"
"@encointer/types": "npm:^0.18.1"
"@encointer/util": "npm:^0.18.1"
"@peculiar/webcrypto": "npm:^1.4.6"
"@polkadot/api": "npm:^11.2.1"
"@polkadot/keyring": "npm:^12.6.2"
Expand Down

0 comments on commit dac61d4

Please sign in to comment.