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 {
cacheRoot string
mirrorURLs []string
mirroredRepos []string
cfg CacheConfig
mirrorIdx atomic.Uint32
sf singleflight.Group //prevents duplicate downloads
mu sync.Mutex
client http.Client
}
func NewCache(cacheRoot string, mirrorURLs []string, mirroredRepos []string) *Cache {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
ResponseHeaderTimeout: 10 * time.Second,
type CacheConfig struct {
cacheRoot string
mirrorURLs []string
mirroredRepos []string
DialTimeout time.Duration
ResponseHeaderTimeout time.Duration
ClientTimeout time.Duration
}
return &Cache{
func NewCache(cacheRoot string, mirrorURLs []string, mirroredRepos []string) *Cache {
cfg := CacheConfig{
cacheRoot: cacheRoot,
mirrorURLs: mirrorURLs,
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{
Timeout: 15 * time.Second,
Timeout: cfg.ClientTimeout,
Transport: transport,
},
}
@@ -65,8 +79,8 @@ func (c *Cache) fetch(pkgName string) error {
// pkgName is relative to the localRoot
// ie pkgName includes /{repo}/os/{arch}/ and the actual name linux-x.x.x.pkg.tar.zst
tempPkgName := pkgName + ".tmp"
tempPkgPath := filepath.Join(c.cacheRoot, tempPkgName) //full tmp write path
outPkg := filepath.Join(c.cacheRoot, pkgName)
tempPkgPath := filepath.Join(c.cfg.cacheRoot, tempPkgName) //full tmp write path
outPkg := filepath.Join(c.cfg.cacheRoot, pkgName)
pkgURL := c.nextMirror() + pkgName
log.Printf("fetching %v", pkgURL)
@@ -102,5 +116,5 @@ func (c *Cache) fetch(pkgName string) error {
func (c *Cache) nextMirror() string {
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 {
t.Helper()
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) {
@@ -39,7 +41,7 @@ func TestFetchFileExists(t *testing.T) {
t.Fatalf("Fetch failed %v", err)
}
fakefilepath := filepath.Join(c.cacheRoot, "fakefile")
fakefilepath := filepath.Join(c.cfg.cacheRoot, "fakefile")
data, err := os.ReadFile(fakefilepath)
if err != nil {
+1 -1
View File
@@ -8,7 +8,7 @@ func (c *Cache) Refresh() error {
}
defer c.mu.Unlock()
for _, repo := range c.mirroredRepos {
for _, repo := range c.cfg.mirroredRepos {
if err := c.refreshDB(repo); err != nil {
return err
}