Expose combination of provider + core stats (#5696)
* Expose combination of provider + core stats * Non-existing core stats
This commit is contained in:
@@ -7,7 +7,7 @@ import type { StorageKey, Vec } from '@polkadot/types';
|
||||
import type { Hash } from '@polkadot/types/interfaces';
|
||||
import type { AnyJson, AnyNumber, Codec, DefinitionRpc, DefinitionRpcExt, DefinitionRpcSub, Registry } from '@polkadot/types/types';
|
||||
import type { Memoized } from '@polkadot/util/types';
|
||||
import type { RpcInterfaceMethod } from './types/index.js';
|
||||
import type { RpcCoreStats, RpcInterfaceMethod } from './types/index.js';
|
||||
|
||||
import { Observable, publishReplay, refCount } from 'rxjs';
|
||||
|
||||
@@ -94,6 +94,8 @@ export class RpcCore {
|
||||
readonly #isPedantic: boolean;
|
||||
readonly #registryDefault: Registry;
|
||||
readonly #storageCache = new Map<string, Codec>();
|
||||
#storageCacheHits = 0;
|
||||
#storageCacheSize = 0;
|
||||
|
||||
#getBlockRegistry?: (blockHash: Uint8Array) => Promise<{ registry: Registry }>;
|
||||
#getBlockHash?: (blockNumber: AnyNumber) => Promise<Uint8Array>;
|
||||
@@ -147,6 +149,23 @@ export class RpcCore {
|
||||
return this.provider.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the underlying core stats, including those from teh provider
|
||||
*/
|
||||
public get stats (): RpcCoreStats | undefined {
|
||||
const stats = this.provider.stats;
|
||||
|
||||
return stats
|
||||
? {
|
||||
...stats,
|
||||
core: {
|
||||
cacheHits: this.#storageCacheHits,
|
||||
cacheSize: this.#storageCacheSize
|
||||
}
|
||||
}
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Sets a registry swap (typically from Api)
|
||||
*/
|
||||
@@ -453,6 +472,8 @@ export class RpcCore {
|
||||
const cached = this.#storageCache.get(hexKey);
|
||||
|
||||
if (cached) {
|
||||
this.#storageCacheHits++;
|
||||
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
@@ -470,6 +491,7 @@ export class RpcCore {
|
||||
// clearing of it, so very long running processes (not just a couple of hours, longer)
|
||||
// will increase memory beyond what is allowed.
|
||||
this.#storageCache.set(hexKey, codec);
|
||||
this.#storageCacheSize++;
|
||||
|
||||
return codec;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ProviderInterface } from '@polkadot/rpc-provider/types';
|
||||
import type { AnyFunction, Codec, DefinitionRpc } from '@polkadot/types/types';
|
||||
|
||||
export interface RpcInterfaceMethod {
|
||||
@@ -14,3 +15,14 @@ export type AugmentedRpc<F extends AnyFunction> = F & {
|
||||
raw: <T> (...params: Parameters<F>) => Observable<T>;
|
||||
meta: DefinitionRpc;
|
||||
};
|
||||
|
||||
/** Stats from the rpc-core layer, including the provider stats */
|
||||
export interface RpcCoreStats extends NonNullable<ProviderInterface['stats']> {
|
||||
/** Internal stats for the rpc-core layer */
|
||||
core: {
|
||||
/** The number of values retrieved from the core cache */
|
||||
cacheHits: number;
|
||||
/** The number of entries in the core cache */
|
||||
cacheSize: number;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user