feat(rpc-provider): allow cacheCapacity option for WsProvider (#5778)
* feat(rpc-provider): allow cacheCapacity option for WsProvider * change args ordering to avoid breaking change
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
// Assuming all 1.5MB responses, we apply a default allowing for 192MB
|
// Assuming all 1.5MB responses, we apply a default allowing for 192MB
|
||||||
// cache space (depending on the historic queries this would vary, metadata
|
// cache space (depending on the historic queries this would vary, metadata
|
||||||
// for Kusama/Polkadot/Substrate falls between 600-750K, 2x for estimate)
|
// for Kusama/Polkadot/Substrate falls between 600-750K, 2x for estimate)
|
||||||
const DEFAULT_CAPACITY = 128;
|
export const DEFAULT_CAPACITY = 128;
|
||||||
|
|
||||||
class LRUNode {
|
class LRUNode {
|
||||||
readonly key: string;
|
readonly key: string;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { WebSocket } from '@polkadot/x-ws';
|
|||||||
|
|
||||||
import { RpcCoder } from '../coder/index.js';
|
import { RpcCoder } from '../coder/index.js';
|
||||||
import defaults from '../defaults.js';
|
import defaults from '../defaults.js';
|
||||||
import { LRUCache } from '../lru.js';
|
import { DEFAULT_CAPACITY, LRUCache } from '../lru.js';
|
||||||
import { getWSErrorString } from './errors.js';
|
import { getWSErrorString } from './errors.js';
|
||||||
|
|
||||||
interface SubscriptionHandler {
|
interface SubscriptionHandler {
|
||||||
@@ -83,7 +83,7 @@ function defaultEndpointStats (): EndpointStats {
|
|||||||
* @see [[HttpProvider]]
|
* @see [[HttpProvider]]
|
||||||
*/
|
*/
|
||||||
export class WsProvider implements ProviderInterface {
|
export class WsProvider implements ProviderInterface {
|
||||||
readonly #callCache = new LRUCache();
|
readonly #callCache: LRUCache;
|
||||||
readonly #coder: RpcCoder;
|
readonly #coder: RpcCoder;
|
||||||
readonly #endpoints: string[];
|
readonly #endpoints: string[];
|
||||||
readonly #headers: Record<string, string>;
|
readonly #headers: Record<string, string>;
|
||||||
@@ -108,7 +108,7 @@ export class WsProvider implements ProviderInterface {
|
|||||||
* @param {Record<string, string>} headers The headers provided to the underlying WebSocket
|
* @param {Record<string, string>} headers The headers provided to the underlying WebSocket
|
||||||
* @param {number} [timeout] Custom timeout value used per request . Defaults to `DEFAULT_TIMEOUT_MS`
|
* @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<string, string> = {}, timeout?: number) {
|
constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record<string, string> = {}, timeout?: number, cacheCapacity?: number) {
|
||||||
const endpoints = Array.isArray(endpoint)
|
const endpoints = Array.isArray(endpoint)
|
||||||
? endpoint
|
? endpoint
|
||||||
: [endpoint];
|
: [endpoint];
|
||||||
@@ -122,7 +122,7 @@ export class WsProvider implements ProviderInterface {
|
|||||||
throw new Error(`Endpoint should start with 'ws://', received '${endpoint}'`);
|
throw new Error(`Endpoint should start with 'ws://', received '${endpoint}'`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.#callCache = new LRUCache(cacheCapacity || DEFAULT_CAPACITY);
|
||||||
this.#eventemitter = new EventEmitter();
|
this.#eventemitter = new EventEmitter();
|
||||||
this.#autoConnectMs = autoConnectMs || 0;
|
this.#autoConnectMs = autoConnectMs || 0;
|
||||||
this.#coder = new RpcCoder();
|
this.#coder = new RpcCoder();
|
||||||
|
|||||||
Reference in New Issue
Block a user