Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove dependency on the backoff library #17

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that backoff still jitters with randomness, even when it reached its max. I'm not worrying about it much though, just FYI.

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