From d4775ecff5512c21c42a443f3da0d2bf42a59b92 Mon Sep 17 00:00:00 2001 From: Aneesh Puttur Date: Thu, 18 Jun 2020 12:35:23 -0400 Subject: [PATCH] Fix handling of delay in acquiring lease with stp turned on Signed-off-by: Aneesh Puttur --- plugins/ipam/dhcp/daemon.go | 1 - plugins/ipam/dhcp/lease.go | 13 ++++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/plugins/ipam/dhcp/daemon.go b/plugins/ipam/dhcp/daemon.go index aeb87aaa..7ec2818b 100644 --- a/plugins/ipam/dhcp/daemon.go +++ b/plugins/ipam/dhcp/daemon.go @@ -34,7 +34,6 @@ import ( ) const listenFdsStart = 3 -const resendCount = 3 var errNoMoreTries = errors.New("no more tries") diff --git a/plugins/ipam/dhcp/lease.go b/plugins/ipam/dhcp/lease.go index dc2a9d92..b014a0b6 100644 --- a/plugins/ipam/dhcp/lease.go +++ b/plugins/ipam/dhcp/lease.go @@ -34,7 +34,7 @@ import ( // RFC 2131 suggests using exponential backoff, starting with 4sec // and randomized to +/- 1sec const resendDelay0 = 4 * time.Second -const resendDelayMax = 32 * time.Second +const resendDelayMax = 62 * time.Second const ( leaseStateBound = iota @@ -335,8 +335,9 @@ func jitter(span time.Duration) time.Duration { func backoffRetry(f func() (*dhcp4.Packet, error)) (*dhcp4.Packet, error) { var baseDelay time.Duration = resendDelay0 + var sleepTime time.Duration - for i := 0; i < resendCount; i++ { + for { pkt, err := f() if err == nil { return pkt, nil @@ -344,10 +345,16 @@ func backoffRetry(f func() (*dhcp4.Packet, error)) (*dhcp4.Packet, error) { log.Print(err) - time.Sleep(baseDelay + jitter(time.Second)) + sleepTime = baseDelay + jitter(time.Second) + + log.Printf("retrying in %f seconds", sleepTime.Seconds()) + + time.Sleep(sleepTime) if baseDelay < resendDelayMax { baseDelay *= 2 + } else { + break } }