From 6441b4329d1baab177ec576bb60b29dc5122025c Mon Sep 17 00:00:00 2001 From: iRedTea Date: Tue, 13 Oct 2026 22:49:19 +0300 Subject: [PATCH] fix: don't let a slow browser link time out the relayed speed test --- internal/server/handlers_panel_stat.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/server/handlers_panel_stat.go b/internal/server/handlers_panel_stat.go index fc6cc17..4eeb3fb 100644 --- a/internal/server/handlers_panel_stat.go +++ b/internal/server/handlers_panel_stat.go @@ -27,6 +27,9 @@ const ( speedDownCap = 256 << 20 speedDownFor = 6 * time.Second speedUpCap = 64 << 20 + // speedRelayUpFor is how long the agent's upload is passed on to the + // browser: the agent's own window with slack. + speedRelayUpFor = 4 * time.Second ) // statCache holds the last sample per agent and serializes its refresh. @@ -364,7 +367,14 @@ func (s *Server) handleSpeedRelayUp(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } - _, _ = io.Copy(rl.upW, http.MaxBytesReader(w, r.Body, speedUpCap)) + body := http.MaxBytesReader(w, r.Body, speedUpCap) + // A proxy in front may hand over the whole upload at once: pass on only + // what the browser takes in the window, or the agent would wait for the + // rest to trickle through a slower link. + t := time.AfterFunc(speedRelayUpFor, func() { rl.upW.Close() }) + _, _ = io.Copy(rl.upW, body) + t.Stop() rl.upW.Close() + _, _ = io.Copy(io.Discard, body) w.WriteHeader(http.StatusNoContent) }