Skip to content

Commit 7dbf27c

Browse files
committed
move retrier to separate package and reuse code
1 parent e1edebe commit 7dbf27c

File tree

3 files changed

+15
-77
lines changed

3 files changed

+15
-77
lines changed

dhcp/dhcp_windows.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/Azure/azure-container-networking/cni/log"
10+
"github.com/Azure/azure-container-networking/retry"
1011
"github.com/pkg/errors"
1112
"go.uber.org/zap"
1213
"golang.org/x/sys/windows"
@@ -18,8 +19,9 @@ const (
1819
addIPAddressDelay = 4 * time.Second
1920
deleteIPAddressDelay = 2 * time.Second
2021
returnDelay = 8 * time.Second // time to wait before returning from DiscoverRequest
21-
22-
socketTimeoutMillis = 1000
22+
retryCount = 5
23+
retryDelayMillis = 500
24+
socketTimeoutMillis = 1000
2325
)
2426

2527
var (
@@ -160,7 +162,11 @@ func (c *DHCP) DiscoverRequest(ctx context.Context, macAddress net.HardwareAddr,
160162
return errors.Wrap(err, "failed to create socket")
161163
}
162164

163-
_, err = sock.Write(bytesToSend)
165+
// retry sending the packet until it succeeds
166+
err = retry.Do(func() error {
167+
_, sockErr := sock.Write(bytesToSend)
168+
return sockErr
169+
}, retryCount, retryDelayMillis)
164170
if err != nil {
165171
return errors.Wrap(err, "failed to write to dhcp socket")
166172
}

network/transparent_vlan_endpointclient_linux.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"net"
66
"strings"
7-
"time"
87

98
"github.com/Azure/azure-container-networking/iptables"
109
"github.com/Azure/azure-container-networking/netio"
@@ -13,6 +12,7 @@ import (
1312
"github.com/Azure/azure-container-networking/network/networkutils"
1413
"github.com/Azure/azure-container-networking/network/snat"
1514
"github.com/Azure/azure-container-networking/platform"
15+
"github.com/Azure/azure-container-networking/retry"
1616
"github.com/pkg/errors"
1717
vishnetlink "github.com/vishvananda/netlink"
1818
"go.uber.org/zap"
@@ -192,7 +192,7 @@ func (client *TransparentVlanEndpointClient) setLinkNetNSAndConfirm(name string,
192192
}
193193

194194
// confirm veth was moved successfully
195-
err = RunWithRetries(func() error {
195+
err = retry.Do(func() error {
196196
// retry checking in the namespace if the interface is not detected
197197
return ExecuteInNS(client.nsClient, client.vnetNSName, func() error {
198198
_, ifDetectedErr := client.netioshim.GetNetworkInterfaceByName(client.vlanIfName)
@@ -220,7 +220,7 @@ func (client *TransparentVlanEndpointClient) PopulateVM(epInfo *EndpointInfo) er
220220
// We assume the only possible error is that the namespace doesn't exist
221221
logger.Info("No existing NS detected. Creating the vnet namespace and switching to it", zap.String("message", existingErr.Error()))
222222

223-
err = RunWithRetries(func() error {
223+
err = retry.Do(func() error {
224224
return client.createNetworkNamespace(vmNS)
225225
}, numRetries, sleepInMs)
226226
if err != nil {
@@ -279,7 +279,7 @@ func (client *TransparentVlanEndpointClient) PopulateVM(epInfo *EndpointInfo) er
279279
}()
280280

281281
// sometimes there is slight delay in interface creation. check if it exists
282-
err = RunWithRetries(func() error {
282+
err = retry.Do(func() error {
283283
_, err = client.netioshim.GetNetworkInterfaceByName(client.vlanIfName)
284284
return errors.Wrap(err, "failed to get vlan veth")
285285
}, numRetries, sleepInMs)
@@ -316,7 +316,7 @@ func (client *TransparentVlanEndpointClient) PopulateVM(epInfo *EndpointInfo) er
316316
}
317317

318318
// Ensure vnet veth is created, as there may be a slight delay
319-
err = RunWithRetries(func() error {
319+
err = retry.Do(func() error {
320320
_, getErr := client.netioshim.GetNetworkInterfaceByName(client.vnetVethName)
321321
return errors.Wrap(getErr, "failed to get vnet veth")
322322
}, numRetries, sleepInMs)
@@ -326,7 +326,7 @@ func (client *TransparentVlanEndpointClient) PopulateVM(epInfo *EndpointInfo) er
326326

327327
// Ensure container veth is created, as there may be a slight delay
328328
var containerIf *net.Interface
329-
err = RunWithRetries(func() error {
329+
err = retry.Do(func() error {
330330
var getErr error
331331
containerIf, getErr = client.netioshim.GetNetworkInterfaceByName(client.containerVethName)
332332
return errors.Wrap(getErr, "failed to get container veth")
@@ -712,16 +712,3 @@ func ExecuteInNS(nsc NamespaceClientInterface, nsName string, f func() error) er
712712
}()
713713
return f()
714714
}
715-
716-
func RunWithRetries(f func() error, maxRuns, sleepMs int) error {
717-
var err error
718-
for i := 0; i < maxRuns; i++ {
719-
err = f()
720-
if err == nil {
721-
break
722-
}
723-
logger.Info("Retrying after delay...", zap.String("error", err.Error()), zap.Int("retry", i), zap.Int("sleepMs", sleepMs))
724-
time.Sleep(time.Duration(sleepMs) * time.Millisecond)
725-
}
726-
return err
727-
}

network/transparent_vlan_endpointclient_linux_test.go

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -908,58 +908,3 @@ func TestTransparentVlanConfigureContainerInterfacesAndRoutes(t *testing.T) {
908908
})
909909
}
910910
}
911-
912-
func createFunctionWithFailurePattern(errorPattern []error) func() error {
913-
s := 0
914-
return func() error {
915-
if s >= len(errorPattern) {
916-
return nil
917-
}
918-
result := errorPattern[s]
919-
s++
920-
return result
921-
}
922-
}
923-
924-
func TestRunWithRetries(t *testing.T) {
925-
errMock := errors.New("mock error")
926-
runs := 4
927-
928-
tests := []struct {
929-
name string
930-
wantErr bool
931-
f func() error
932-
}{
933-
{
934-
name: "Succeed on first try",
935-
f: createFunctionWithFailurePattern([]error{}),
936-
wantErr: false,
937-
},
938-
{
939-
name: "Succeed on first try do not check again",
940-
f: createFunctionWithFailurePattern([]error{nil, errMock, errMock, errMock}),
941-
wantErr: false,
942-
},
943-
{
944-
name: "Succeed on last try",
945-
f: createFunctionWithFailurePattern([]error{errMock, errMock, errMock, nil, errMock}),
946-
wantErr: false,
947-
},
948-
{
949-
name: "Fail after too many attempts",
950-
f: createFunctionWithFailurePattern([]error{errMock, errMock, errMock, errMock, nil, nil}),
951-
wantErr: true,
952-
},
953-
}
954-
for _, tt := range tests {
955-
tt := tt
956-
t.Run(tt.name, func(t *testing.T) {
957-
err := RunWithRetries(tt.f, runs, 100)
958-
if tt.wantErr {
959-
require.Error(t, err)
960-
} else {
961-
require.NoError(t, err)
962-
}
963-
})
964-
}
965-
}

0 commit comments

Comments
 (0)