Make TTL configurable on WsProvider & HttpProvider (#6123)

* Make TTL configurable on WsProvider & HttpProvider

* lint

* change ttl to cacheTtl
This commit is contained in:
Valentin Fernandez
2025-03-27 12:38:51 -03:00
committed by GitHub
parent a5c5f76aee
commit 7f7acd6421
3 changed files with 12 additions and 7 deletions
+6 -3
View File
@@ -9,7 +9,7 @@ import { fetch } from '@polkadot/x-fetch';
import { RpcCoder } from '../coder/index.js';
import defaults from '../defaults.js';
import { DEFAULT_CAPACITY, LRUCache } from '../lru.js';
import { DEFAULT_CAPACITY, DEFAULT_TTL, LRUCache } from '../lru.js';
const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead';
@@ -45,8 +45,11 @@ export class HttpProvider implements ProviderInterface {
/**
* @param {string} endpoint The endpoint url starting with http://
* @param {Record<string, string>} headers The headers provided to the underlying Http Endpoint
* @param {number} [cacheCapacity] Custom size of the HttpProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
* @param {number} [cacheTtl] Custom TTL of the HttpProvider LRUCache. Determines how long an object can live in the cache. Defaults to `DEFAULT_TTL` (30000)
*/
constructor (endpoint: string = defaults.HTTP_URL, headers: Record<string, string> = {}, cacheCapacity?: number) {
constructor (endpoint: string = defaults.HTTP_URL, headers: Record<string, string> = {}, cacheCapacity?: number, cacheTtl?: number) {
if (!/^(https|http):\/\//.test(endpoint)) {
throw new Error(`Endpoint should start with 'http://' or 'https://', received '${endpoint}'`);
}
@@ -54,7 +57,7 @@ export class HttpProvider implements ProviderInterface {
this.#coder = new RpcCoder();
this.#endpoint = endpoint;
this.#headers = headers;
this.#callCache = new LRUCache(cacheCapacity === 0 ? 0 : cacheCapacity || DEFAULT_CAPACITY);
this.#callCache = new LRUCache(cacheCapacity === 0 ? 0 : cacheCapacity || DEFAULT_CAPACITY, cacheTtl || DEFAULT_TTL);
this.#cacheCapacity = cacheCapacity === 0 ? 0 : cacheCapacity || DEFAULT_CAPACITY;
this.#stats = {
+2 -1
View File
@@ -6,6 +6,7 @@
// for Kusama/Polkadot/Substrate falls between 600-750K, 2x for estimate)
export const DEFAULT_CAPACITY = 1024;
export const DEFAULT_TTL = 30000;
class LRUNode {
readonly key: string;
@@ -46,7 +47,7 @@ export class LRUCache {
readonly #ttl: number;
constructor (capacity = DEFAULT_CAPACITY, ttl = 30000) {
constructor (capacity = DEFAULT_CAPACITY, ttl = DEFAULT_TTL) {
this.capacity = capacity;
this.#ttl = ttl;
this.#head = this.#tail = new LRUNode('<empty>', ttl);
+4 -3
View File
@@ -13,7 +13,7 @@ import { WebSocket } from '@polkadot/x-ws';
import { RpcCoder } from '../coder/index.js';
import defaults from '../defaults.js';
import { DEFAULT_CAPACITY, LRUCache } from '../lru.js';
import { DEFAULT_CAPACITY, DEFAULT_TTL, LRUCache } from '../lru.js';
import { getWSErrorString } from './errors.js';
interface SubscriptionHandler {
@@ -110,8 +110,9 @@ 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`
* @param {number} [cacheCapacity] Custom size of the WsProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
* @param {number} [cacheTtl] Custom TTL of the WsProvider LRUCache. Determines how long an object can live in the cache. Defaults to DEFAULT_TTL` (30000)
*/
constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record<string, string> = {}, timeout?: number, cacheCapacity?: number) {
constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record<string, string> = {}, timeout?: number, cacheCapacity?: number, cacheTtl?: number) {
const endpoints = Array.isArray(endpoint)
? endpoint
: [endpoint];
@@ -125,7 +126,7 @@ export class WsProvider implements ProviderInterface {
throw new Error(`Endpoint should start with 'ws://', received '${endpoint}'`);
}
});
this.#callCache = new LRUCache(cacheCapacity || DEFAULT_CAPACITY);
this.#callCache = new LRUCache(cacheCapacity || DEFAULT_CAPACITY, cacheTtl || DEFAULT_TTL);
this.#cacheCapacity = cacheCapacity || DEFAULT_CAPACITY;
this.#eventemitter = new EventEmitter();
this.#autoConnectMs = autoConnectMs || 0;