Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

Stop a cached search from un-hiding an exercise removed upstream (#215)

Carter9ab4716888e0
diff --git a/backend/oedb/client.go b/backend/oedb/client.go
index 6bf4941..a35fe9f 100644
--- a/backend/oedb/client.go
+++ b/backend/oedb/client.go
@@ -303,6 +303,17 @@ func (c *Client) Revalidating() *Client {
return &v
}
+// DropCache forgets every cached response, in the map the Revalidating views share. A
+// caller that has just learned upstream changed uses it so the next read asks again
+// instead of being answered from before.
+func (c *Client) DropCache() {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ for k := range c.cache {
+ delete(c.cache, k)
+ }
+}
+
// revalidate refreshes a stale entry off the request path. It deliberately uses
// its own timeout-bounded context rather than the caller's: the caller has
// already been served, and cancelling the refresh when that request finishes
diff --git a/backend/oedb/client_test.go b/backend/oedb/client_test.go
index dfe8e93..ae1c694 100644
--- a/backend/oedb/client_test.go
+++ b/backend/oedb/client_test.go
@@ -723,3 +723,30 @@ func TestGet_304ForAnEvictedEntryRefetchesTheBody(t *testing.T) {
t.Errorf("calls = %d, want 3 (initial, conditional, refetch)", n)
}
}
+
+func TestDropCache_NextReadAsksAgain(t *testing.T) {
+ var calls int32
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ atomic.AddInt32(&calls, 1)
+ w.Header().Set("Cache-Control", "max-age=900")
+ fmt.Fprint(w, listBody("Bench"))
+ }))
+ defer srv.Close()
+
+ c := New(srv.URL, "test")
+ for i := 0; i < 2; i++ {
+ if _, err := c.get(context.Background(), "/x"); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if n := atomic.LoadInt32(&calls); n != 1 {
+ t.Fatalf("calls before the drop = %d, want 1 (second read from cache)", n)
+ }
+ c.Revalidating().DropCache()
+ if _, err := c.get(context.Background(), "/x"); err != nil {
+ t.Fatal(err)
+ }
+ if n := atomic.LoadInt32(&calls); n != 2 {
+ t.Errorf("calls after the drop = %d, want 2 (a view shares the cache it drops)", n)
+ }
+}
diff --git a/backend/stores/exercise.go b/backend/stores/exercise.go
index fafe769..3d1f0a8 100644
--- a/backend/stores/exercise.go
+++ b/backend/stores/exercise.go
@@ -738,6 +738,9 @@ func (s *ExerciseStore) markUpstreamMissing(items []oedb.Exercise) error {
}
if n, _ := res.RowsAffected(); n > 0 {
log.Printf("exercises: %d exercises no longer in the upstream catalog; hidden from search, kept for history", n)
+ // A cached search answer still lists them, and recording it would un-hide them
+ // until the next pass.
+ s.catalog.DropCache()
}
return nil
}
diff --git a/backend/stores/exercise_removal_test.go b/backend/stores/exercise_removal_test.go
index 77bb882..418eff9 100644
--- a/backend/stores/exercise_removal_test.go
+++ b/backend/stores/exercise_removal_test.go
@@ -3,9 +3,14 @@ package stores
import (
"context"
"database/sql"
+ "encoding/json"
"errors"
"fmt"
+ "net/http"
+ "net/http/httptest"
+ "slices"
"strings"
+ "sync/atomic"
"testing"
"github.com/Cawlumm/lyftr-backend/oedb"
@@ -378,3 +383,65 @@ func TestPurgeDroppedLocales_RefusesToDropMostLanguagesAtOnce(t *testing.T) {
t.Errorf("translations = %d, want 2 after dropping 1 of 3", n)
}
}
+
+// A search answered from the response cache can still list an exercise the daily pass
+// has just found removed. Recording that answer must not bring the exercise back.
+func TestSyncCatalog_RemovalIsNotUndoneByACachedSearch(t *testing.T) {
+ var removed atomic.Bool
+ both := []oedb.Exercise{catalogExercise("uuid-x", "Squat X"), catalogExercise("uuid-y", "Squat Y")}
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Cache-Control", "max-age=300")
+ items := both
+ if removed.Load() {
+ items = both[1:]
+ }
+ switch r.URL.Path {
+ case "/api/v1/meta/locales":
+ fmt.Fprint(w, `{"locales":[{"code":"en"}],"total":1}`)
+ case "/api/v1/exercises/export":
+ json.NewEncoder(w).Encode(map[string]any{"exercises": items})
+ default:
+ if removed.Load() && r.URL.Query().Get("q") != "" {
+ items = both[1:]
+ } else if !removed.Load() {
+ items = both
+ }
+ json.NewEncoder(w).Encode(map[string]any{"exercises": items, "total": len(items)})
+ }
+ }))
+ defer srv.Close()
+
+ s := NewExerciseStore(testDB(t))
+ s.UseCatalog(oedb.New(srv.URL, "test"))
+ names := func() []string {
+ out, err := s.Search(context.Background(), ExerciseFilter{Query: "squat", Limit: 50})
+ if err != nil {
+ t.Fatal(err)
+ }
+ var n []string
+ for _, e := range out {
+ n = append(n, e.Name)
+ }
+ return n
+ }
+
+ if got := names(); len(got) != 2 {
+ t.Fatalf("before the removal, search = %v, want both", got)
+ }
+ removed.Store(true)
+ if _, err := s.syncCatalog(context.Background()); err != nil {
+ t.Fatalf("sync: %v", err)
+ }
+ if got := names(); slices.Contains(got, "Squat X") {
+ t.Errorf("search after the removal = %v, want Squat X to stay hidden", got)
+ }
+ list, err := s.List(ExerciseFilter{Query: "squat", Limit: 50})
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, e := range list {
+ if e.Name == "Squat X" {
+ t.Error("the removed exercise was un-hidden by a search")
+ }
+ }
+}