db update requires own download logic for prefetch, previous logic caused check for updates to use old database and all databases were being updated before all repos were checked for cached pkgs

This commit is contained in:
2026-05-26 00:39:18 -06:00
parent ca5addd913
commit 76ca7567e5
4 changed files with 57 additions and 25 deletions
+47 -17
View File
@@ -1,27 +1,57 @@
package cache
import "path/filepath"
import (
"io"
"log/slog"
"net/http"
"path/filepath"
)
func (c *Cache) FetchDB() error {
if !c.refreshMu.TryLock() {
return nil
}
defer c.refreshMu.Unlock()
for _, repo := range c.cfg.mirroredRepos {
if err := c.fetchDB(repo); err != nil {
return err
}
}
return nil
}
func (c *Cache) fetchDB(repo string) error {
func (c *Cache) FetchDB(repo string) error {
dbFile := repo + ".db.tar.gz"
dbPath := filepath.Join(repo, "os/x86_64", dbFile)
_, _, err := c.getStream(dbPath)
url := c.nextMirror() + dbPath
// set the user agent
req, err := http.NewRequest("GET", url, nil)
if err != nil {
slog.Error("failed create request", "err", err)
return err
}
req.Header.Set("User-Agent", userAgent)
resp, err := c.client.Do(req)
if err != nil {
slog.Warn("fetch failed", "url", url, "err", err)
return err
}
if resp.StatusCode != 200 {
slog.Info("fetch returned", "url", url, "status", resp.StatusCode)
return &UpstreamError{StatusCode: resp.StatusCode}
}
defer resp.Body.Close()
tmpPath := dbPath + ".tmp"
tmpFile, err := c.cr.Create(tmpPath)
if err != nil {
return err
}
if _, err := io.Copy(tmpFile, resp.Body); err != nil {
tmpFile.Close()
c.cr.Remove(tmpPath)
return err
}
if err := tmpFile.Close(); err != nil {
c.cr.Remove(tmpPath)
return err
}
if err := c.cr.Rename(tmpPath, dbPath); err != nil {
c.cr.Remove(tmpPath)
slog.Error("failed to move tmpfile to permanent file", "file", tmpFile, "err", err)
return err
}
return nil
}
+2 -2
View File
@@ -16,7 +16,7 @@ const (
)
type CacheClient interface {
FetchDB() error
FetchDB(repo string) error
Fetch(relpath string) (*cache.CacheFile, error)
}
@@ -53,7 +53,7 @@ func (r *RepoSync) Sync() error {
// call cache db fetch
slog.Info("refreshing databases")
if err := r.c.FetchDB(); err != nil {
if err := r.c.FetchDB(repo); err != nil {
return err
}
+1 -1
View File
@@ -19,7 +19,7 @@ type mockCache struct {
fetchDBCalled bool
}
func (m *mockCache) FetchDB() error {
func (m *mockCache) FetchDB(repo string) error {
m.fetchDBCalled = true
return nil
}