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
+30 -16
View File
@@ -16,29 +16,43 @@ import (
) )
type Cache struct { type Cache struct {
cacheRoot string cfg CacheConfig
mirrorURLs []string mirrorIdx atomic.Uint32
mirroredRepos []string sf singleflight.Group //prevents duplicate downloads
mirrorIdx atomic.Uint32 mu sync.Mutex
sf singleflight.Group //prevents duplicate downloads client http.Client
mu sync.Mutex }
client http.Client
type CacheConfig struct {
cacheRoot string
mirrorURLs []string
mirroredRepos []string
DialTimeout time.Duration
ResponseHeaderTimeout time.Duration
ClientTimeout time.Duration
} }
func NewCache(cacheRoot string, mirrorURLs []string, mirroredRepos []string) *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{ transport := &http.Transport{
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: 5 * time.Second, Timeout: cfg.DialTimeout,
}).DialContext, }).DialContext,
ResponseHeaderTimeout: 10 * time.Second, ResponseHeaderTimeout: cfg.ResponseHeaderTimeout,
} }
return &Cache{ return &Cache{
cacheRoot: cacheRoot, cfg: cfg,
mirrorURLs: mirrorURLs,
mirroredRepos: mirroredRepos,
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
} }