Attempt to clean up relay loading

This commit is contained in:
Jon Staab 2024-08-27 17:17:34 -07:00
parent 01381ae143
commit 2c14d73289
5 changed files with 97 additions and 64 deletions

View File

@ -3,8 +3,7 @@
import "@fortawesome/fontawesome-free/css/solid.css"
import {nip19} from "nostr-tools"
import {pluck} from "ramda"
import {seconds, Fetch} from "hurdak"
import {seconds} from "hurdak"
import * as store from "svelte/store"
import * as lib from "@welshman/lib"
import * as util from "@welshman/util"
@ -12,7 +11,7 @@
import logger from "src/util/logger"
import * as misc from "src/util/misc"
import * as nostr from "src/util/nostr"
import {storage, session, pubkey, relays, getSetting, dufflepud} from "src/engine"
import {storage, session, pubkey, relays, getSetting} from "src/engine"
import * as engine from "src/engine"
import * as domain from "src/domain"
import {loadAppData, slowConnections, loadUserData} from "src/app/state"
@ -484,22 +483,8 @@
.filter(r => (r.last_checked || 0) < lib.now() - seconds(7, "day"))
.slice(0, 20)
if (staleRelays.length > 0) {
misc.tryFetch(async () => {
const result = await Fetch.fetchJson(dufflepud("relay/info"), {
method: "POST",
body: JSON.stringify({urls: pluck("url", staleRelays)}),
headers: {
"Content-Type": "application/json",
},
})
for (const {url: rawUrl, info} of result.data) {
const url = util.normalizeRelayUrl(rawUrl)
relays.key(url).merge({...info, url, last_checked: lib.now()})
}
})
for (const relay of staleRelays) {
engine.loadRelay(relay.url)
}
}, 30_000)

View File

@ -1,6 +1,7 @@
<script lang="ts">
import cx from "classnames"
import {isNil} from "ramda"
import {isNil} from "@welshman/lib"
import {onMount} from "svelte"
import {quantify} from "hurdak"
import {stringToHue, displayUrl, hsl} from "src/util/misc"
import {getAvgRating} from "src/util/nostr"
@ -19,6 +20,7 @@
setInboxPolicy,
setOutboxPolicy,
deriveUserRelayPolicy,
loadRelay,
} from "src/engine"
export let url
@ -43,6 +45,10 @@
setOutboxPolicy(newPolicy)
}
}
onMount(() => {
loadRelay(url)
})
</script>
<div

View File

@ -40,6 +40,7 @@
$: url = normalizeRelayUrl(url)
$: rating = getAvgRating(reviews)
$: console.log($relay)
document.title = displayRelayUrl(url)
</script>

View File

