@@ -18,20 +18,21 @@ import (
1818//TODO make this file a sub pacakge?
1919
2020func (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.
4850func (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.
193195func (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
210210func (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