automatic prune broken relay objects and keep track of relay idleness so they can be pruned.

This commit is contained in:
fiatjaf
2026-02-02 17:30:54 -03:00
parent 9db705d86c
commit ab802c8dbe
4 changed files with 55 additions and 24 deletions

View File

@@ -88,9 +88,7 @@ export class AbstractSimplePool {
enableReconnect: this.enableReconnect,
})
relay.onclose = () => {
if (relay && !relay.enableReconnect) {
this.relays.delete(url)
}
this.relays.delete(url)
}
this.relays.set(url, relay)
}
@@ -102,10 +100,15 @@ export class AbstractSimplePool {
}
}
await relay.connect({
timeout: params?.connectionTimeout,
abort: params?.abort,
})
try {
await relay.connect({
timeout: params?.connectionTimeout,
abort: params?.abort,
})
} catch (err) {
this.relays.delete(url)
throw err
}
return relay
}
@@ -380,4 +383,19 @@ export class AbstractSimplePool {
this.relays.forEach(conn => conn.close())
this.relays = new Map()
}
pruneIdleRelays(idleThresholdMs: number = 10000): string[] {
const prunedUrls: string[] = []
// check each relay's idle status and prune if over threshold
for (const [url, relay] of this.relays) {
if (relay.idleSince && Date.now() - relay.idleSince >= idleThresholdMs) {
this.relays.delete(url)
prunedUrls.push(url)
relay.close()
}
}
return prunedUrls
}
}