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 21, 2024
1 parent c8651b1 commit 46ea364
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 @@ -27,6 +27,7 @@ import {
getSyncTime,
removeFromNodeList,
sanitizeIpAndPort,
removeOldestFilter,
} from './utils'
import crypto from 'crypto'
import { logEventEmitter } from './logger'
Expand Down Expand Up @@ -2800,8 +2801,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 @@ -2838,8 +2838,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 @@ -2943,8 +2942,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 @@ -1838,6 +1839,24 @@ export function sanitizeIpAndPort(ipPort: string): { isValid: boolean; error?: s
return { isValid: true }
}

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 46ea364

Please sign in to comment.