From 9b0610b87979577ab5a8385d0e09eb98f62dff08 Mon Sep 17 00:00:00 2001 From: Eric Phillips Date: Mon, 18 May 2026 01:14:57 -0600 Subject: [PATCH] tailer struct was incorrect and used incorrect channel --- internal/cache/cache_test.go | 8 ++++++-- internal/cache/download.go | 9 ++++++++- internal/cache/get_stream.go | 1 + internal/cache/refresh.go | 2 +- internal/cache/tailer.go | 9 +++++---- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/internal/cache/cache_test.go b/internal/cache/cache_test.go index 03fe933..5bf7c45 100644 --- a/internal/cache/cache_test.go +++ b/internal/cache/cache_test.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "log/slog" "net/http" "net/http/httptest" "os" @@ -22,6 +23,8 @@ func newTestServer(t *testing.T, handler http.HandlerFunc) *httptest.Server { func newTestCache(t *testing.T, mirrorURLs []string) *Cache { t.Helper() + // set slog to debug + slog.SetLogLoggerLevel(slog.LevelDebug) mirroredRepos := []string{"core", "extra"} c, err := NewCache(t.TempDir(), mirrorURLs, mirroredRepos) if err != nil { @@ -33,7 +36,6 @@ func newTestCache(t *testing.T, mirrorURLs []string) *Cache { func TestCacheHit(t *testing.T) { const expected = "This is fake file contents" - c := newTestCache(t, []string{"http://example.com/"}) tmpFileName := "fakeFile" err := c.cr.WriteFile(tmpFileName, []byte(expected), 0644) @@ -78,10 +80,12 @@ func TestCacheMissExists(t *testing.T) { c := newTestCache(t, []string{svr.URL + "/"}) - _, err := c.Fetch("fakefile") + cf, err := c.Fetch("fakefile") if err != nil { t.Fatalf("Fetch failed %v", err) } + io.Copy(io.Discard, cf.Reader) + cf.Reader.Close() data, err := c.cr.ReadFile("fakefile") if err != nil { diff --git a/internal/cache/download.go b/internal/cache/download.go index de143b7..0546cf6 100644 --- a/internal/cache/download.go +++ b/internal/cache/download.go @@ -9,6 +9,8 @@ import ( ) func (c *Cache) downloadWrangle(relPath string, flight *inFlight, tmpFile *os.File) { + + slog.Debug("wrangle starting", "relPath", relPath) // defer map cleanup and signal client handle done defer c.cleanupFlight(relPath, flight) @@ -37,6 +39,7 @@ func (c *Cache) downloadWrangle(relPath string, flight *inFlight, tmpFile *os.Fi } return } + slog.Debug("wrangle download complete", "err", err) // mv file to final location err = c.cr.Rename(flight.tmpPath, relPath) @@ -47,7 +50,7 @@ func (c *Cache) downloadWrangle(relPath string, flight *inFlight, tmpFile *os.Fi } return } - return + slog.Debug("file moved to final location", "err", err) } func (c *Cache) downloadToDisk(url string, tmpFile *os.File) error { @@ -79,6 +82,9 @@ func (c *Cache) downloadToDisk(url string, tmpFile *os.File) error { if err != nil { return err } + if err = tmpFile.Sync(); err != nil { + return err + } return nil } @@ -86,5 +92,6 @@ func (c *Cache) cleanupFlight(key string, f *inFlight) { c.inFlightMu.Lock() delete(c.inFlight, key) c.inFlightMu.Unlock() + slog.Debug("closing done channel") close(f.done) } diff --git a/internal/cache/get_stream.go b/internal/cache/get_stream.go index e4adb7c..c13d5c4 100644 --- a/internal/cache/get_stream.go +++ b/internal/cache/get_stream.go @@ -35,6 +35,7 @@ func (c *Cache) getStream(relPath string) (*inFlight, *os.File, error) { flight := &inFlight{ tmpPath: tmpPath, + done: make(chan struct{}), } c.inFlight[relPath] = flight diff --git a/internal/cache/refresh.go b/internal/cache/refresh.go index 0478a76..01fa3f1 100644 --- a/internal/cache/refresh.go +++ b/internal/cache/refresh.go @@ -19,7 +19,7 @@ func (c *Cache) Refresh() error { func (c *Cache) refreshDB(repo string) error { dbFile := repo + ".db.tar.gz" dbPath := filepath.Join(repo, "os/x86_64", dbFile) - err := c.getStream(dbPath) + _, _, err := c.getStream(dbPath) if err != nil { return err } diff --git a/internal/cache/tailer.go b/internal/cache/tailer.go index 3036aab..b40faf1 100644 --- a/internal/cache/tailer.go +++ b/internal/cache/tailer.go @@ -2,31 +2,32 @@ package cache import ( "io" + "log/slog" "os" "time" ) type tailer struct { f *os.File - done <-chan struct{} flight *inFlight } func (t *tailer) Read(p []byte) (int, error) { for { n, err := t.f.Read(p) + slog.Debug("tailer read", "n", n, "err", err) if n > 0 { return n, nil } if err == io.EOF { select { - case <-t.done: + case <-t.flight.done: if t.flight.err != nil { return 0, t.flight.err } return t.f.Read(p) // send remainiing bytes - default: - time.Sleep(50 * time.Millisecond) + case <-time.After(50 * time.Millisecond): + slog.Debug("tailer waiting for more data") continue } }