Skip to content

Commit

Permalink
clear intervals in RPCcore
Browse files Browse the repository at this point in the history
  • Loading branch information
filvecchiato committed Oct 15, 2024
1 parent 2f8b5a1 commit fc040ab
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/rpc-core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export class RpcCore {
/**
* @description Manually disconnect from the attached provider
*/
public disconnect (): Promise<void> {
public async disconnect (): Promise<void> {
await this.#storageCache.clearInterval();
return this.provider.disconnect();

Check failure on line 151 in packages/rpc-core/src/bundle.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Expected blank line before this statement
}

Expand Down
13 changes: 6 additions & 7 deletions packages/rpc-provider/src/lru.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@

import { LRUCache } from './lru.js';



describe('LRUCache', (): void => {
let lru: LRUCache | undefined;

beforeEach((): void => {
lru = new LRUCache(4);
});

afterEach(async () => {
await lru?.clearInterval();
lru = undefined;
});
it('allows getting of items below capacity', async (): Promise<void> => {

it('allows getting of items below capacity', (): void => {
const keys = ['1', '2', '3', '4'];

keys.forEach((k) => lru?.set(k, `${k}${k}${k}`));
let lruKeys = lru?.keys();
const lruKeys = lru?.keys();

expect(lruKeys && lruKeys.join(', ')).toBe(keys.reverse().join(', '));
expect(lruKeys?.join(', ')).toBe(keys.reverse().join(', '));
expect(lru?.length === lru?.lengthData && lru?.length === lru?.lengthRefs).toBe(true);

keys.forEach((k) => expect(lru?.get(k)).toEqual(`${k}${k}${k}`));
Expand Down
30 changes: 10 additions & 20 deletions packages/rpc-provider/src/lru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
// cache space (depending on the historic queries this would vary, metadata
// for Kusama/Polkadot/Substrate falls between 600-750K, 2x for estimate)

import {createWriteStream} from 'fs'
import { config } from 'process'

console.log = async (message: any) => {
const tty = createWriteStream('/dev/tty')
const msg = typeof message === 'string' ? message : JSON.stringify(message, null, 2)
return tty.write(msg + '\n')
}

export const DEFAULT_CAPACITY = 64;

class LRUNode {
Expand Down Expand Up @@ -53,7 +44,7 @@ export class LRUCache {
// TTL
readonly #ttl: number;
readonly #ttlInterval: number;
#ttlP: NodeJS.Timeout | undefined = undefined;
#ttlTimerId: ReturnType<typeof setInterval> | null = null;

constructor (capacity = DEFAULT_CAPACITY, ttl = 30000, ttlInterval = 15000) {
this.capacity = capacity;
Expand Down Expand Up @@ -135,6 +126,7 @@ export class LRUCache {
this.#toHead(key);
} else {
const node = new LRUNode(key);

this.#refs.set(node.key, node);

if (this.length === 0) {
Expand All @@ -156,15 +148,13 @@ export class LRUCache {
}
}

if (this.#ttl > 0 && !this.#ttlP) {
this.#ttlP = setInterval(() => {
if (this.#ttl > 0 && !this.#ttlTimerId) {
this.#ttlTimerId = setInterval(() => {
this.#ttlClean();
}, this.#ttlInterval);
}

this.#data.set(key, value);

return;
}

#ttlClean () {
Expand All @@ -173,9 +163,9 @@ export class LRUCache {

// traverse map to find the lastAccessed
while (this.#tail.lastAccess && this.#tail.lastAccess < expires && this.#length > 0) {
if (this.#ttlP && this.#length === 0) {
clearInterval(this.#ttlP);
this.#ttlP = undefined;
if (this.#ttlTimerId && this.#length === 0) {
clearInterval(this.#ttlTimerId);
this.#ttlTimerId = null;
this.#head = this.#tail = new LRUNode('<empty>');
} else {
this.#refs.delete(this.#tail.key);
Expand Down Expand Up @@ -203,9 +193,9 @@ export class LRUCache {

// eslint-disable-next-line @typescript-eslint/require-await
public async clearInterval (): Promise<void> {
if (this.#ttlP) {
clearInterval(this.#ttlP);
this.#ttlP = undefined;
if (this.#ttlTimerId) {
clearInterval(this.#ttlTimerId);
this.#ttlTimerId = null;
}
}
}

0 comments on commit fc040ab

Please sign in to comment.