Commit eaec14c

Eric Bower  ·  2026-04-20 19:53:13 -0400 EDT
parent 54c886f
refactor(httpcache): ignore routes should use no-store
2 files changed,  +15, -18
+8, -2
......@@ -123,7 +123,6 @@ func NewPgsHttpCache(cfg *PgsConfig, upstream http.Handler) *httpcache.HttpCache
123123 TxtPrefix: cfg.TxtPrefix,
124124 },
125125 CacheMetrics: metrics,
126- IgnoreRoutes: []string{"/check", "/_metrics"},
127126 }
128127 httpCache.Logger.Info("httpcache initiated", "ttl", httpCache.Ttl, "storage", "lru")
129128 return httpCache
......@@ -212,7 +211,11 @@ func (web *WebRouter) initRouters() {
212211 // root domain
213212 rootRouter := http.NewServeMux()
214213 rootRouter.HandleFunc("GET /check", web.checkHandler)
215- rootRouter.HandleFunc("GET /_metrics", promhttp.Handler().ServeHTTP)
214+ rootRouter.HandleFunc("GET /_metrics", func(w http.ResponseWriter, r *http.Request) {
215+ // we do *not* want to cache this handler
216+ w.Header().Set("cache-control", "no-store")
217+ promhttp.Handler().ServeHTTP(w, r)
218+ })
216219 rootRouter.Handle("GET /main.css", web.serveFile("main.css", "text/css"))
217220 rootRouter.Handle("GET /favicon-16x16.png", web.serveFile("favicon-16x16.png", "image/png"))
218221 rootRouter.Handle("GET /favicon.ico", web.serveFile("favicon.ico", "image/x-icon"))
......@@ -316,6 +319,9 @@ func (web *WebRouter) checkHandler(w http.ResponseWriter, r *http.Request) {
316319 hostDomain := r.URL.Query().Get("domain")
317320 appDomain := strings.Split(cfg.Domain, ":")[0]
318321
322+ // we do *not* want to cache this handler
323+ w.Header().Set("cache-control", "no-store")
324+
319325 if !strings.Contains(hostDomain, appDomain) {
320326 subdomain := router.GetCustomDomain(hostDomain, cfg.TxtPrefix)
321327 props, err := router.GetProjectFromSubdomain(subdomain)
+7, -16
......@@ -45,11 +45,10 @@ func (p *DefaultCacheMetrics) AddUpstreamRequest() {}
4545 type HttpCache struct {
4646 CacheKey
4747 CacheMetrics
48- Ttl time.Duration
49- Upstream http.Handler
50- Cache Cacher
51- Logger *slog.Logger
52- IgnoreRoutes []string
48+ Ttl time.Duration
49+ Upstream http.Handler
50+ Cache Cacher
51+ Logger *slog.Logger
5352 }
5453
5554 func NewHttpCache(log *slog.Logger, upstream http.Handler) *HttpCache {
......@@ -73,14 +72,6 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7372 return
7473 }
7574
76- reqUri := r.URL.Path
77- for _, uri := range c.IgnoreRoutes {
78- if uri == reqUri {
79- c.Upstream.ServeHTTP(w, r)
80- return
81- }
82- }
83-
8475 cacheKey := c.GetCacheKey(r)
8576 log := c.Logger.With("cache_key", cacheKey)
8677
......@@ -155,7 +146,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
155146 }
156147 cacheValue.Header[key] = values
157148 }
158- // Revalidation refreshes the entry — reset CreatedAt so it's fresh again.
149+ // Revalidation refreshes the entry -- reset CreatedAt so it's fresh again.
159150 cacheValue.CreatedAt = time.Now()
160151 enc, _ := json.Marshal(cacheValue)
161152 c.Cache.Remove(cacheKey)
......@@ -163,7 +154,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
163154 c.AddCacheItem(float64(len(enc)))
164155
165156 if clientConditional {
166- // Client sent conditional headers — re-evaluate against the
157+ // Client sent conditional headers -- re-evaluate against the
167158 // updated cached entry and return 304 if it still matches.
168159 r.Header.Set("If-None-Match", clientIfNoneMatch)
169160 r.Header.Set("If-Modified-Since", clientIfModifiedSince)
......@@ -184,7 +175,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
184175 }
185176 }
186177
187- // Client request was unconditional (or conditional but no longer matches) —
178+ // Client request was unconditional (or conditional but no longer matches)
188179 // serve the full cached response.
189180 serveCache(w, c.Ttl, cacheKey, &cacheValue)
190181 return