Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix heavy loop #114

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/nbd-wtf/go-nostr

go 1.20
go 1.21

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2
Expand Down
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
Loading