implmented fetch on cache miss

This commit is contained in:
2026-04-13 17:26:15 -06:00
parent dc21cc8f5b
commit 54315ced9d
6 changed files with 135 additions and 21 deletions
+76
View File
@@ -0,0 +1,76 @@
package cache
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"golang.org/x/sync/singleflight"
)
type Cache struct {
repo string
mirrorURL string
sf singleflight.Group
}
func NewCache(repo string, mirrorURL string) *Cache {
return &Cache{
repo: repo,
mirrorURL: mirrorURL,
}
}
func (c *Cache) Fetch(pkgPath string) error {
_, err, _ := c.sf.Do(pkgPath, func() (any, error) {
return nil, c.fetch(pkgPath)
})
return err
}
type UpstreamError struct {
StatusCode int
}
func (e *UpstreamError) Error() string {
return fmt.Sprintf("upstream returned %d", e.StatusCode)
}
func (c *Cache) fetch(pkgPath string) error {
// pkgPath is relative to the repo or mirror root
tempPkgPath := pkgPath + ".tmp"
tempPkgName := filepath.Join(c.repo, tempPkgPath) //full tmp write path
outPkg := filepath.Join(c.repo, pkgPath)
pkgURL := c.mirrorURL + pkgPath
resp, err := http.Get(pkgURL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return &UpstreamError{StatusCode: resp.StatusCode}
}
outFile, err := os.Create(tempPkgName)
if err != nil {
return err
}
defer outFile.Close()
_, err = io.Copy(outFile, resp.Body)
if err != nil {
os.Remove(tempPkgName)
return err
}
if err := os.Rename(tempPkgName, outPkg); err != nil {
os.Remove(tempPkgName)
return err
}
return nil
}