TODO: handledEvents memory leak
This commit is contained in:
parent
abffc5c6df
commit
96ab25713f
1 changed files with 16 additions and 3 deletions
|
|
@ -3,7 +3,6 @@ import { SimplePool, Sub, Event, UnsignedEvent, getEventHash, finishEvent, relay
|
||||||
import { encryptData, decryptData, getSharedSecret, decodePayload, encodePayload } from './nip44.js'
|
import { encryptData, decryptData, getSharedSecret, decodePayload, encodePayload } from './nip44.js'
|
||||||
import { getLogger } from '../helpers/logger.js'
|
import { getLogger } from '../helpers/logger.js'
|
||||||
import { encodeNprofile } from '../../custom-nip19.js'
|
import { encodeNprofile } from '../../custom-nip19.js'
|
||||||
const handledEvents: string[] = [] // TODO: - big memory leak here, add TTL
|
|
||||||
type AppInfo = { appId: string, publicKey: string, privateKey: string, name: string }
|
type AppInfo = { appId: string, publicKey: string, privateKey: string, name: string }
|
||||||
export type SendData = { type: "content", content: string, pub: string } | { type: "event", event: UnsignedEvent }
|
export type SendData = { type: "content", content: string, pub: string } | { type: "event", event: UnsignedEvent }
|
||||||
export type NostrSend = (appId: string, data: SendData, relays?: string[] | undefined) => void
|
export type NostrSend = (appId: string, data: SendData, relays?: string[] | undefined) => void
|
||||||
|
|
@ -41,6 +40,20 @@ type EventResponse = {
|
||||||
|
|
||||||
export type ChildProcessRequest = SettingsRequest | SendRequest
|
export type ChildProcessRequest = SettingsRequest | SendRequest
|
||||||
export type ChildProcessResponse = ReadyResponse | EventResponse
|
export type ChildProcessResponse = ReadyResponse | EventResponse
|
||||||
|
|
||||||
|
const EVENT_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
||||||
|
const CLEANUP_INTERVAL_SECONDS = 60
|
||||||
|
const handledEvents: { eventId: string, addedAtUnix: number }[] = []
|
||||||
|
const removeExpiredEvents = () => {
|
||||||
|
const now = Date.now();
|
||||||
|
for (let i = handledEvents.length - 1; i >= 0; i--) {
|
||||||
|
if (now - handledEvents[i].addedAtUnix > EVENT_TTL_MS) {
|
||||||
|
handledEvents.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setInterval(removeExpiredEvents, CLEANUP_INTERVAL_SECONDS * 1000);
|
||||||
|
|
||||||
const send = (message: ChildProcessResponse) => {
|
const send = (message: ChildProcessResponse) => {
|
||||||
if (process.send) {
|
if (process.send) {
|
||||||
process.send(message)
|
process.send(message)
|
||||||
|
|
@ -150,11 +163,11 @@ export default class Handler {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const eventId = e.id
|
const eventId = e.id
|
||||||
if (handledEvents.includes(eventId)) {
|
if (handledEvents.find(e => e.eventId === eventId)) {
|
||||||
console.log("event already handled")
|
console.log("event already handled")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
handledEvents.push(eventId)
|
handledEvents.push({eventId, addedAtUnix: Date.now()});
|
||||||
const startAtMs = Date.now()
|
const startAtMs = Date.now()
|
||||||
const startAtNano = process.hrtime.bigint().toString()
|
const startAtNano = process.hrtime.bigint().toString()
|
||||||
const decoded = decodePayload(e.content)
|
const decoded = decodePayload(e.content)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue