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

reconnect #113

Merged
merged 1 commit into from
Dec 2, 2023
Merged
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
103 changes: 63 additions & 40 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,19 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
ticker := time.NewTicker(seenAlreadyDropTick)
eose := false

updateSince := func() {
// After reconnection, update the since in the filter so that
// old events are not retrieved.
now := Now()
for i := range filters {
filters[i].Since = &now
}
}

pending := xsync.NewCounter()
pending.Add(int64(len(urls)))
for _, url := range urls {
go func(nm string) {
relay, err := pool.EnsureRelay(nm)
if err != nil {
return
}

sub, _ := relay.Subscribe(ctx, filters)
if sub == nil {
return
}

defer func() {
pending.Dec()
if pending.Value() == 0 {
Expand All @@ -103,41 +102,65 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt

for {
select {
case evt, more := <-sub.Events:
if !more {
return
}
if unique {
if _, seen := seenAlready.LoadOrStore(evt.ID, evt.CreatedAt); seen {
continue
}
}
case <-ctx.Done():
return
default:
}

relay, err := pool.EnsureRelay(nm)
if err != nil {
time.Sleep(3 * time.Second)
updateSince()
continue
}

sub, err := relay.Subscribe(ctx, filters)
if err != nil {
time.Sleep(3 * time.Second)
updateSince()
continue
}

loop:
for {
select {
case events <- IncomingEvent{Event: evt, Relay: relay}:
case <-ctx.Done():
}
case <-sub.EndOfStoredEvents:
eose = true
case <-ticker.C:
if eose {
del := map[string]struct{}{}
old := Timestamp(time.Now().Add(-seenAlreadyDropTick).Unix())
seenAlready.Range(func(id string, value Timestamp) bool {
if value < old {
del[id] = struct{}{}
case evt, more := <-sub.Events:
if !more {
break loop
}
if unique {
if _, seen := seenAlready.LoadOrStore(evt.ID, evt.CreatedAt); seen {
continue
}
return true
})
for k := range del {
seenAlready.Delete(k)
}
select {
case events <- IncomingEvent{Event: evt, Relay: relay}:
case <-ctx.Done():
}
case <-sub.EndOfStoredEvents:
eose = true
case <-ticker.C:
if eose {
del := map[string]struct{}{}
old := Timestamp(time.Now().Add(-seenAlreadyDropTick).Unix())
seenAlready.Range(func(id string, value Timestamp) bool {
if value < old {
del[id] = struct{}{}
}
return true
})
for k := range del {
seenAlready.Delete(k)
}
}
case reason := <-sub.ClosedReason:
log.Printf("CLOSED from %s: '%s'\n", nm, reason)
return
case <-ctx.Done():
return
}
case reason := <-sub.ClosedReason:
log.Printf("CLOSED from %s: '%s'\n", nm, reason)
return
case <-ctx.Done():
return
}
updateSince()
}
}(NormalizeURL(url))
}
Expand Down
Loading