Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
fix: Add exponential backoff
  • Loading branch information
mafredri committed Nov 15, 2022
commit b7256c5682b60cbf56f31bccc3b79c46ef2eb0d5
23 changes: 20 additions & 3 deletions tailnet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/google/uuid"
"go4.org/netipx"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -431,20 +432,36 @@ func (c *Conn) AwaitReachable(ctx context.Context, ip netip.Addr) bool {
defer completed()

run := func() {
// Safety timeout, initially we'll have around 10-20 goroutines
// running in parallel. The exponential backoff will converge
// around ~1 ping / 30s, this means we'll have around 10-20
// goroutines pending towards the end as well.
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()

_, err := c.Ping(ctx, ip)
if err == nil {
completed()
}
}

ticker := time.NewTicker(time.Millisecond * 100)
defer ticker.Stop()
eb := backoff.NewExponentialBackOff()
eb.MaxElapsedTime = 0
eb.InitialInterval = 50 * time.Millisecond
eb.MaxInterval = 30 * time.Second
// Consume the first interval since
// we'll fire off a ping immediately.
_ = eb.NextBackOff()

t := backoff.NewTicker(eb)
defer t.Stop()

go run()
for {
select {
case <-completedCtx.Done():
return true
case <-ticker.C:
case <-t.C:
// Pings can take a while, so we can run multiple
// in parallel to return ASAP.
go run()
Expand Down