From 9847be464a041c0f040d435dfbe595106d25541b Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 22 Sep 2021 18:56:59 +0100 Subject: [PATCH] remove dependency on the backoff library --- client.go | 48 +++++++++++++++++++++++------------------------- go.mod | 1 - go.sum | 2 -- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/client.go b/client.go index d22fcc98..34b50b41 100644 --- a/client.go +++ b/client.go @@ -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" @@ -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. @@ -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) } } diff --git a/go.mod b/go.mod index 76f7fafc..97770765 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 9f4fe9c2..72d48a5c 100644 --- a/go.sum +++ b/go.sum @@ -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=