Skip to content

Commit 6c82f7a

Browse files
committed
use a wait group and simplify down
1 parent 5044c81 commit 6c82f7a

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

cns/restserver/helper_for_nodesubnet_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ func GetRestServiceObjectForNodeSubnetTest(t *testing.T, generator CNIConflistGe
7979
return interfaces, nil
8080
},
8181
},
82-
wscli: &fakes.WireserverClientFake{},
83-
ncSyncState: &NetworkContainerSyncState{},
82+
wscli: &fakes.WireserverClientFake{},
8483
}
8584
}
8685

cns/restserver/restserver.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type HTTPRestService struct {
102102
PnpIDByMacAddress map[string]string
103103
imdsClient imdsClient
104104
nodesubnetIPFetcher *nodesubnet.IPFetcher
105-
ncSyncState *NetworkContainerSyncState
105+
ncSyncState networkContainerSyncState
106106
}
107107

108108
type CNIConflistGenerator interface {
@@ -253,7 +253,6 @@ func NewHTTPRestService(config *common.ServiceConfig, wscli interfaceGetter, wsp
253253
homeAzMonitor: homeAzMonitor,
254254
cniConflistGenerator: gen,
255255
imdsClient: imdsClient,
256-
ncSyncState: &NetworkContainerSyncState{},
257256
}, nil
258257
}
259258

@@ -388,7 +387,7 @@ func (service *HTTPRestService) AttachIPConfigsHandlerMiddleware(middleware cns.
388387
service.IPConfigsHandlerMiddleware = middleware
389388
}
390389

391-
// GetNetworkContainerSyncState returns the NetworkContainerSyncState for external use.
390+
// Wait waits for the NetworkContainerSyncState to be ready or for the context to be done.
392391
func (service *HTTPRestService) Wait(ctx context.Context) {
393-
service.ncSyncState.WaitForConfList(ctx)
392+
service.ncSyncState.Wait(ctx)
394393
}

cns/restserver/synchostnc.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"strconv"
99
"strings"
10+
"sync"
1011
"sync/atomic"
1112
"time"
1213

@@ -18,48 +19,42 @@ import (
1819

1920
// TODO: make this file a sub pacakge?
2021

21-
// NetworkContainerSyncState manages the synchronization state for network container operations.
22-
type NetworkContainerSyncState struct {
23-
ncSynced chan struct{}
24-
ncSyncLoop atomic.Bool
22+
// NetworkContainerSyncState manages waiting on conflist to ge
23+
// Basically a wait group that can only be added once and waits with a context
24+
// meant to be used uninitialized and then started once the sync loop begins.
25+
type networkContainerSyncState struct {
26+
wg sync.WaitGroup
27+
started atomic.Bool
2528
}
2629

2730
// Start attempts to start the sync loop. Returns an error if already started.
28-
func (n *NetworkContainerSyncState) Start() error {
29-
if n == nil {
30-
return errors.New("NetworkContainerSyncState is nil")
31-
}
32-
if !n.ncSyncLoop.CompareAndSwap(false, true) {
31+
func (n *networkContainerSyncState) Start() error {
32+
if !n.started.CompareAndSwap(false, true) {
3333
return errors.New("sync loop already started")
3434
}
35-
n.ncSynced = make(chan struct{})
35+
n.wg.Add(1)
3636
return nil
3737
}
3838

39-
// NotifyReady closes the ncSynced channel to signal readiness.
40-
func (n *NetworkContainerSyncState) NotifyReady() {
41-
if n == nil || !n.ncSyncLoop.Load() {
39+
// NotifyReady called once
40+
func (n *networkContainerSyncState) NotifyReady() {
41+
if !n.started.Load() {
4242
return //nobody ever set this up just move on.
4343
}
44-
close(n.ncSynced)
44+
n.wg.Done()
4545
}
4646

47-
// WaitForConfList waits for the CNI conflist to be ready or for the context to be done.
48-
func (n *NetworkContainerSyncState) WaitForConfList(ctx context.Context) {
49-
if n == nil {
50-
return //do nothing if we never got intiialized.
51-
}
52-
53-
// Sync loop never set up, get out of here.
54-
if n.ncSyncLoop.Load() {
55-
return
56-
}
47+
// Wait waits for the CNI conflist to be ready or for the context to be done.
48+
func (n *networkContainerSyncState) Wait(ctx context.Context) {
49+
done := make(chan struct{})
50+
go func() {
51+
n.wg.Wait()
52+
close(done)
53+
}()
5754

5855
select {
59-
case <-n.ncSynced:
60-
return
56+
case <-done:
6157
case <-ctx.Done():
62-
return
6358
}
6459
}
6560

@@ -90,7 +85,7 @@ func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context,
9085
return nil
9186
}
9287

93-
// TODO: lowercase/unexport this function drive everything through
88+
// TODO: lowercase/unexport this function drive everything through StartSyncHostNCVersionLoop?
9489
// SyncHostNCVersion will check NC version from NMAgent and save it as host NC version in container status.
9590
// If NMAgent NC version got updated, CNS will refresh the pending programming IP status.
9691
func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMode string) {

0 commit comments

Comments
 (0)