Disconnect stale connections, see if this helps with stalling stuff

This commit is contained in:
Jonathan Staab 2023-10-02 16:14:23 -07:00
parent 95ccc51a44
commit f526ec5c58
4 changed files with 25 additions and 6 deletions

View File

@ -1,5 +1,5 @@
import {partition} from "ramda"
import {noop} from "hurdak"
import {max, partition} from "ramda"
import {noop, pickVals} from "hurdak"
import {Plex, Relays, Executor, Multi} from "paravel"
import {error, warn} from "src/util/logger"
import {normalizeRelayUrl} from "src/engine/relays/utils"
@ -82,3 +82,17 @@ export const getExecutor = (urls: string[]) => {
return executor
}
setInterval(() => {
const activityKeys = ["lastRequest", "lastPublish", "lastEvent"]
for (const [url, con] of pool.data.entries()) {
// @ts-ignore
const lastActivity = pickVals(activityKeys, con.meta).reduce(max)
// If our connection hasn't been used in a while, close it and reopen
if (lastActivity && lastActivity < Date.now() - 60_000) {
pool.remove(url)
}
}
}, 10_000)

View File

@ -43,9 +43,15 @@ export class Subscription extends EventEmitter {
setTimeout(this.close, timeout)
}
const urls = getUrls(relays)
this.executor = getExecutor(getUrls(relays))
// If one of our connections gets closed make sure to kill our sub
this.executor.target.connections.forEach(con => {
con.on("close", () => {
this.close()
})
})
this.executor = getExecutor(urls)
this.sub = this.executor.subscribe(filters, {
onEvent: this.onEvent,
onEose: this.onEose,

View File

@ -16,7 +16,6 @@
const setDate = d => {
date = d
console.log(d)
value = formatDateAsLocalISODate(d).slice(0, 10)
}

View File

@ -194,7 +194,7 @@ const WARN_TAGS = new Set([
"porn",
"ass",
"boob",
"boostr",
"boobstr",
"sex",
"sexy",
"fuck",