Skip to content

Commit 84fa79f

Browse files
committed
sync immediatly and newticker
1 parent 713c47c commit 84fa79f

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

cns/restserver/restserver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/http/pprof"
88
"sync"
9+
"sync/atomic"
910
"time"
1011

1112
"github.com/Azure/azure-container-networking/cns"
@@ -104,7 +105,7 @@ type HTTPRestService struct {
104105
nodesubnetIPFetcher *nodesubnet.IPFetcher
105106
//put in ncstate struct?
106107
ncSynced chan struct{}
107-
ncSyncLoop bool
108+
ncSyncLoop atomic.Bool
108109
}
109110

110111
type CNIConflistGenerator interface {

cns/restserver/synchostnc.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ import (
1818
//TODO make this file a sub pacakge?
1919

2020
func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context, cnsconfig configuration.CNSConfig) error {
21-
service.Lock() //could use a seperate lock or atomic bool.
22-
defer service.Unlock()
23-
if service.ncSyncLoop {
21+
if !service.ncSyncLoop.CompareAndSwap(false, true) {
2422
return errors.New("SyncHostNCVersion loop already started")
2523
}
26-
service.ncSyncLoop = true
2724
go func() {
2825
logger.Printf("Starting SyncHostNCVersion loop.")
2926
// Periodically poll vfp programmed NC version from NMAgent
30-
tickerChannel := time.Tick(time.Duration(cnsconfig.SyncHostNCVersionIntervalMs) * time.Millisecond)
27+
ticker := time.NewTicker(time.Duration(cnsconfig.SyncHostNCVersionIntervalMs) * time.Millisecond)
28+
timeout := time.Duration(cnsconfig.SyncHostNCVersionIntervalMs) * time.Millisecond
3129
for {
30+
timedCtx, cancel := context.WithTimeout(ctx, timeout)
31+
service.SyncHostNCVersion(timedCtx, cnsconfig.ChannelMode)
32+
cancel()
3233
select {
33-
case <-tickerChannel:
34-
timedCtx, cancel := context.WithTimeout(ctx, time.Duration(cnsconfig.SyncHostNCVersionIntervalMs)*time.Millisecond)
34+
case <-ticker.C:
35+
timedCtx, cancel := context.WithTimeout(ctx, timeout)
3536
service.SyncHostNCVersion(timedCtx, cnsconfig.ChannelMode)
3637
cancel()
3738
case <-ctx.Done():
@@ -43,6 +44,7 @@ func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context,
4344
return nil
4445
}
4546

47+
// TODO: lowercase/unexport this function drive everything through
4648
// SyncHostNCVersion will check NC version from NMAgent and save it as host NC version in container status.
4749
// If NMAgent NC version got updated, CNS will refresh the pending programming IP status.
4850
func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMode string) {
@@ -192,11 +194,9 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
192194
// a conflist generator. If not, this is a no-op.
193195
func (service *HTTPRestService) mustGenerateCNIConflistOnce() {
194196
service.generateCNIConflistOnce.Do(func() {
195-
service.Lock() //lock inside a do scary?
196-
if service.ncSyncLoop {
197+
if service.ncSyncLoop.Load() {
197198
close(service.ncSynced)
198199
}
199-
service.Unlock()
200200
if err := service.cniConflistGenerator.Generate(); err != nil {
201201
panic("unable to generate cni conflist with error: " + err.Error())
202202
}
@@ -209,13 +209,9 @@ func (service *HTTPRestService) mustGenerateCNIConflistOnce() {
209209

210210
func (service *HTTPRestService) WaitForConfList(ctx context.Context) {
211211
//sync loop never set up get out of here.
212-
func() {
213-
service.Lock()
214-
defer service.Unlock()
215-
if !service.ncSyncLoop {
216-
return
217-
}
218-
}()
212+
if !service.ncSyncLoop.Load() {
213+
return
214+
}
219215

220216
select {
221217
case <-service.ncSynced:

0 commit comments

Comments
 (0)