Skip to content

Commit

Permalink
replace timeout context with timeout channel
Browse files Browse the repository at this point in the history
  • Loading branch information
barrydeen committed Sep 12, 2024
1 parent c12335e commit 059500b
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ func appendOneHopNetwork(pubkey string) {
}

func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(config.RefreshInterval)*time.Hour)
defer cancel()
timeout := time.After(time.Duration(config.RefreshInterval) * time.Hour)

filters := []nostr.Filter{{
Kinds: []int{
Expand All @@ -336,15 +335,31 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
log.Println("📦 archiving trusted notes...")
var trustedNotes uint64
var untrustedNotes uint64

trustNetworkFilterMu.Lock()
defer trustNetworkFilterMu.Unlock()

for ev := range pool.SubMany(timeout, seedRelays, filters) {
eventChan := pool.SubMany(ctx, seedRelays, filters)

for {
select {
case <-ctx.Done():
case <-timeout:
log.Println("⏰ Archive process terminated due to timeout")
log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
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 len(ev.Event.Tags) > 3000 {
continue
Expand All @@ -358,7 +373,4 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
}
}
}

log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
return
}

0 comments on commit 059500b

Please sign in to comment.