Skip to content

Commit

Permalink
feat: Implement removeOldestFilter to manage filtersMap size
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulazeem-tk4vr committed Nov 20, 2024
1 parent 27e5918 commit 3d72ed0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
calculateContractStorageAccountId,
getSyncTime,
removeFromNodeList,
removeOldestFilter,
} from './utils'
import crypto from 'crypto'
import { logEventEmitter } from './logger'
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
OriginalTxData,
AccountTypesData,
Account2,
InternalFilter
} from './types'
import Sntp from '@hapi/sntp'
import { randomBytes, createHash } from 'crypto'
Expand Down Expand Up @@ -1807,6 +1808,24 @@ export function hexToBN(hexString: string): BN {
return new BN(hexString, 16)
}

export function removeOldestFilter(filtersMap: Map<string, InternalFilter>): 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
Expand Down

0 comments on commit 3d72ed0

Please sign in to comment.