Skip to content

Commit

Permalink
exponential backoff on SimplePool reconnections and only update `sinc…
Browse files Browse the repository at this point in the history
…e` once.
  • Loading branch information
fiatjaf committed Dec 2, 2023
1 parent 0e0ecb2 commit 15f026d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
cancel()
}()

interval := 3 * time.Second
for {
select {
case <-ctx.Done():
Expand All @@ -102,20 +103,28 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt

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

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

// reset interval when we get a good subscription
interval = 3 * time.Second

for {
select {
case evt, more := <-sub.Events:
if !more {
// this means the connection was closed for weird reasons, like the server shut down
// so we will update the filters here to include only events seem from now on
// and try to reconnect until we succeed
now := Now()
for i := range filters {
filters[i].Since = &now
}
goto reconnect
}
if unique {
Expand Down Expand Up @@ -148,11 +157,10 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters Filt
}

reconnect:
// when attempting to reconnect update the `since` in filters so old events are not retrieved
now := Now()
for i := range filters {
filters[i].Since = &now
}
// we will go back to the beginning of the loop and try to connect again and again
// until the context is canceled
time.Sleep(interval)
interval = interval * 17 / 10 // the next time we try we will wait longer
}
}(NormalizeURL(url))
}
Expand Down

0 comments on commit 15f026d

Please sign in to comment.