From 3d72ed004be323e5ff96f015a12bb956b9066b64 Mon Sep 17 00:00:00 2001 From: Abdul Azeem Date: Wed, 20 Nov 2024 14:21:58 +0300 Subject: [PATCH] feat: Implement removeOldestFilter to manage filtersMap size --- src/api.ts | 10 ++++------ src/utils.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/api.ts b/src/api.ts index 577ffca5..33974137 100755 --- a/src/api.ts +++ b/src/api.ts @@ -26,6 +26,7 @@ import { calculateContractStorageAccountId, getSyncTime, removeFromNodeList, + removeOldestFilter, } from './utils' import crypto from 'crypto' import { logEventEmitter } from './logger' @@ -2786,8 +2787,7 @@ export const methods = { type: Types.FilterTypes.block, } if (filtersMap.size >= config.maxEntriesAllowed) { - filtersMap.clear() - console.log(`filtersMap cleared after ${config.maxEntriesAllowed} entries`) + removeOldestFilter(filtersMap) } filtersMap.set(filterId.toString(), internalFilter) @@ -2824,8 +2824,7 @@ export const methods = { type: Types.FilterTypes.pendingTransaction, } if (filtersMap.size >= config.maxEntriesAllowed) { - filtersMap.clear() - console.log(`filtersMap cleared after ${config.maxEntriesAllowed} entries`) + removeOldestFilter(filtersMap) } filtersMap.set(filterId.toString(), internalFilter) @@ -2929,8 +2928,7 @@ export const methods = { type: Types.FilterTypes.log, } if (filtersMap.size >= config.maxEntriesAllowed) { - filtersMap.clear() - console.log(`filtersMap cleared after ${config.maxEntriesAllowed} entries`) + removeOldestFilter(filtersMap) } filtersMap.set(filterId.toString(), internalFilter) diff --git a/src/utils.ts b/src/utils.ts index ffa63e69..d6f57fc9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -29,6 +29,7 @@ import { OriginalTxData, AccountTypesData, Account2, + InternalFilter } from './types' import Sntp from '@hapi/sntp' import { randomBytes, createHash } from 'crypto' @@ -1807,6 +1808,24 @@ export function hexToBN(hexString: string): BN { return new BN(hexString, 16) } +export function removeOldestFilter(filtersMap: Map): void { + let oldestKey: string | undefined; + let oldestTimestamp = Infinity; + + // Iterate through the map to find the oldest entry + for (const [key, value] of filtersMap) { + if (value.filter.lastQueriedTimestamp < oldestTimestamp) { + oldestTimestamp = value.filter.lastQueriedTimestamp; + oldestKey = key; + } + } + + // Remove the oldest entry + if (oldestKey !== undefined) { + filtersMap.delete(oldestKey); + } +} + class Semaphore { private queue: (() => void)[] = [] private value: number