Improve relay scoring

This commit is contained in:
Jon Staab
2025-01-15 09:54:19 -08:00
parent b195124da4
commit 29a0726165
3 changed files with 27 additions and 14 deletions
+19 -8
View File
@@ -327,19 +327,30 @@ export class RouterScenario {
export const getRelayQuality = (url: string) => { export const getRelayQuality = (url: string) => {
const relay = relaysByUrl.get().get(url) const relay = relaysByUrl.get().get(url)
// Skip non-relays entirely
if (!isRelayUrl(url)) return 0
// If we have recent errors, skip it
if (relay?.stats) { if (relay?.stats) {
if (relay.stats.recent_errors.filter(n => n > ago(5)).length > 0) return 0 if (relay.stats.recent_errors.filter(n => n > ago(MINUTE)).length > 0) return 0
if (relay.stats.recent_errors.filter(n => n > ago(MINUTE)).length > 1) 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(HOUR)).length > 5) 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(DAY)).length > 20) return 0 if (relay.stats.recent_errors.filter(n => n > ago(WEEK)).length > 50) return 0
if (relay.stats.recent_errors.filter(n => n > ago(WEEK)).length > 100) return 0
} }
if (isIPAddress(url)) { // Prefer stuff we're connected to
return 0.5 if (ctx.net.pool.has(url)) return 1
// Prefer stuff we've connected to in the past
if (relay?.stats) return 0.9
// If it's not weird url give it an ok score
if (!isIPAddress(url) && !isLocalUrl(url) && !isOnionUrl(url) && !url.startsWith("ws://")) {
return 0.8
} }
return 1 // Default to a "meh" score
return 0.7
} }
export const getPubkeyRelays = (pubkey: string, mode?: string) => { export const getPubkeyRelays = (pubkey: string, mode?: string) => {
+1
View File
@@ -122,6 +122,7 @@ export class Socket {
} }
} }
} catch (e) { } catch (e) {
this.lastError = Date.now()
this.status = SocketStatus.Invalid this.status = SocketStatus.Invalid
this.cxn.emit(ConnectionEvent.InvalidUrl) this.cxn.emit(ConnectionEvent.InvalidUrl)
} }
+7 -6
View File
@@ -35,13 +35,14 @@ export const isRelayUrl = (url: string) => {
url = "wss://" + url url = "wss://" + url
} }
if (!url.match(/^wss?:\/\//)) { // Skip non-ws urls
return false if (!url.match(/^wss?:\/\//)) return false
}
if (!url.match(/\./)) { // Skip urls with a slash before the dot
return false if (url.match(/\\.*\./)) return false
}
// Skip urls without a dot
if (!url.match(/\./)) return false
try { try {
new URL(url) new URL(url)