use atomic eosed bool in subMany()

This commit is contained in:
fiatjaf 2025-02-16 17:36:05 -03:00
parent b437cc6a1f
commit 7412a6fb40

10
pool.go
View File

@ -9,6 +9,7 @@ import (
"slices" "slices"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/nbd-wtf/go-nostr/nip45/hyperloglog" "github.com/nbd-wtf/go-nostr/nip45/hyperloglog"
@ -301,7 +302,7 @@ func (pool *SimplePool) subMany(
continue continue
} }
eosed := false eosed := atomic.Bool{}
firstConnection := true firstConnection := true
go func(nm string) { go func(nm string) {
@ -311,7 +312,7 @@ func (pool *SimplePool) subMany(
close(events) close(events)
cancel(fmt.Errorf("aborted: %w", context.Cause(ctx))) cancel(fmt.Errorf("aborted: %w", context.Cause(ctx)))
} }
if !eosed { if !eosed.Load() {
eoseWg.Done() eoseWg.Done()
} }
}() }()
@ -370,8 +371,7 @@ func (pool *SimplePool) subMany(
<-sub.EndOfStoredEvents <-sub.EndOfStoredEvents
// guard here otherwise a resubscription will trigger a duplicate call to eoseWg.Done() // guard here otherwise a resubscription will trigger a duplicate call to eoseWg.Done()
if !eosed { if eosed.CompareAndSwap(false, true) {
eosed = true
eoseWg.Done() eoseWg.Done()
} }
}() }()
@ -407,7 +407,7 @@ func (pool *SimplePool) subMany(
return return
} }
case <-ticker.C: case <-ticker.C:
if eosed { if eosed.Load() {
old := Timestamp(time.Now().Add(-seenAlreadyDropTick).Unix()) old := Timestamp(time.Now().Add(-seenAlreadyDropTick).Unix())
for id, value := range seenAlready.Range { for id, value := range seenAlready.Range {
if value < old { if value < old {