@ -1,6 +1,6 @@
import {debounce} from "throttle-debounce"
import {get, writable} from "svelte/store"
import {batch, Fetch, noop, tryFunc, seconds, createMapOf, sleep, switcherFn} from "hurdak"
import {batch, noop, tryFunc, seconds, createMapOf, sleep, switcherFn} from "hurdak"
import type {LoadOpts} from "@welshman/feeds"
import {
FeedLoader,
@ -10,7 +10,21 @@ import {
makeRelayFeed,
makeUnionFeed,
} from "@welshman/feeds"
import {Worker, bech32ToHex, batcher, pick, cached, nthEq, nth, now, max} from "@welshman/lib"
import {
Worker,
bech32ToHex,
batcher,
pick,
simpleCache,
nthEq,
nth,
now,
max,
postJson,
indexBy,
uniqBy,
flatten,
} from "@welshman/lib"
import type {Filter, TrustedEvent, SignedEvent} from "@welshman/util"
import {
Tags,
@ -19,6 +33,7 @@ import {
isGroupAddress,
isSignedEvent,
createEvent,
normalizeRelayUrl,
WRAP,
EPOCH,
LABEL,
@ -37,7 +52,7 @@ import {updateIn} from "src/util/misc"
import {noteKinds, reactionKinds, repostKinds} from "src/util/nostr"
import {always, partition, pluck, uniq, without} from "ramda"
import {LIST_KINDS} from "src/domain"
import type {Zapper} from "src/engine/model"
import type {Zapper, RelayInfo} from "src/engine/model"
import {repository} from "src/engine/repository"
import {
getUserCircles,
@ -64,6 +79,9 @@ import {
subscribePersistent,
dufflepud,
signer,
relays,
getFreshness,
setFreshness,
} from "src/engine/state"
import {updateCurrentSession, updateSession} from "src/engine/commands"
@ -84,57 +102,53 @@ export const addSinceToFilter = (filter, overlap = seconds(1, "hour")) => {
// Handles/Zappers
const fetchHandle = batcher(500, async (handles: string[]) => {
const data =
(await tryFunc(async () => {
const res = await Fetch.postJson(dufflepud("handle/info"), {handles: uniq(handles)})
export const loadHandle = simpleCache(
batcher(500, async (handles: string[][]) => {
handles = uniq(flatten(handles))
return res?.data
})) || []
const data =
(await tryFunc(async () => {
const res = await postJson(dufflepud("handle/info"), {handles})
const infoByHandle = createMapOf("handle", "info", data)
return res?.data
})) || []
return handles.map(h => infoByHandle[h])
})
const infoByHandle = createMapOf("handle", "info", data)
export const loadHandle = cached({
maxSize: 100,
getKey: ([handle]) => handle,
getValue: ([handle]) => fetchHandle(handle),
})
return handles.map(h => infoByHandle[h])
}),
)
const fetchZapper = batcher(3000, async (lnurls: string[]) => {
const data =
(await tryFunc(async () => {
// Dufflepud expects plaintext but we store lnurls encoded
const res = await Fetch.postJson(dufflepud("zapper/info"), {
lnurls: uniq(lnurls).map(bech32ToHex),
})
export const loadZapper = simpleCache(
batcher(3000, async (lnurls: string[][]) => {
lnurls = uniq(flatten(lnurls))
return res?.data
})) || []
const data =
(await tryFunc(async () => {
// Dufflepud expects plaintext but we store lnurls encoded
const res = await postJson(dufflepud("zapper/info"), {
lnurls: lnurls.map(bech32ToHex),
})
const infoByLnurl = createMapOf("lnurl", "info", data)
return res?.data
})) || []
return lnurls.map(lnurl => {
const zapper = tryFunc(() => infoByLnurl[bech32ToHex(lnurl)])
const infoByLnurl = createMapOf("lnurl", "info", data)
if (!zapper) {
return null
}
return lnurls.map(lnurl => {
const zapper = tryFunc(() => infoByLnurl[bech32ToHex(lnurl)])
return {
...pick(["callback", "minSendable", "maxSendable", "nostrPubkey", "allowsNostr"], zapper),
lnurl,
} as Zapper
})
})
if (!zapper) {
return null
}
export const loadZapper = cached({
maxSize: 100,
getKey: ([handle]) => handle,
getValue: ([handle]) => fetchZapper(handle),
})
return {
...pick(["callback", "minSendable", "maxSendable", "nostrPubkey", "allowsNostr"], zapper),
lnurl,
} as Zapper
})
}),
)
export const attemptedAddrs = new Map()
@ -550,3 +564,28 @@ export const loadHandlers = () =>
addSinceToFilter({kinds: [HANDLER_INFORMATION]}),
],
})
export const loadRelay = batcher(800, async (urls: string[]) => {
const urlSet = new Set(
urls
.map(url => normalizeRelayUrl(url))
.filter(url => getFreshness("relay", url) < now() - 3600),
)
for (const url of urlSet) {
setFreshness("relay", url, now())
}
const res = urlSet.size && await postJson(dufflepud("relay/info"), {urls: Array.from(urlSet)})
console.log('relays', res)
const index = indexBy((item: any) => item.url, res?.data || [])
const items: RelayInfo[] = urls.map(url => {
const {info = {}} = index.get(url) || {}
return {...info, url, last_checked: now()}
})
relays.update($relays => uniqBy($relay => $relay.url, [...$relays, ...items]))
return items
})

View File

@ -155,7 +155,9 @@
class="flex flex-grow flex-col-reverse justify-start overflow-auto p-4 pb-6">
<div>
{#if sending}
<div class="m-auto flex gap-2 justify-center items-center text-neutral-500 pt-2" transition:slide>
<div
class="m-auto flex items-center justify-center gap-2 pt-6 text-neutral-500"
transition:slide>
<i class="fa fa-circle-notch fa-spin" />
<span>Sending your message...</span>
</div>