Fix relay error stats and router relay quality

This commit is contained in:
Jon Staab
2025-04-07 17:28:00 -07:00
parent d0d727edd1
commit a6c916c0e1
4 changed files with 15 additions and 22 deletions
+1 -5
View File
@@ -62,11 +62,7 @@ export const loadWithAsapMetaRelayUrls = <T>(pubkey: string, relays: string[], f
return Promise.race([
load({filters, relays: router.merge([router.FromRelays(relays), router.Index()]).getUrls()}),
loadRelaySelections(pubkey, relays).then(() => {
const relays = router.FromPubkey(pubkey).policy(addNoFallbacks).getUrls()
return load({filters, relays})
}),
loadRelaySelections(pubkey, relays).then(() => load({filters, relays: router.FromPubkey(pubkey).getUrls()})),
])
}
+10 -11
View File
@@ -233,27 +233,26 @@ const onSocketStatus = (status: string, url: string) => {
},
])
}
}
const onSocketError = (error: string, url: string) =>
updateRelayStats([
url,
stats => {
stats.last_error = now()
stats.recent_errors = uniq(stats.recent_errors.concat(now())).slice(-10)
},
])
if (status === SocketStatus.Error) {
updateRelayStats([
url,
stats => {
stats.last_error = now()
stats.recent_errors = uniq(stats.recent_errors.concat(now())).slice(-10)
},
])
}
}
export const trackRelayStats = (socket: Socket) => {
socket.on(SocketEvent.Send, onSocketSend)
socket.on(SocketEvent.Receive, onSocketReceive)
socket.on(SocketEvent.Status, onSocketStatus)
socket.on(SocketEvent.Error, onSocketError)
return () => {
socket.off(SocketEvent.Send, onSocketSend)
socket.off(SocketEvent.Receive, onSocketReceive)
socket.off(SocketEvent.Status, onSocketStatus)
socket.off(SocketEvent.Error, onSocketError)
}
}
+3 -4
View File
@@ -141,7 +141,6 @@ export const getRelayQuality = (url: string) => {
if (relay.stats.recent_errors.filter(n => n > ago(MINUTE)).length > 0) return 0
if (relay.stats.recent_errors.filter(n => n > ago(HOUR)).length > 3) return 0
if (relay.stats.recent_errors.filter(n => n > ago(DAY)).length > 10) return 0
if (relay.stats.recent_errors.filter(n => n > ago(WEEK)).length > 50) return 0
}
// Prefer stuff we're connected to
@@ -382,13 +381,13 @@ export class RouterScenario {
}
const scoreRelay = (relay: string) => {
const quality = this.router.options.getRelayQuality?.(relay) || 1
const quality = this.router.options.getRelayQuality?.(relay)
const weight = relayWeights.get(relay)!
// Log the weight, since it's a straight count which ends up over-weighting hubs.
// Also add some random noise so that we'll occasionally pick lower quality/less
// popular relays.
return -(quality * inc(Math.log(weight)) * Math.random())
return quality ? -(quality * inc(Math.log(weight)) * Math.random()) : 0
}
const relays = take(
@@ -492,7 +491,7 @@ export const getFilterSelections = (
const result = []
for (const [id, filter] of filtersById.entries()) {
const scenario = Router.get().merge(scenariosById.get(id) || [])
const scenario = Router.get().merge(scenariosById.get(id) || []).policy(addMinimalFallbacks)
result.push({filters: [filter], relays: scenario.getUrls()})
}