@@ -123,6 +123,7 @@ type CoreAgentService struct {
123123 // help when triaging via logs.
124124 rcType string
125125
126+ api api.API
126127 clock clock.Clock
127128 hostname string
128129 tagsGetter func () []string
@@ -162,7 +163,6 @@ type CoreAgentService struct {
162163 sync.Mutex
163164
164165 uptane coreAgentUptaneClient
165- api api.API
166166
167167 firstUpdate bool
168168
@@ -248,13 +248,15 @@ func newOrgStatusPoller(refreshInterval time.Duration) *orgStatusPoller {
248248}
249249
250250// start begins the periodic polling of org status
251- func (p * orgStatusPoller ) start (clock clock.Clock , getAPI func () api.API , rcType string ) {
251+ func (p * orgStatusPoller ) start (clock clock.Clock , apiClient api.API , rcType string ) {
252252 go func () {
253- p .poll (getAPI (), rcType )
253+ timer := clock .Timer (0 )
254+ defer timer .Stop ()
254255 for {
255256 select {
256- case <- clock .After (p .refreshInterval ):
257- p .poll (getAPI (), rcType )
257+ case <- timer .C :
258+ p .poll (apiClient , rcType )
259+ timer .Reset (p .refreshInterval )
258260 case <- p .stopChan :
259261 log .Infof ("[%s] Stopping Remote Config org status poller" , rcType )
260262 return
@@ -601,6 +603,7 @@ func NewService(cfg model.Reader, rcType, baseRawURL, hostname string, tagsGette
601603
602604 now := clock .Now ().UTC ()
603605 cas := & CoreAgentService {
606+ api : http ,
604607 rcType : rcType ,
605608 startupTime : now ,
606609 hostname : hostname ,
@@ -635,7 +638,6 @@ func NewService(cfg model.Reader, rcType, baseRawURL, hostname string, tagsGette
635638 cas .mu .backoffErrorCount = 0
636639 cas .mu .products = make (map [rdata.Product ]struct {})
637640 cas .mu .newProducts = make (map [rdata.Product ]struct {})
638- cas .mu .api = http
639641 cas .mu .uptane = uptaneClient
640642
641643 cfg .OnUpdate (cas .apiKeyUpdateCallback ())
@@ -655,7 +657,7 @@ func newRCBackendOrgUUIDProvider(http api.API) uptane.OrgUUIDProvider {
655657func (s * CoreAgentService ) Start () {
656658 refreshBypassCh := make (chan chan <- struct {})
657659 s .refreshBypassCh = refreshBypassCh
658- s .orgStatusPoller .start (s .clock , s .getAPI , s .rcType )
660+ s .orgStatusPoller .start (s .clock , s .api , s .rcType )
659661
660662 go func () {
661663 if s .disableConfigPollLoop {
@@ -671,9 +673,7 @@ func (s *CoreAgentService) Start() {
671673// UpdatePARJWT updates the stored JWT for Private Action Runners
672674// for authentication to the remote config backend.
673675func (s * CoreAgentService ) UpdatePARJWT (jwt string ) {
674- s .mu .Lock ()
675- s .mu .api .UpdatePARJWT (jwt )
676- s .mu .Unlock ()
676+ s .api .UpdatePARJWT (jwt )
677677}
678678
679679func startWithAgentPollLoop (s * CoreAgentService , refreshBypassRequests <- chan chan <- struct {}) {
@@ -762,12 +762,6 @@ func (s *CoreAgentService) Stop() error {
762762 return err
763763}
764764
765- func (s * CoreAgentService ) getAPI () api.API {
766- s .mu .Lock ()
767- defer s .mu .Unlock ()
768- return s .mu .api
769- }
770-
771765func (s * CoreAgentService ) calculateRefreshInterval () time.Duration {
772766 s .mu .Lock ()
773767 backoffErrorCount := s .mu .backoffErrorCount
@@ -824,7 +818,7 @@ func (s *CoreAgentService) refresh() error {
824818 s .mu .Unlock ()
825819 defer s .mu .Lock ()
826820 ctx := context .Background ()
827- response , err = s .mu . api .Fetch (ctx , request )
821+ response , err = s .api .Fetch (ctx , request )
828822 }()
829823 s .mu .lastUpdateErr = nil
830824 if err != nil {
@@ -1091,7 +1085,7 @@ func (s *CoreAgentService) apiKeyUpdateCallback() func(string, model.Source, any
10911085 s .mu .Lock ()
10921086 defer s .mu .Unlock ()
10931087
1094- s .mu . api .UpdateAPIKey (newKey )
1088+ s .api .UpdateAPIKey (newKey )
10951089
10961090 // Verify that the Org UUID hasn't changed
10971091 storedOrgUUID , err := s .mu .uptane .StoredOrgUUID ()
@@ -1101,7 +1095,7 @@ func (s *CoreAgentService) apiKeyUpdateCallback() func(string, model.Source, any
11011095 }
11021096
11031097 // TODO: Do not hold the mutex while calling FetchOrgData.
1104- newOrgUUID , err := s .mu . api .FetchOrgData (context .Background ())
1098+ newOrgUUID , err := s .api .FetchOrgData (context .Background ())
11051099 if err != nil {
11061100 log .Warnf ("Could not get org uuid: %s" , err )
11071101 return
@@ -1161,7 +1155,7 @@ func (s *CoreAgentService) ConfigResetState() (*pbgo.ResetStateConfigResponse, e
11611155 }
11621156 uptaneClient , err := uptane .NewCoreAgentClientWithRecreatedTransactionalStore (
11631157 metadata ,
1164- newRCBackendOrgUUIDProvider (s .mu . api ),
1158+ newRCBackendOrgUUIDProvider (s .api ),
11651159 opt ... ,
11661160 )
11671161 if err != nil {
0 commit comments