Skip to content

Commit

Permalink
reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Dec 2, 2023
1 parent 2b9b4df commit e726fd8
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 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 @@ -102,6 +101,26 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
}()

for {
select {
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
}

select {
case evt, more := <-sub.Events:
if !more {
Expand Down Expand Up @@ -138,6 +157,7 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
case <-ctx.Done():
return
}
updateSince()
}
}(NormalizeURL(url))
}
Expand Down

0 comments on commit e726fd8

Please sign in to comment.