@@ -6,14 +6,29 @@ import (
66 "net/http"
77 "sort"
88 "strings"
9+ "sync"
910 "time"
1011
1112 "github.com/hashicorp/go-retryablehttp"
1213 "github.com/pkg/errors"
14+ "github.com/rs/zerolog/log"
1315)
1416
1517const (
1618 defaultHttpTimeout = 10 * time .Second
19+ cacheDuration = 6 * time .Hour
20+ )
21+
22+ // cachedConfig holds a cached configuration with its timestamp
23+ type cachedConfig struct {
24+ config Config
25+ timestamp time.Time
26+ }
27+
28+ // configCache holds the cached configuration
29+ var (
30+ configCache * cachedConfig
31+ cacheMutex sync.RWMutex
1732)
1833
1934// Config is configuration set by the organization
@@ -88,6 +103,18 @@ func uniqueStr(slice []string) []string {
88103}
89104
90105func getConfig (run RunMode , url string , httpClient * http.Client ) (ext Config , err error ) {
106+ // Check cache first
107+ cacheMutex .RLock ()
108+ // Return cached config if it exists and is not expired
109+ if configCache != nil && time .Since (configCache .timestamp ) < cacheDuration {
110+ log .Debug ().Msg ("getting zos config from cache" )
111+ config := configCache .config
112+ cacheMutex .RUnlock ()
113+ return config , nil
114+ }
115+ cacheMutex .RUnlock ()
116+ log .Debug ().Msg ("zos config cache expired fetching from git" )
117+ // Fetch new config from URL
91118 if ! strings .HasSuffix (url , "/" ) {
92119 url += "/"
93120 }
@@ -112,5 +139,13 @@ func getConfig(run RunMode, url string, httpClient *http.Client) (ext Config, er
112139 return ext , errors .Wrap (err , "failed to decode extended settings" )
113140 }
114141
142+ // Cache the new config
143+ cacheMutex .Lock ()
144+ configCache = & cachedConfig {
145+ config : ext ,
146+ timestamp : time .Now (),
147+ }
148+ cacheMutex .Unlock ()
149+
115150 return
116151}
0 commit comments