Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc-provider): allow cacheCapacity option for WsProvider #5778

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> = {}, cacheCapacity: number = DEFAULT_CAPACITY, timeout?: number) {
Copy link
Member

@jacogr jacogr Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather move this as the last parameter. (No breaking change - we really don't like breaking changes at all, unless very much required)

On use can do this.#callCache = new LRUCache(cacheCapacity || DEFAULT_CAPACITY); to apply defaults.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I just applied the change you recommended and I agree it makes more sense.

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);
this.#eventemitter = new EventEmitter();
this.#autoConnectMs = autoConnectMs || 0;
this.#coder = new RpcCoder();
Expand Down