@@ -17,9 +17,13 @@ import (
1717
1818//TODO make this file a sub pacakge?
1919
20- func (service * HTTPRestService ) StartSyncHostNCVersionLoop (ctx context.Context , cnsconfig configuration.CNSConfig ) {
21- //do we need a sync.once to protect this? should we error if this is called twice?
22- service .ncSynced = make (chan struct {})
20+ 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 {
24+ return errors .New ("SyncHostNCVersion loop already started" )
25+ }
26+ service .ncSyncLoop = true
2327 go func () {
2428 logger .Printf ("Starting SyncHostNCVersion loop." )
2529 // Periodically poll vfp programmed NC version from NMAgent
@@ -36,6 +40,7 @@ func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context,
3640 }
3741 }
3842 }()
43+ return nil
3944}
4045
4146// SyncHostNCVersion will check NC version from NMAgent and save it as host NC version in container status.
@@ -187,9 +192,11 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
187192// a conflist generator. If not, this is a no-op.
188193func (service * HTTPRestService ) mustGenerateCNIConflistOnce () {
189194 service .generateCNIConflistOnce .Do (func () {
190- if service .ncSynced != nil {
195+ service .Lock () //lock inside a do scary?
196+ if service .ncSyncLoop {
191197 close (service .ncSynced )
192198 }
199+ service .Unlock ()
193200 if err := service .cniConflistGenerator .Generate (); err != nil {
194201 panic ("unable to generate cni conflist with error: " + err .Error ())
195202 }
@@ -202,9 +209,13 @@ func (service *HTTPRestService) mustGenerateCNIConflistOnce() {
202209
203210func (service * HTTPRestService ) WaitForConfList (ctx context.Context ) {
204211 //sync loop never set up get out of here.
205- if service .ncSynced == nil {
206- return
207- }
212+ func () {
213+ service .Lock ()
214+ defer service .Unlock ()
215+ if ! service .ncSyncLoop {
216+ return
217+ }
218+ }()
208219
209220 select {
210221 case <- service .ncSynced :
0 commit comments