m/n/c/n/dhcp4c: minor cleanup
The DiscoverBackoff was not Reset initially, which means the first
discovery started with a backoff of 500 ms (ExponentialBackOff default)
instead of 1400 ms.
Remove the unused lastBoundTransition field.
If the backoff has no timeout and stateDeadline is not set,
stateDeadlineExceeded should not be called (the discovering state does
not define this function). This was actually already the case before,
because NextBackOff never returns a duration smaller than 10 ms, but
this change makes that more clear.
Change-Id: I714a562d8901fba69afaf6b779c6db310577fca5
Reviewed-on: https://review.monogon.dev/c/monogon/+/3215
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/go.mod b/go.mod
index 6ed84d0..fc42bf3 100644
--- a/go.mod
+++ b/go.mod
@@ -76,7 +76,7 @@
github.com/adrg/xdg v0.4.0
github.com/bazelbuild/rules_go v0.43.0
github.com/cavaliergopher/cpio v1.0.1
- github.com/cenkalti/backoff/v4 v4.2.1
+ github.com/cenkalti/backoff/v4 v4.3.0
github.com/cockroachdb/cockroach-go/v2 v2.2.10
github.com/container-storage-interface/spec v1.8.0
github.com/containerd/containerd v1.7.15
diff --git a/go.sum b/go.sum
index 898f8da..11423fe 100644
--- a/go.sum
+++ b/go.sum
@@ -1065,8 +1065,9 @@
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.2/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
-github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
+github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
+github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
diff --git a/metropolis/node/core/network/dhcp4c/dhcpc.go b/metropolis/node/core/network/dhcp4c/dhcpc.go
index be31700..2b21cd0 100644
--- a/metropolis/node/core/network/dhcp4c/dhcpc.go
+++ b/metropolis/node/core/network/dhcp4c/dhcpc.go
@@ -163,8 +163,6 @@
state state
- lastBoundTransition time.Time
-
iface *net.Interface
// now can be used to override time for testing
@@ -189,10 +187,9 @@
leaseRenewDeadline time.Time
}
-// newDefaultBackoff returns an infinitely-retrying randomized exponential
-// backoff with a DHCP-appropriate InitialInterval
-func newDefaultBackoff() *backoff.ExponentialBackOff {
- b := backoff.NewExponentialBackOff()
+// defaultBackoffOpts can be passed to NewExponentialBackOff and configures it
+// to retry infinitely and use a DHCP-appropriate InitialInterval.
+func defaultBackoffOpts(b *backoff.ExponentialBackOff) {
b.MaxElapsedTime = 0 // No Timeout
// Lots of servers wait 1s for existing users of an IP. Wait at least for
// that and keep some slack for randomization, communication and processing
@@ -200,7 +197,6 @@
b.InitialInterval = 1400 * time.Millisecond
b.MaxInterval = 30 * time.Second
b.RandomizationFactor = 0.2
- return b
}
// NewClient instantiates (but doesn't start) a new DHCPv4 client.
@@ -221,19 +217,19 @@
return nil, fmt.Errorf("failed to create DHCP broadcast transport: %w", err)
}
- discoverBackoff := newDefaultBackoff()
+ discoverBackoff := backoff.NewExponentialBackOff(defaultBackoffOpts)
- acceptOfferBackoff := newDefaultBackoff()
- // Abort after 30s and go back to discovering
- acceptOfferBackoff.MaxElapsedTime = 30 * time.Second
+ acceptOfferBackoff := backoff.NewExponentialBackOff(defaultBackoffOpts,
+ // Abort after 30s and go back to discovering
+ backoff.WithMaxElapsedTime(30*time.Second))
- renewBackoff := newDefaultBackoff()
- // Increase maximum interval to reduce chatter when the server is down
- renewBackoff.MaxInterval = 5 * time.Minute
+ renewBackoff := backoff.NewExponentialBackOff(defaultBackoffOpts,
+ // Increase maximum interval to reduce chatter when the server is down
+ backoff.WithMaxInterval(5*time.Minute))
- rebindBackoff := newDefaultBackoff()
- // Increase maximum interval to reduce chatter when the server is down
- renewBackoff.MaxInterval = 5 * time.Minute
+ rebindBackoff := backoff.NewExponentialBackOff(defaultBackoffOpts,
+ // Increase maximum interval to reduce chatter when the server is down
+ backoff.WithMaxInterval(5*time.Minute))
// Check if the hardware address contains at least one non-zero value.
// This exists to catch undefined/non-supplied hardware address values,
@@ -250,17 +246,16 @@
}
return &Client{
- state: stateDiscovering,
- broadcastConn: broadcastConn,
- unicastConn: transport.NewUnicastTransport(iface),
- iface: iface,
- RequestedOptions: dhcpv4.OptionCodeList{},
- lastBoundTransition: time.Now(),
- now: time.Now,
- DiscoverBackoff: discoverBackoff,
- AcceptOfferBackoff: acceptOfferBackoff,
- RenewBackoff: renewBackoff,
- RebindBackoff: rebindBackoff,
+ state: stateDiscovering,
+ broadcastConn: broadcastConn,
+ unicastConn: transport.NewUnicastTransport(iface),
+ iface: iface,
+ RequestedOptions: dhcpv4.OptionCodeList{},
+ now: time.Now,
+ DiscoverBackoff: discoverBackoff,
+ AcceptOfferBackoff: acceptOfferBackoff,
+ RenewBackoff: renewBackoff,
+ RebindBackoff: rebindBackoff,
}, nil
}
@@ -453,14 +448,14 @@
receiveDeadline := sentTime.Add(wait)
if !s.stateDeadline.IsZero() {
receiveDeadline = earliestDeadline(s.stateDeadline, receiveDeadline)
- }
- // Jump out if deadline expires in less than 10ms. Minimum lease time is 1s
- // and if we have less than 10ms to wait for an answer before switching
- // state it makes no sense to send out another request. This nearly
- // eliminates the problem of sending two different requests back-to-back.
- if receiveDeadline.Add(-10 * time.Millisecond).Before(sentTime) {
- return s.stateDeadlineExceeded()
+ // Jump out if deadline expires in less than 10ms. Minimum lease time is 1s
+ // and if we have less than 10ms to wait for an answer before switching
+ // state it makes no sense to send out another request. This nearly
+ // eliminates the problem of sending two different requests back-to-back.
+ if s.stateDeadline.Add(-10 * time.Millisecond).Before(sentTime) {
+ return s.stateDeadlineExceeded()
+ }
}
if err := s.transport.Send(msg); err != nil {
diff --git a/third_party/go/repositories.bzl b/third_party/go/repositories.bzl
index b95f308..dcffde4 100644
--- a/third_party/go/repositories.bzl
+++ b/third_party/go/repositories.bzl
@@ -761,8 +761,8 @@
go_repository(
name = "com_github_cenkalti_backoff_v4",
importpath = "github.com/cenkalti/backoff/v4",
- sum = "h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=",
- version = "v4.2.1",
+ sum = "h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=",
+ version = "v4.3.0",
)
go_repository(
name = "com_github_census_instrumentation_opencensus_proto",