sdk: optimize caching lists (so we don't fetch twice in a row).

This commit is contained in:
fiatjaf
2024-09-25 22:38:31 -03:00
parent 1b786ab213
commit 2edc0fb713
5 changed files with 78 additions and 71 deletions

View File

@@ -1,21 +1,35 @@
package sdk
import (
"strings"
)
import "time"
// IsVirtualRelay returns true if the given normalized relay URL shouldn't be considered for outbox-model calculations.
func IsVirtualRelay(url string) bool {
if len(url) < 6 {
// this is just invalid
return true
}
var serial = 0
if strings.HasPrefix(url, "wss://feeds.nostr.band") ||
strings.HasPrefix(url, "wss://filter.nostr.wine") ||
strings.HasPrefix(url, "wss://cache") {
return true
}
return false
func pickNext(list []string) string {
serial++
return list[serial%len(list)]
}
func doThisNotMoreThanOnceAnHour(key string) (doItNow bool) {
if _dtnmtoah == nil {
go func() {
_dtnmtoah = make(map[string]time.Time)
for {
time.Sleep(time.Minute * 10)
_dtnmtoahLock.Lock()
now := time.Now()
for k, v := range _dtnmtoah {
if v.Before(now) {
delete(_dtnmtoah, k)
}
}
_dtnmtoahLock.Unlock()
}
}()
}
_dtnmtoahLock.Lock()
defer _dtnmtoahLock.Unlock()
_, exists := _dtnmtoah[key]
return !exists
}