Skip to content

Commit

Permalink
Merge pull request #17 from libp2p/remove-backoff-library
Browse files Browse the repository at this point in the history
remove dependency on the backoff library
  • Loading branch information
marten-seemann authored Sep 27, 2021
2 parents 9dfbd03 + 9847be4 commit a3c4995
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
48 changes: 23 additions & 25 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package zeroconf
import (
"context"
"fmt"
"math/rand"
"net"
"strings"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/miekg/dns"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
Expand Down Expand Up @@ -365,31 +365,16 @@ func (c *client) recv(ctx context.Context, l interface{}, msgCh chan *dns.Msg) {
// TODO: move error reporting to shutdown function as periodicQuery is called from
// go routine context.
func (c *client) periodicQuery(ctx context.Context, params *lookupParams) error {
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 4 * time.Second
bo.MaxInterval = 60 * time.Second
bo.MaxElapsedTime = 0
bo.Reset()

var timer *time.Timer
defer func() {
if timer != nil {
timer.Stop()
}
}()
for {
// Do periodic query.
if err := c.query(params); err != nil {
return err
}
// Backoff logic
wait := bo.NextBackOff()
if timer == nil {
timer = time.NewTimer(wait)
} else {
timer.Reset(wait)
}
// Do the first query immediately.
if err := c.query(params); err != nil {
return err
}

const maxInterval = 60 * time.Second
interval := 4 * time.Second
timer := time.NewTimer(interval)
defer timer.Stop()
for {
select {
case <-timer.C:
// Wait for next iteration.
Expand All @@ -403,6 +388,19 @@ func (c *client) periodicQuery(ctx context.Context, params *lookupParams) error
}
return ctx.Err()
}

if err := c.query(params); err != nil {
return err
}
// Exponential increase of the interval with jitter:
// the new interval will be between 1.5x and 2.5x the old interval, capped at maxInterval.
if interval != maxInterval {
interval += time.Duration(rand.Int63n(interval.Nanoseconds())) + interval/2
if interval > maxInterval {
interval = maxInterval
}
}
timer.Reset(interval)
}
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/libp2p/zeroconf/v2
go 1.16

require (
github.com/cenkalti/backoff/v4 v4.1.1
github.com/miekg/dns v1.1.41
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6
golang.org/x/sys v0.0.0-20210426080607-c94f62235c83 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY=
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand Down

0 comments on commit a3c4995

Please sign in to comment.