Fix relay error stats and router relay quality
This commit is contained in:
@@ -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
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()})
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ export enum SocketStatus {
|
||||
Closing = "socket:status:closing",
|
||||
Closed = "socket:status:closed",
|
||||
Error = "socket:status:error",
|
||||
Invalid = "socket:status:invalid",
|
||||
}
|
||||
|
||||
export enum SocketEvent {
|
||||
@@ -110,7 +109,7 @@ export class Socket extends EventEmitter {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
this.emit(SocketEvent.Status, SocketStatus.Invalid, this.url)
|
||||
this.emit(SocketEvent.Status, SocketStatus.Error, this.url)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user