diff --git a/speed.go b/speed.go index 21dca7d..f77fae0 100644 --- a/speed.go +++ b/speed.go @@ -9,6 +9,7 @@ import ( "net/http" "strconv" "strings" + "sync" "sync/atomic" "time" ) @@ -184,8 +185,23 @@ func measureStream(ctx context.Context, client *http.Client, streams, seconds in defer cancel() var counter int64 + // JOIN the workers before returning (via wg) so the caller observes a SETTLED + // stream. This matters for the on-demand button's ok-aware workers (speedbutton.go): + // the upload worker issues ONE long streaming POST per goroutine that does NOT + // complete within the short window, so its 200/non-200/transport-error is recorded + // only once client.Do returns AFTER the window-end cancel. Without the join, + // onDemandSpeed read the upload tally while it was still empty and reported a bogus + // "no response and no transport error recorded" on EVERY run. Joining also closes a + // goroutine leak on the --speed path. Workers exit promptly on cancel (every loop is + // `for ctx.Err() == nil` and any in-flight request is aborted by the cancelled ctx), + // so the wait adds only the time for those in-flight requests to unwind. + var wg sync.WaitGroup + wg.Add(streams) for i := 0; i < streams; i++ { - go worker(ctx, client, url, &counter) + go func() { + defer wg.Done() + worker(ctx, client, url, &counter) + }() } sleepCtx(ctx, 1*time.Second) // warm-up (ramp to full rate); not sampled @@ -200,6 +216,7 @@ func measureStream(ctx context.Context, client *http.Client, streams, seconds in prev = cur } cancel() + wg.Wait() // join workers so a tally-recording worker has FINISHED before we return avg, min, max, _ = speedStats(samples) return avg, min, max, samples } diff --git a/speed_test.go b/speed_test.go index 4585d14..35c34b7 100644 --- a/speed_test.go +++ b/speed_test.go @@ -1,10 +1,40 @@ package main import ( + "context" "math" + "net/http" + "sync/atomic" "testing" + "time" ) +// TestMeasureStreamJoinsWorkers is the root-cause regression for the on-demand speed +// button: measureStream used to return the instant the window closed, WITHOUT waiting +// for its worker goroutines to finish. The button's ok-aware upload worker records its +// outcome only when its long streaming request returns — which is AFTER the window-end +// cancel — so onDemandSpeed read the tally while it was still empty and reported a bogus +// failure on EVERY run. This test proves measureStream now JOINS its workers: a worker +// that records only after it observes ctx cancellation MUST have recorded by the time +// measureStream returns. +func TestMeasureStreamJoinsWorkers(t *testing.T) { + var recorded int64 + // This worker mimics the real upload worker's shape: it blocks (as if mid-request) + // until the window-end cancel, then records its outcome — exactly the ordering that + // exposed the missing join. + worker := func(ctx context.Context, _ *http.Client, _ string, _ *int64) { + <-ctx.Done() // "request" unblocks only when the window closes + time.Sleep(10 * time.Millisecond) // a touch of unwind latency, as a real Do has + atomic.AddInt64(&recorded, 1) + } + // seconds=0 so the measurement window is just the 1s warm-up — keeps the test quick + // while still exercising the full cancel+join path. + measureStream(context.Background(), nil, 3, 0, worker, "http://unused.invalid/") + if got := atomic.LoadInt64(&recorded); got != 3 { + t.Fatalf("measureStream returned before its 3 workers finished: recorded=%d, want 3 (workers were not joined)", got) + } +} + func TestMbps(t *testing.T) { // 100 MB in 8 s = 100e6*8 bits / 8 s = 100 Mbit/s... check: bytes*8/1e6/sec if got := mbps(12_500_000, 1.0); math.Abs(got-100) > 0.01 { diff --git a/speedbutton.go b/speedbutton.go index 284bd04..bf2313a 100644 --- a/speedbutton.go +++ b/speedbutton.go @@ -224,10 +224,11 @@ func guardedSpeedClient(streams int) *http.Client { // The ok-aware wrappers below flip these so a 429-throughout run reports ok:false + // "rate-limited (HTTP 429)", never a bogus measured 0. type speedTally struct { - ok200 int64 // count of 200 responses that yielded body bytes - non200 int64 // count of non-200 responses (429/403/…) - lastCode int64 // last non-200 status observed - errCount int64 // count of client.Do/transport errors (request never saw an HTTP status) + ok200 int64 // count of 200 responses that yielded body bytes + non200 int64 // count of non-200 responses (429/403/…) + lastCode int64 // last non-200 status observed + errCount int64 // count of client.Do/transport errors (request never saw an HTTP status) + retryAfter int64 // 1 if any non-200 carried a Retry-After header (explicit back-off ask) // lastErr holds the text of the most recent transport error (a string, so NOT // atomic-safe like the counters). Multiple stream goroutines write it, so it is @@ -261,13 +262,21 @@ func (st *speedTally) download(ctx context.Context, client *http.Client, url str } resp, err := client.Do(req) if err != nil { - st.recordErr(err) + // A window-end cancellation (ctx done) is NORMAL termination, not a server + // failure — never record it, or a healthy stream whose in-flight request is + // aborted at the window edge would be mislabeled a transport error. + if ctx.Err() == nil { + st.recordErr(err) + } sleepCtx(ctx, 100*time.Millisecond) continue } if resp.StatusCode != http.StatusOK { atomic.AddInt64(&st.non200, 1) atomic.StoreInt64(&st.lastCode, int64(resp.StatusCode)) + if resp.Header.Get("Retry-After") != "" { + atomic.StoreInt64(&st.retryAfter, 1) + } io.Copy(io.Discard, resp.Body) resp.Body.Close() sleepCtx(ctx, 250*time.Millisecond) @@ -314,13 +323,24 @@ func (st *speedTally) upload(ctx context.Context, client *http.Client, url strin // rate-limited), never as "unreachable". resp, err := client.Do(req) if err != nil { - st.recordErr(err) + // A window-end cancellation (ctx done) is NORMAL termination for the long + // streaming upload, not a server failure — never record it. The whole reason + // the button's upload leg looked broken was that this ONE streaming POST never + // completes a 200 inside the short window; it is torn down by the window-end + // cancel, and that benign error must not pollute the tally (onDemandSpeed keys + // upload success off MEASURED throughput, see speedLegErr). + if ctx.Err() == nil { + st.recordErr(err) + } sleepCtx(ctx, 100*time.Millisecond) continue } if resp.StatusCode != http.StatusOK { atomic.AddInt64(&st.non200, 1) atomic.StoreInt64(&st.lastCode, int64(resp.StatusCode)) + if resp.Header.Get("Retry-After") != "" { + atomic.StoreInt64(&st.retryAfter, 1) + } } else { atomic.AddInt64(&st.ok200, 1) } @@ -372,10 +392,21 @@ func tallyErr(st *speedTally, what string) string { } if atomic.LoadInt64(&st.non200) > 0 { code := atomic.LoadInt64(&st.lastCode) - if code == http.StatusTooManyRequests { - return what + ": rate-limited by server (HTTP 429)" + retryAfter := atomic.LoadInt64(&st.retryAfter) > 0 + switch { + case code == http.StatusTooManyRequests: + // The signature rate-limit — name it explicitly so the panel tells the user + // WHY the run failed and that retrying later is the remedy. + return what + ": rate-limited by server (HTTP 429) — try again in a bit" + case code == http.StatusServiceUnavailable && retryAfter: + // 503 + Retry-After is an explicit "I'm overloaded, back off" — a rate-limit + // in spirit, so it gets the same try-again framing, distinct from a bare 503. + return what + ": server busy / rate-limited (HTTP 503) — try again in a bit" + case code >= 500: + return fmt.Sprintf("%s: server error (HTTP %d)", what, code) + default: + return fmt.Sprintf("%s: server returned HTTP %d", what, code) } - return fmt.Sprintf("%s: server returned HTTP %d", what, code) } if atomic.LoadInt64(&st.errCount) > 0 { st.mu.Lock() @@ -395,6 +426,23 @@ func tallyErr(st *speedTally, what string) string { return what + ": no data received (no response and no transport error recorded)" } +// speedLegErr decides one leg's (download/upload) honest outcome, folding in the +// MEASURED throughput. The UPLOAD leg's single long streaming POST does NOT complete a +// full 200 inside the short reduced-load window — it is torn down by the window-end +// cancel — so "did we see a 200?" is the WRONG success test for upload (it is why the +// button's upload leg always reported failure). The right test is: did real bytes flow +// AND did the server not reject us? If so, it is a legitimate measurement, window-edge +// cancellation notwithstanding. Any server rejection (non-200: 429/5xx/…) still wins, so +// a rate-limit is surfaced even when a little data slipped through. With no throughput +// and no clean 200, fall back to tallyErr's evidence-first classification (HTTP status, +// then transport-error cause). PURE given the tally + a measured number. +func speedLegErr(st *speedTally, measuredMbps float64, what string) string { + if measuredMbps > 0 && atomic.LoadInt64(&st.non200) == 0 { + return "" // real throughput, no server rejection => success + } + return tallyErr(st, what) +} + // onDemandSpeed is the production on-demand runner (the speedRunner seam default) and // the SOLE on-demand Cloudflare caller. It copies cfg, applies the reduced-load // profile, selects the plain (Cloudflare) or guarded (custom) client, threads ctx @@ -448,11 +496,11 @@ func onDemandSpeed(ctx context.Context, cfg Config, custom customTarget) OnDeman return OnDemandSpeedResult{Result: r, Target: target, OK: false, Err: "speed test aborted (timed out or cancelled)"} } - if msg := tallyErr(&dt, "download"); msg != "" { + if msg := speedLegErr(&dt, r.DownloadMbps, "download"); msg != "" { return OnDemandSpeedResult{Result: r, Target: target, OK: false, Err: msg} } if doUpload { - if msg := tallyErr(&ut, "upload"); msg != "" { + if msg := speedLegErr(&ut, r.UploadMbps, "upload"); msg != "" { return OnDemandSpeedResult{Result: r, Target: target, OK: false, Err: msg} } } diff --git a/speedbutton_test.go b/speedbutton_test.go index 77afd48..47e468c 100644 --- a/speedbutton_test.go +++ b/speedbutton_test.go @@ -48,6 +48,14 @@ func TestValidateSpeedTarget(t *testing.T) { // A non-literal hostname that would REBIND is accepted here (pre-screen only) — // the dial-time guard refuses it. Prove the pure screen passes a plain name. {"hostname accepted (dial-time gates rebind)", "https://totally-legit.example/path", false}, + // The DEFAULT Cloudflare targets (DefaultConfig) MUST pass the pre-screen: these + // are exactly the URLs the button's default run and the documented custom examples + // use, so a regression that rejected them would make every default run fail at the + // validator. (The default run does not even call validateSpeedTarget — only custom + // mode does — but pinning these keeps the allowlist honest for the custom path too.) + {"cloudflare default down", "https://speed.cloudflare.com/__down?bytes=50000000", false}, + {"cloudflare default up", "https://speed.cloudflare.com/__up", false}, + {"cloudflare default latency", "https://speed.cloudflare.com/__down?bytes=0", false}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { @@ -227,15 +235,17 @@ func TestTallyErrClassification(t *testing.T) { } } - // (c) a non-200 wins over any recorded error: the HTTP-code message. + // (c) a non-200 wins over any recorded error: the HTTP-code message. A 429 is named + // as a rate-limit with a try-again cue (the panel must say WHY, per the error- + // visibility requirement), not a bare status number. { var st speedTally st.recordErr(errors.New("connection reset by peer")) // also present, but status wins atomic.AddInt64(&st.non200, 1) atomic.StoreInt64(&st.lastCode, int64(http.StatusTooManyRequests)) msg := tallyErr(&st, "upload") - if !strings.Contains(msg, "429") { - t.Errorf("non200 429: want HTTP 429 message, got %q", msg) + if !strings.Contains(msg, "429") || !strings.Contains(msg, "rate-limited") || !strings.Contains(msg, "try again") { + t.Errorf("non200 429: want rate-limited/try-again/429 message, got %q", msg) } } { @@ -247,6 +257,39 @@ func TestTallyErrClassification(t *testing.T) { t.Errorf("non200 403: want HTTP 403 message, got %q", msg) } } + // (c') 503 WITH a Retry-After header => treated as an explicit back-off / rate-limit + // (server busy), distinct from a bare 503 which is a generic server error. + { + var st speedTally + atomic.AddInt64(&st.non200, 1) + atomic.StoreInt64(&st.lastCode, int64(http.StatusServiceUnavailable)) + atomic.StoreInt64(&st.retryAfter, 1) + msg := tallyErr(&st, "download") + if !strings.Contains(msg, "503") || !strings.Contains(msg, "rate-limited") || !strings.Contains(msg, "try again") { + t.Errorf("503+Retry-After: want rate-limited/try-again/503 message, got %q", msg) + } + } + { + var st speedTally // bare 503 (no Retry-After) => generic server error, NOT rate-limit framing + atomic.AddInt64(&st.non200, 1) + atomic.StoreInt64(&st.lastCode, int64(http.StatusServiceUnavailable)) + msg := tallyErr(&st, "download") + if !strings.Contains(msg, "503") || !strings.Contains(msg, "server error") { + t.Errorf("bare 503: want generic server-error message, got %q", msg) + } + if strings.Contains(msg, "try again") { + t.Errorf("bare 503 must not use the rate-limit try-again framing: %q", msg) + } + } + { + var st speedTally // a generic 5xx surfaces as a server error with the code + atomic.AddInt64(&st.non200, 1) + atomic.StoreInt64(&st.lastCode, int64(http.StatusBadGateway)) + msg := tallyErr(&st, "download") + if !strings.Contains(msg, "502") || !strings.Contains(msg, "server error") { + t.Errorf("502: want server-error message with code, got %q", msg) + } + } // (d) at least one 200 => success (""). { @@ -268,6 +311,64 @@ func TestTallyErrClassification(t *testing.T) { } } +// TestSpeedLegErr pins the per-leg outcome that fixes the button: the UPLOAD leg's one +// long streaming POST never completes a full 200 inside the short window, so success +// MUST key off MEASURED throughput, not "saw a 200". A leg that pushed real bytes with +// NO server rejection is a success even with zero recorded 200s; a server rejection +// (429/5xx) still wins and is surfaced; no throughput + no clean response falls back to +// the honest transport-error classification. +func TestSpeedLegErr(t *testing.T) { + // (a) THE REGRESSION: bytes flowed (mbps>0), but the streaming request never + // recorded a 200/non-200/error (it was torn down at the window edge). Pre-fix this + // returned the bogus "no response ..." fallback; it MUST now be success. + { + var st speedTally // empty: no ok200, no non200, no errCount + if msg := speedLegErr(&st, 30.7, "upload"); msg != "" { + t.Errorf("measured upload with empty tally must be success, got %q", msg) + } + } + // (b) a measured leg that ALSO saw a server rejection surfaces the rejection (a + // rate-limit must not be masked just because a little data slipped through). + { + var st speedTally + atomic.AddInt64(&st.non200, 1) + atomic.StoreInt64(&st.lastCode, int64(http.StatusTooManyRequests)) + msg := speedLegErr(&st, 12.0, "upload") + if !strings.Contains(msg, "429") { + t.Errorf("measured leg with a 429 must surface the rate-limit, got %q", msg) + } + } + // (c) zero throughput + a connect failure => unreachable (unchanged honesty). + { + var st speedTally + st.recordErr(errors.New("dial tcp 1.2.3.4:443: connect: connection refused")) + msg := speedLegErr(&st, 0, "upload") + if !strings.Contains(msg, "unreachable") { + t.Errorf("no throughput + connect failure => unreachable, got %q", msg) + } + } + // (d) a clean 200 with zero measured mbps (tiny/odd rounding) is still success. + { + var st speedTally + atomic.AddInt64(&st.ok200, 1) + if msg := speedLegErr(&st, 0, "download"); msg != "" { + t.Errorf("a clean 200 must be success even at 0 mbps, got %q", msg) + } + } +} + +// TestDefaultConfigSpeedTargetsValidate proves the shipped DefaultConfig speed URLs all +// pass the SSRF pre-screen — a guard against a future config/allowlist drift that would +// make the default button run fail at validation. +func TestDefaultConfigSpeedTargetsValidate(t *testing.T) { + c := DefaultConfig() + for _, u := range []string{c.SpeedDownURL, c.SpeedUpURL, c.SpeedLatencyURL} { + if _, err := validateSpeedTarget(u); err != nil { + t.Errorf("DefaultConfig speed URL %q must pass validateSpeedTarget, got %v", u, err) + } + } +} + func TestSpeedResultJSONShape(t *testing.T) { ok := OnDemandSpeedResult{ Result: SpeedResult{DownloadMbps: 123.4, UploadMbps: 45.6, LatencyMs: 12.3, JitterMs: 1.2},