fix heavy loop

This commit is contained in:
Yasuhiro Matsumoto
2023-12-15 00:27:56 +09:00
committed by fiatjaf_
parent c55b509653
commit 242af0bf76

13
pool.go
View File

@@ -102,6 +102,8 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
events := make(chan IncomingEvent) events := make(chan IncomingEvent)
seenAlready := xsync.NewMapOf[Timestamp]() seenAlready := xsync.NewMapOf[Timestamp]()
ticker := time.NewTicker(seenAlreadyDropTick) ticker := time.NewTicker(seenAlreadyDropTick)
var mu sync.Mutex
eose := false eose := false
pending := xsync.NewCounter() pending := xsync.NewCounter()
@@ -139,6 +141,13 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
goto reconnect goto reconnect
} }
go func() {
<-sub.EndOfStoredEvents
mu.Lock()
eose = true
mu.Unlock()
}()
// reset interval when we get a good subscription // reset interval when we get a good subscription
interval = 3 * time.Second interval = 3 * time.Second
@@ -164,9 +173,8 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
case events <- IncomingEvent{Event: evt, Relay: relay}: case events <- IncomingEvent{Event: evt, Relay: relay}:
case <-ctx.Done(): case <-ctx.Done():
} }
case <-sub.EndOfStoredEvents:
eose = true
case <-ticker.C: case <-ticker.C:
mu.Lock()
if eose { if eose {
old := Timestamp(time.Now().Add(-seenAlreadyDropTick).Unix()) old := Timestamp(time.Now().Add(-seenAlreadyDropTick).Unix())
seenAlready.Range(func(id string, value Timestamp) bool { seenAlready.Range(func(id string, value Timestamp) bool {
@@ -176,6 +184,7 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
return true return true
}) })
} }
mu.Unlock()
case reason := <-sub.ClosedReason: case reason := <-sub.ClosedReason:
if strings.HasPrefix(reason, "auth-required:") && pool.authHandler != nil && !hasAuthed { if strings.HasPrefix(reason, "auth-required:") && pool.authHandler != nil && !hasAuthed {
// relay is requesting auth. if we can we will perform auth and try again // relay is requesting auth. if we can we will perform auth and try again