Skip to content

Commit

Permalink
feat: implement cache refresh mechanism in SMDClient
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlovelltroy committed Jan 8, 2025
1 parent 40c8682 commit 186181b
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions internal/smdclient/SMDclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type SMDClient struct {
nodes map[string]NodeMapping
nodesMutex *sync.Mutex
nodes_last_update time.Time
stopCacheRefresh chan struct{}
}

type NodeInterface struct {
Expand All @@ -74,15 +75,14 @@ func NewSMDClient(clusterName, baseurl, jwtURL, accessToken, certPath string, in
certPool *x509.CertPool
)

// try and load the cert if path is provied first
// try and load the cert if path is provided first
if certPath != "" {
cacert, err := os.ReadFile(certPath)
if err != nil {
return nil, fmt.Errorf("failed to read cert from path %s: %v", certPath, err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(cacert)

}

// set up the HTTP client's config
Expand All @@ -109,11 +109,42 @@ func NewSMDClient(clusterName, baseurl, jwtURL, accessToken, certPath string, in
nodesMutex: &sync.Mutex{},
nodes_last_update: time.Now(),
nodes: make(map[string]NodeMapping),
stopCacheRefresh: make(chan struct{}),
}
client.PopulateNodes()

// Start the cache refresh goroutine
go client.startCacheRefresh()

return client, nil
}

// startCacheRefresh starts a goroutine that refreshes the cache every minute
func (s *SMDClient) startCacheRefresh() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()

for {
select {
case <-ticker.C:
s.RefreshCache()
case <-s.stopCacheRefresh:
return
}
}
}

// RefreshCache refreshes the cache
func (s *SMDClient) RefreshCache() {
s.nodesMutex.Lock()
defer s.nodesMutex.Unlock()
s.PopulateNodes()
}

// StopCacheRefresh stops the cache refresh goroutine
func (s *SMDClient) StopCacheRefresh() {
close(s.stopCacheRefresh)
}

// ClusterName returns the name of the cluster
func (s *SMDClient) ClusterName() string {
return s.clusterName
Expand Down

0 comments on commit 186181b

Please sign in to comment.