Skip to content

Commit

Permalink
fix heavy loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn authored and fiatjaf committed Dec 14, 2023
1 parent c55b509 commit 242af0b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
events := make(chan IncomingEvent)
seenAlready := xsync.NewMapOf[Timestamp]()
ticker := time.NewTicker(seenAlreadyDropTick)

var mu sync.Mutex
eose := false

pending := xsync.NewCounter()
Expand Down Expand Up @@ -139,6 +141,13 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
goto reconnect
}

go func() {
<-sub.EndOfStoredEvents
mu.Lock()
eose = true
mu.Unlock()
}()

// reset interval when we get a good subscription
interval = 3 * time.Second

Expand All @@ -164,9 +173,8 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
case events <- IncomingEvent{Event: evt, Relay: relay}:
case <-ctx.Done():
}
case <-sub.EndOfStoredEvents:
eose = true
case <-ticker.C:
mu.Lock()
if eose {
old := Timestamp(time.Now().Add(-seenAlreadyDropTick).Unix())
seenAlready.Range(func(id string, value Timestamp) bool {
Expand All @@ -176,6 +184,7 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
return true
})
}
mu.Unlock()
case reason := <-sub.ClosedReason:
if strings.HasPrefix(reason, "auth-required:") && pool.authHandler != nil && !hasAuthed {
// relay is requesting auth. if we can we will perform auth and try again
Expand Down

0 comments on commit 242af0b

Please sign in to comment.