From bd69555866eaeb4fdab6388124e1383c0d0f30f2 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Thu, 11 Jan 2024 10:35:35 -0500 Subject: [PATCH 1/2] feat(rpc-provider): allow cacheCapacity option for WsProvider --- packages/rpc-provider/src/lru.ts | 2 +- packages/rpc-provider/src/ws/index.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/rpc-provider/src/lru.ts b/packages/rpc-provider/src/lru.ts index be62b1c0b319..b9afa882acd4 100644 --- a/packages/rpc-provider/src/lru.ts +++ b/packages/rpc-provider/src/lru.ts @@ -4,7 +4,7 @@ // Assuming all 1.5MB responses, we apply a default allowing for 192MB // cache space (depending on the historic queries this would vary, metadata // for Kusama/Polkadot/Substrate falls between 600-750K, 2x for estimate) -const DEFAULT_CAPACITY = 128; +export const DEFAULT_CAPACITY = 128; class LRUNode { readonly key: string; diff --git a/packages/rpc-provider/src/ws/index.ts b/packages/rpc-provider/src/ws/index.ts index 04dc37bdd56f..2fe85bd00178 100644 --- a/packages/rpc-provider/src/ws/index.ts +++ b/packages/rpc-provider/src/ws/index.ts @@ -12,7 +12,7 @@ import { WebSocket } from '@polkadot/x-ws'; import { RpcCoder } from '../coder/index.js'; import defaults from '../defaults.js'; -import { LRUCache } from '../lru.js'; +import { DEFAULT_CAPACITY, LRUCache } from '../lru.js'; import { getWSErrorString } from './errors.js'; interface SubscriptionHandler { @@ -83,7 +83,7 @@ function defaultEndpointStats (): EndpointStats { * @see [[HttpProvider]] */ export class WsProvider implements ProviderInterface { - readonly #callCache = new LRUCache(); + readonly #callCache: LRUCache; readonly #coder: RpcCoder; readonly #endpoints: string[]; readonly #headers: Record; @@ -108,7 +108,7 @@ export class WsProvider implements ProviderInterface { * @param {Record} headers The headers provided to the underlying WebSocket * @param {number} [timeout] Custom timeout value used per request . Defaults to `DEFAULT_TIMEOUT_MS` */ - constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record = {}, timeout?: number) { + constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record = {}, cacheCapacity: number = DEFAULT_CAPACITY, timeout?: number) { const endpoints = Array.isArray(endpoint) ? endpoint : [endpoint]; @@ -122,7 +122,7 @@ export class WsProvider implements ProviderInterface { throw new Error(`Endpoint should start with 'ws://', received '${endpoint}'`); } }); - + this.#callCache = new LRUCache(cacheCapacity); this.#eventemitter = new EventEmitter(); this.#autoConnectMs = autoConnectMs || 0; this.#coder = new RpcCoder(); From c2121111f1f71d68f609de1fdacaaa28e4bc43a6 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 12 Jan 2024 09:04:49 -0500 Subject: [PATCH 2/2] change args ordering to avoid breaking change --- packages/rpc-provider/src/ws/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rpc-provider/src/ws/index.ts b/packages/rpc-provider/src/ws/index.ts index 2fe85bd00178..6528b00d782b 100644 --- a/packages/rpc-provider/src/ws/index.ts +++ b/packages/rpc-provider/src/ws/index.ts @@ -108,7 +108,7 @@ export class WsProvider implements ProviderInterface { * @param {Record} headers The headers provided to the underlying WebSocket * @param {number} [timeout] Custom timeout value used per request . Defaults to `DEFAULT_TIMEOUT_MS` */ - constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record = {}, cacheCapacity: number = DEFAULT_CAPACITY, timeout?: number) { + constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record = {}, timeout?: number, cacheCapacity?: number) { const endpoints = Array.isArray(endpoint) ? endpoint : [endpoint]; @@ -122,7 +122,7 @@ export class WsProvider implements ProviderInterface { throw new Error(`Endpoint should start with 'ws://', received '${endpoint}'`); } }); - this.#callCache = new LRUCache(cacheCapacity); + this.#callCache = new LRUCache(cacheCapacity || DEFAULT_CAPACITY); this.#eventemitter = new EventEmitter(); this.#autoConnectMs = autoConnectMs || 0; this.#coder = new RpcCoder();