diff --git a/internal/importer/fourthwall.go b/internal/importer/fourthwall.go index 8463305..6030b77 100644 --- a/internal/importer/fourthwall.go +++ b/internal/importer/fourthwall.go @@ -216,7 +216,7 @@ func SyncFourthwall(opt FourthwallOptions) (*FourthwallReport, error) { if opt.Currency != "" { q.Set("currency", opt.Currency) } - res, err := opt.Client.Get(strings.TrimRight(opt.API, "/") + p + "?" + q.Encode()) + res, err := fwFresh(opt.Client, strings.TrimRight(opt.API, "/")+p+"?"+q.Encode()) if err != nil { return err } @@ -592,6 +592,41 @@ func fwBuy(checkout, variant, cur string) string { return checkout + "/cart/checkout?" + q.Encode() } +// fwStaleWait is how long to give Fourthwall's cache to fetch a fresh copy. +var fwStaleWait = 2 * time.Second + +// fwFresh gets u, and gets it again when the answer is a stale copy. +// Fourthwall's API sits behind a cache that, after a quiet spell, answers +// with the copy it has, however old, and fetches a fresh one behind it: the +// first sync after an edit could miss it. A copy older than its max-age is +// asked for again, a few times, before it's taken as it is. +func fwFresh(c *http.Client, u string) (*http.Response, error) { + for try := 0; ; try++ { + res, err := c.Get(u) + if err != nil || res.StatusCode != http.StatusOK || try == 3 || !staleCopy(res.Header) { + return res, err + } + res.Body.Close() + time.Sleep(fwStaleWait) + } +} + +// staleCopy says whether a cached answer is older than its max-age. +func staleCopy(h http.Header) bool { + age, err := strconv.Atoi(strings.TrimSpace(h.Get("Age"))) + if err != nil { + return false + } + for _, d := range strings.Split(h.Get("Cache-Control"), ",") { + if v, ok := strings.CutPrefix(strings.TrimSpace(strings.ToLower(d)), "max-age="); ok { + if max, err := strconv.Atoi(v); err == nil { + return age > max + } + } + } + return false +} + func fetchWith(c *http.Client, u string) ([]byte, error) { p, err := url.Parse(u) if err != nil || (p.Scheme != "https" && p.Scheme != "http") { diff --git a/internal/importer/fourthwall_test.go b/internal/importer/fourthwall_test.go index 6a6abba..832adf7 100644 --- a/internal/importer/fourthwall_test.go +++ b/internal/importer/fourthwall_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "image" "image/color" + "io" "net/http" "net/http/httptest" "os" @@ -199,3 +200,36 @@ func TestFourthwallPhotosByColor(t *testing.T) { t.Errorf("one color, the product's own list: %s", g) } } + +func TestFourthwallStaleCopy(t *testing.T) { + fwStaleWait = 0 + calls := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + w.Header().Set("Cache-Control", "max-age=60,public") + if calls == 1 { + w.Header().Set("Age", "2037") // the cache's old copy + w.Write([]byte("old")) + return + } + w.Header().Set("Age", "0") + w.Write([]byte("new")) + })) + defer srv.Close() + res, err := fwFresh(srv.Client(), srv.URL) + if err != nil { + t.Fatal(err) + } + b, _ := io.ReadAll(res.Body) + res.Body.Close() + if string(b) != "new" || calls != 2 { + t.Fatalf("got %q after %d calls", b, calls) + } + for h, want := range map[[2]string]bool{ + {"61", "max-age=60"}: true, {"60", "public, max-age=60"}: false, {"", "max-age=60"}: false, {"999", "no-store"}: false, + } { + if got := staleCopy(http.Header{"Age": {h[0]}, "Cache-Control": {h[1]}}); got != want { + t.Errorf("Age %q, %q: stale %v", h[0], h[1], got) + } + } +}