added config struct for cache so we can change timeouts in testing

This commit is contained in:
2026-04-21 09:51:09 -06:00
parent e9f0a49077
commit b52df05565
3 changed files with 35 additions and 19 deletions
+28 -14
View File
@@ -16,29 +16,43 @@ import (
) )
type Cache struct { type Cache struct {
cacheRoot string cfg CacheConfig
mirrorURLs []string
mirroredRepos []string
mirrorIdx atomic.Uint32 mirrorIdx atomic.Uint32
sf singleflight.Group //prevents duplicate downloads sf singleflight.Group //prevents duplicate downloads
mu sync.Mutex mu sync.Mutex
client http.Client client http.Client
} }
func NewCache(cacheRoot string, mirrorURLs []string, mirroredRepos []string) *Cache { type CacheConfig struct {
transport := &http.Transport{ cacheRoot string
DialContext: (&net.Dialer{ mirrorURLs []string
Timeout: 5 * time.Second, mirroredRepos []string
}).DialContext, DialTimeout time.Duration
ResponseHeaderTimeout: 10 * time.Second, ResponseHeaderTimeout time.Duration
ClientTimeout time.Duration
} }
return &Cache{ func NewCache(cacheRoot string, mirrorURLs []string, mirroredRepos []string) *Cache {
cfg := CacheConfig{
cacheRoot: cacheRoot, cacheRoot: cacheRoot,
mirrorURLs: mirrorURLs, mirrorURLs: mirrorURLs,
mirroredRepos: mirroredRepos, mirroredRepos: mirroredRepos,
DialTimeout: 5 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ClientTimeout: 15 * time.Second,
}
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: cfg.DialTimeout,
}).DialContext,
ResponseHeaderTimeout: cfg.ResponseHeaderTimeout,
}
return &Cache{
cfg: cfg,
client: http.Client{ client: http.Client{
Timeout: 15 * time.Second, Timeout: cfg.ClientTimeout,
Transport: transport, Transport: transport,
}, },
} }
@@ -65,8 +79,8 @@ func (c *Cache) fetch(pkgName string) error {
// pkgName is relative to the localRoot // pkgName is relative to the localRoot
// ie pkgName includes /{repo}/os/{arch}/ and the actual name linux-x.x.x.pkg.tar.zst // ie pkgName includes /{repo}/os/{arch}/ and the actual name linux-x.x.x.pkg.tar.zst
tempPkgName := pkgName + ".tmp" tempPkgName := pkgName + ".tmp"
tempPkgPath := filepath.Join(c.cacheRoot, tempPkgName) //full tmp write path tempPkgPath := filepath.Join(c.cfg.cacheRoot, tempPkgName) //full tmp write path
outPkg := filepath.Join(c.cacheRoot, pkgName) outPkg := filepath.Join(c.cfg.cacheRoot, pkgName)
pkgURL := c.nextMirror() + pkgName pkgURL := c.nextMirror() + pkgName
log.Printf("fetching %v", pkgURL) log.Printf("fetching %v", pkgURL)
@@ -102,5 +116,5 @@ func (c *Cache) fetch(pkgName string) error {
func (c *Cache) nextMirror() string { func (c *Cache) nextMirror() string {
idx := c.mirrorIdx.Add(1) - 1 idx := c.mirrorIdx.Add(1) - 1
return c.mirrorURLs[idx%uint32(len(c.mirrorURLs))] return c.cfg.mirrorURLs[idx%uint32(len(c.cfg.mirrorURLs))]
} }
+4 -2
View File
@@ -22,7 +22,9 @@ func newTestServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
func newTestCache(t *testing.T, mirrorURL []string) *Cache { func newTestCache(t *testing.T, mirrorURL []string) *Cache {
t.Helper() t.Helper()
mirroredRepos := []string{"core", "extra"} mirroredRepos := []string{"core", "extra"}
return NewCache(t.TempDir(), mirrorURL, mirroredRepos) c := NewCache(t.TempDir(), mirrorURL, mirroredRepos)
c.client.Timeout = 500 * time.Millisecond
return c
} }
func TestFetchFileExists(t *testing.T) { func TestFetchFileExists(t *testing.T) {
@@ -39,7 +41,7 @@ func TestFetchFileExists(t *testing.T) {
t.Fatalf("Fetch failed %v", err) t.Fatalf("Fetch failed %v", err)
} }
fakefilepath := filepath.Join(c.cacheRoot, "fakefile") fakefilepath := filepath.Join(c.cfg.cacheRoot, "fakefile")
data, err := os.ReadFile(fakefilepath) data, err := os.ReadFile(fakefilepath)
if err != nil { if err != nil {
+1 -1
View File
@@ -8,7 +8,7 @@ func (c *Cache) Refresh() error {
} }
defer c.mu.Unlock() defer c.mu.Unlock()
for _, repo := range c.mirroredRepos { for _, repo := range c.cfg.mirroredRepos {
if err := c.refreshDB(repo); err != nil { if err := c.refreshDB(repo); err != nil {
return err return err
} }