replace timeout context with timeout channel

This commit is contained in:
Barry Deen 2024-09-12 10:20:18 -04:00
parent c12335effb
commit 059500b2e9

28
main.go
View File

@ -314,8 +314,7 @@ func appendOneHopNetwork(pubkey string) {
} }
func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) { func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(config.RefreshInterval)*time.Hour) timeout := time.After(time.Duration(config.RefreshInterval) * time.Hour)
defer cancel()
filters := []nostr.Filter{{ filters := []nostr.Filter{{
Kinds: []int{ Kinds: []int{
@ -336,15 +335,31 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
log.Println("📦 archiving trusted notes...") log.Println("📦 archiving trusted notes...")
var trustedNotes uint64 var trustedNotes uint64
var untrustedNotes uint64 var untrustedNotes uint64
trustNetworkFilterMu.Lock() trustNetworkFilterMu.Lock()
defer trustNetworkFilterMu.Unlock() defer trustNetworkFilterMu.Unlock()
for ev := range pool.SubMany(timeout, seedRelays, filters) { eventChan := pool.SubMany(ctx, seedRelays, filters)
for {
select { select {
case <-ctx.Done(): case <-timeout:
log.Println("⏰ Archive process terminated due to timeout") log.Println("⏰ Archive process terminated due to timeout")
log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
return return
default:
case <-ctx.Done():
log.Println("⏰ Archive process terminated due to context cancellation")
log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
return
case ev, ok := <-eventChan:
if !ok {
log.Println("📦 SubMany channel closed")
log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
return
}
if trustNetworkFilter.Has(xxhash.Sum64([]byte(ev.Event.PubKey))) { if trustNetworkFilter.Has(xxhash.Sum64([]byte(ev.Event.PubKey))) {
if len(ev.Event.Tags) > 3000 { if len(ev.Event.Tags) > 3000 {
continue continue
@ -358,7 +373,4 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
} }
} }
} }
log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
return
} }