diff --git a/go.mod b/go.mod index 1ac2b09..88706c8 100644 --- a/go.mod +++ b/go.mod @@ -92,8 +92,8 @@ require ( golang.org/x/sys v0.28.0 // indirect golang.org/x/text v0.21.0 // indirect golang.org/x/time v0.8.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20241223144023-3abc09e42ca8 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20241230172942-26aa7a208def // indirect google.golang.org/grpc v1.69.2 // indirect google.golang.org/protobuf v1.36.1 // indirect ) diff --git a/go.sum b/go.sum index 1ae9379..34aac58 100644 --- a/go.sum +++ b/go.sum @@ -138,8 +138,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g= go.ntppool.org/api v0.3.4 h1:KeRyFhIRkjJwZif7hkpqEDEBmukyYGiOi2Fd6j3UzQ0= go.ntppool.org/api v0.3.4/go.mod h1:LFLAwnrc/JyjzKnjgf8tCOJhps6oFIjuledS3PCx7xc= -go.ntppool.org/common v0.3.0 h1:IuSmyjEhI1F3tr5kc5MqlR4cy5y0o5f3EKvC7Koc6rs= -go.ntppool.org/common v0.3.0/go.mod h1:25pUt3YUusF1MY0nsljjskcMMeTvKZszVvNsubvWhSM= go.ntppool.org/common v0.3.1 h1:JaJpS3m8oAc9jH0yhHYJnjOC+RUzxx/F+EDe0QON4eQ= go.ntppool.org/common v0.3.1/go.mod h1:1SKjFBH9AL7Fj2S0zy41isHzV6dTC+6yIKD5QDtX8aY= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= @@ -248,10 +246,10 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/api v0.0.0-20241223144023-3abc09e42ca8 h1:st3LcW/BPi75W4q1jJTEor/QWwbNlPlDG0JTn6XhZu0= -google.golang.org/genproto/googleapis/api v0.0.0-20241223144023-3abc09e42ca8/go.mod h1:klhJGKFyG8Tn50enBn7gizg4nXGXJ+jqEREdCWaPcV4= -google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 h1:TqExAhdPaB60Ux47Cn0oLV07rGnxZzIsaRhQaqS666A= -google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA= +google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def h1:0Km0hi+g2KXbXL0+riZzSCKz23f4MmwicuEb00JeonI= +google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def/go.mod h1:u2DoMSpCXjrzqLdobRccQMc9wrnMAJ1DLng0a2yqM2Q= +google.golang.org/genproto/googleapis/rpc v0.0.0-20241230172942-26aa7a208def h1:4P81qv5JXI/sDNae2ClVx88cgDDA6DPilADkG9tYKz8= +google.golang.org/genproto/googleapis/rpc v0.0.0-20241230172942-26aa7a208def/go.mod h1:bdAgzvd4kFrpykc5/AC2eLUiegK9T/qxZHD4hXYf/ho= google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU= google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= diff --git a/server/history.go b/server/history.go index 67c7c5b..6c8bd61 100644 --- a/server/history.go +++ b/server/history.go @@ -145,7 +145,8 @@ func (srv *Server) history(c echo.Context) error { ctx, span := tracing.Tracer().Start(c.Request().Context(), "history") defer span.End() - // just cache for a short time by default + // set a reasonable default cache time; adjusted later for + // happy path common responses c.Response().Header().Set("Cache-Control", "public,max-age=240") mode := paramHistoryMode(c.Param("mode")) @@ -316,13 +317,7 @@ func (srv *Server) historyJSON(ctx context.Context, c echo.Context, server ntpdb } } - if len(history.LogScores) == 0 || - history.LogScores[len(history.LogScores)-1].Ts.After(time.Now().Add(-8*time.Hour)) { - // cache for longer if data hasn't updated for a while - c.Request().Header.Set("Cache-Control", "s-maxage=3600,max-age=1800") - } else { - c.Request().Header.Set("Cache-Control", "s-maxage=300,max-age=240") - } + setHistoryCacheControl(c, history) return c.JSON(http.StatusOK, res) } @@ -390,9 +385,26 @@ func (srv *Server) historyCSV(ctx context.Context, c echo.Context, history *logs // log.Info("entries", "count", len(history.LogScores), "out_bytes", b.Len()) - c.Response().Header().Set("Cache-Control", "s-maxage=150,max-age=120") + setHistoryCacheControl(c, history) + c.Response().Header().Set("Content-Disposition", "inline") // Chrome and Firefox force-download text/csv files, so use text/plain // https://bugs.chromium.org/p/chromium/issues/detail?id=152911 return c.Blob(http.StatusOK, "text/plain", b.Bytes()) } + +func setHistoryCacheControl(c echo.Context, history *logscores.LogScoreHistory) { + hdr := c.Response().Header() + if len(history.LogScores) == 0 || + // cache for longer if data hasn't updated for a while; or we didn't + // find any. + (time.Now().Add(-8 * time.Hour).After(history.LogScores[len(history.LogScores)-1].Ts)) { + hdr.Set("Cache-Control", "s-maxage=3600,max-age=1800") + } else { + if len(history.LogScores) == 1 { + hdr.Set("Cache-Control", "s-maxage=60,max-age=35") + } else { + hdr.Set("Cache-Control", "s-maxage=240,max-age=120") + } + } +}