prevent archiving from getting stuck

This commit is contained in:
Barry Deen 2024-09-11 12:55:40 -04:00
parent 489e51e274
commit f1ff8bb6bb

36
main.go
View File

@ -147,7 +147,7 @@ func LoadConfig() Config {
godotenv.Load(".env") godotenv.Load(".env")
if os.Getenv("REFRESH_INTERVAL_HOURS") == "" { if os.Getenv("REFRESH_INTERVAL_HOURS") == "" {
os.Setenv("REFRESH_INTERVAL_HOURS", "24") os.Setenv("REFRESH_INTERVAL_HOURS", "3")
} }
refreshInterval, _ := strconv.Atoi(os.Getenv("REFRESH_INTERVAL_HOURS")) refreshInterval, _ := strconv.Atoi(os.Getenv("REFRESH_INTERVAL_HOURS"))
@ -193,7 +193,7 @@ func updateTrustNetworkFilter() {
func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) { func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
runTrustNetworkRefresh := func() { runTrustNetworkRefresh := func() {
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Second) timeoutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel() defer cancel()
filters := []nostr.Filter{{ filters := []nostr.Filter{{
@ -210,7 +210,7 @@ func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
log.Println("🌐 building web of trust graph") log.Println("🌐 building web of trust graph")
for i := 0; i < len(oneHopNetwork); i += 100 { for i := 0; i < len(oneHopNetwork); i += 100 {
timeout, cancel := context.WithTimeout(ctx, 3*time.Second) timeout, cancel := context.WithTimeout(ctx, 4*time.Second)
defer cancel() defer cancel()
end := i + 100 end := i + 100
@ -321,19 +321,27 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
var trustedNotes uint64 var trustedNotes uint64
var untrustedNotes uint64 var untrustedNotes uint64
trustNetworkFilterMu.Lock() trustNetworkFilterMu.Lock()
for ev := range pool.SubMany(timeout, seedRelays, filters) { defer trustNetworkFilterMu.Unlock()
if trustNetworkFilter.Has(xxhash.Sum64([]byte(ev.Event.PubKey))) {
if len(ev.Event.Tags) > 2000 {
continue
}
relay.AddEvent(ctx, ev.Event) for ev := range pool.SubManyEose(timeout, seedRelays, filters) {
log.Println("📦 archived note from", ev.Event.PubKey) select {
trustedNotes++ case <-ctx.Done():
} else { log.Println("⏰ Archive process terminated due to timeout")
untrustedNotes++ return
default:
if trustNetworkFilter.Has(xxhash.Sum64([]byte(ev.Event.PubKey))) {
if len(ev.Event.Tags) > 3000 {
continue
}
relay.AddEvent(ctx, ev.Event)
log.Println("📦 archived note: ", ev.Event.ID)
trustedNotes++
} else {
untrustedNotes++
}
} }
} }
trustNetworkFilterMu.Unlock()
log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes") log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
} }