Skip to content

Commit

Permalink
feat(rpc-provider): allow cacheCapacity option for WsProvider (#5778)
Browse files Browse the repository at this point in the history
* feat(rpc-provider): allow cacheCapacity option for WsProvider

* change args ordering to avoid breaking change
  • Loading branch information
TarikGul authored Jan 26, 2024
1 parent 5de73b7 commit c5371c1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/rpc-provider/src/lru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/rpc-provider/src/ws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<string, string>;
Expand All @@ -108,7 +108,7 @@ export class WsProvider implements ProviderInterface {
* @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`
*/
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)
? endpoint
: [endpoint];
Expand All @@ -122,7 +122,7 @@ export class WsProvider implements ProviderInterface {
throw new Error(`Endpoint should start with 'ws://', received '${endpoint}'`);
}
});

this.#callCache = new LRUCache(cacheCapacity || DEFAULT_CAPACITY);
this.#eventemitter = new EventEmitter();
this.#autoConnectMs = autoConnectMs || 0;
this.#coder = new RpcCoder();
Expand Down

0 comments on commit c5371c1

Please sign in to comment.