mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-02-06 00:34:31 +00:00
automatic prune broken relay objects and keep track of relay idleness so they can be pruned.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user