added timeouts to fetch

This commit is contained in:
2026-04-16 22:26:06 -06:00
parent 8345f333c3
commit 24e62605b0
3 changed files with 78 additions and 2 deletions
+3 -1
View File
@@ -1,9 +1,11 @@
- Basic testing for internal/cache - Basic config Testing
- Add chi for mux
- add cli option for config location - add cli option for config location
- Complete testing - Complete testing
- Deployment(PKGBUILD, systemd, bootstrap script?, systemd sync timer) - Deployment(PKGBUILD, systemd, bootstrap script?, systemd sync timer)
- More complete sync(refresh packages on schedule with db, prefetch updates to pkgs we already have) - More complete sync(refresh packages on schedule with db, prefetch updates to pkgs we already have)
- Build server/tool - Build server/tool
- ~Basic testing for internal/cache~
- ~basic file server that fulfills pacman api~ - ~basic file server that fulfills pacman api~
- ~fetch requested files from mirror~ - ~fetch requested files from mirror~
- ~DB sync from mirror~ - ~DB sync from mirror~
+15 -1
View File
@@ -3,10 +3,12 @@ package cache
import ( import (
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"time"
"golang.org/x/sync/singleflight" "golang.org/x/sync/singleflight"
) )
@@ -17,13 +19,25 @@ type Cache struct {
mirroredRepos []string mirroredRepos []string
sf singleflight.Group //prevents duplicate downloads sf singleflight.Group //prevents duplicate downloads
mu sync.Mutex mu sync.Mutex
client http.Client
} }
func NewCache(cacheRoot string, mirrorURL string) *Cache { func NewCache(cacheRoot string, mirrorURL string) *Cache {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
ResponseHeaderTimeout: 10 * time.Second,
}
return &Cache{ return &Cache{
cacheRoot: cacheRoot, cacheRoot: cacheRoot,
mirrorURL: mirrorURL, mirrorURL: mirrorURL,
mirroredRepos: []string{"core", "extra"}, mirroredRepos: []string{"core", "extra"},
client: http.Client{
Timeout: 15 * time.Second,
Transport: transport,
},
} }
} }
@@ -50,7 +64,7 @@ func (c *Cache) fetch(pkgName string) error {
outPkg := filepath.Join(c.cacheRoot, pkgName) outPkg := filepath.Join(c.cacheRoot, pkgName)
pkgURL := c.mirrorURL + pkgName pkgURL := c.mirrorURL + pkgName
resp, err := http.Get(pkgURL) resp, err := c.client.Get(pkgURL)
if err != nil { if err != nil {
return err return err
} }
+60
View File
@@ -2,12 +2,14 @@ package cache
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"time"
) )
func newTestServer(t *testing.T, handler http.HandlerFunc) *httptest.Server { func newTestServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
@@ -46,3 +48,61 @@ func TestFetchFileExists(t *testing.T) {
t.Errorf("expected file to contain %s got %s", expected, data) t.Errorf("expected file to contain %s got %s", expected, data)
} }
} }
func TestFetchNotFound(t *testing.T) {
svr := newTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
c := newTestCache(t, svr.URL+"/")
err := c.Fetch("fakefile")
var upstreamErr *UpstreamError
if !errors.As(err, &upstreamErr) {
t.Fatalf("expected UpstreamError fot %v", err)
}
if upstreamErr.StatusCode != http.StatusNotFound {
t.Errorf("expected 404 got %d", upstreamErr.StatusCode)
}
}
func TestFetchSrvError(t *testing.T) {
svr := newTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
c := newTestCache(t, svr.URL+"/")
err := c.Fetch("fakefile")
var upstreamErr *UpstreamError
if !errors.As(err, &upstreamErr) {
t.Fatalf("expected UpstreamError fot %v", err)
}
if upstreamErr.StatusCode != http.StatusInternalServerError {
t.Errorf("expected 500 got %d", upstreamErr.StatusCode)
}
}
func TestFetchSrvDead(t *testing.T) {
svr := newTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-r.Context().Done():
return
case <-time.After(60 * time.Second):
fmt.Fprint(w, "too late")
}
}))
defer svr.Close()
c := newTestCache(t, svr.URL+"/")
err := c.Fetch("fakefile")
if err == nil {
t.Fatal("expected err got nil")
}
var upstreamErr *UpstreamError
if errors.As(err, &upstreamErr) {
t.Error("expected network error not UpstreamError")
}
}