From 2174b556a01596a2fb43f9b2308a53ae30e1290b Mon Sep 17 00:00:00 2001 From: diviocity Date: Tue, 10 Nov 2026 22:09:13 +0100 Subject: [PATCH] fix: timouts no longer rely on progress updates --- src/mirrors.v | 101 ++++++++++++++++++++++++++++++++++++++------- src/mirrors_test.v | 65 +++++++++++++++++++++++++---- 2 files changed, 144 insertions(+), 22 deletions(-) diff --git a/src/mirrors.v b/src/mirrors.v index c68f913..1158cdc 100644 --- a/src/mirrors.v +++ b/src/mirrors.v @@ -248,39 +248,110 @@ fn stdin_text() !string { return lines.join('\n') } +// FetchOutcome is what a fetch hands back over its job's reply channel: the +// measurement of an answer that came whole, the failure text of one that did +// not +struct FetchOutcome { + ms i64 + size int + err string +} + +// FetchJob is one fetch for a fetcher thread: the request to make and the +// channel to answer on. the job crosses to the fetcher as a value: the +// arguments of a spawned call and heap objects handed across threads have +// been seen carrying another probe's values, so a fetch travels by copy and +// the fetcher spawns with no arguments at all +struct FetchJob { + url string + range bool + limit i64 + deadline time.Time + reply chan FetchOutcome +} + +// fetch_jobs is the queue the fetcher threads serve +__global fetch_jobs = chan FetchJob{cap: 256} + // timed_get fetches a url and returns the milliseconds it took and the size // of the answer. range asks for the first byte only, which measures latency. // the timeout comes from the config, and it bounds the probe twice over: each // read and write on its own, and the answer as a whole. the socket timeouts // alone start over with every read, so a mirror that trickles its db a few -// bytes at a time would hold a speed probe for as long as it cared to; the -// deadline is looked at on each chunk that arrives, and an answer still -// arriving past it is the probe's failure +// bytes at a time would hold a speed probe for as long as it cared to. the +// request therefore runs on a fetcher thread and the wait on its answer is +// bounded by the deadline directly — a bound that holds even when no chunk +// arrives to fire on_progress, as from a mirror that falls silent. the +// callback stays as the fetcher's own early exit: the first chunk past the +// deadline unwinds it, so a stalled fetch does not linger past its read_timeout fn timed_get(url string, range bool) !(i64, int) { limit := rank_timeout - deadline := time.now().add(limit * time.second) + job := FetchJob{ + url: url + range: range + limit: limit + deadline: time.now().add(limit * time.second) + reply: chan FetchOutcome{cap: 1} + } + fetch_jobs <- job + spawn fetch_worker() + mut res := FetchOutcome{} + mut timed := false + select { + res = <-job.reply { + } + // the timeout branch counts nanoseconds, the unit of time.Duration + job.deadline - time.now() { + timed = true + } + } + if timed { + return error('no whole answer within ${limit}s') + } + if res.err != '' { + return error(res.err) + } + return res.ms, res.size +} + +// fetch_worker serves one queued fetch and dies. it takes no arguments on +// purpose — the job comes off the queue — and one fetcher per fetch keeps the +// pairing exact: every queued job has a thread waiting to pop it, whichever +// thread answers which +fn fetch_worker() { + job := <-fetch_jobs or { return } mut req := http.Request{ - url: url - read_timeout: limit * time.second - write_timeout: limit * time.second - on_progress: fn [deadline, limit] (_ &http.Request, _ []u8, _ u64) ! { - if time.now() > deadline { - return error('no whole answer within ${limit}s') + url: job.url + read_timeout: job.limit * time.second + write_timeout: job.limit * time.second + on_progress: fn [job] (_ &http.Request, _ []u8, _ u64) ! { + if time.now() > job.deadline { + return error('no whole answer within ${job.limit}s') } } } - if range { - req.header.add_custom('Range', 'bytes=0-0')! + if job.range { + req.header.add_custom('Range', 'bytes=0-0') or { + job.reply <- FetchOutcome{ err: err.msg() } + return + } } start := time.now() - resp := req.do()! + resp := req.do() or { + job.reply <- FetchOutcome{ err: err.msg() } + return + } // an answer that needs no body — a 404, a 403 — is a mirror that does // not serve the repo db: a probe without body size is ranked on air // unless the status is turned into the probe's failure if resp.status_code < 200 || resp.status_code >= 300 { - return error('http ${resp.status_code} from ${url}') + job.reply <- FetchOutcome{ err: 'http ${resp.status_code} from ${job.url}' } + return + } + job.reply <- FetchOutcome{ + ms: (time.now() - start).milliseconds() + size: resp.body.len } - return (time.now() - start).milliseconds(), resp.body.len } fn probe_latency(m Mirror, spec RankOs, override string) Probe { diff --git a/src/mirrors_test.v b/src/mirrors_test.v index e163d36..259189c 100644 --- a/src/mirrors_test.v +++ b/src/mirrors_test.v @@ -1,3 +1,4 @@ +@[has_globals] module main import blip.vlib.blip.machine @@ -198,14 +199,34 @@ fn test_probe_batch_empty() { assert out.len == 0 } -// trickle_mirror answers every connection on the listener with a body of -// total bytes, sent one byte each pause: each read lands well inside the -// socket timeout while the whole answer takes total pauses. it ends when the -// listener closes, and a client that hangs up ends its own connection -fn trickle_mirror(mut l net.TcpListener, total int, pause time.Duration) { +// TrickleServer is one trickle url's listener and the body shape it serves, +// handed to its serving thread by channel +struct TrickleServer { + listener &net.TcpListener + total int + pause time.Duration +} + +// the trickle server's spec crosses to its serving thread by channel as a +// value, and the thread spawns with no arguments, like the fetcher's jobs in +// mirrors.v: the arguments of a spawned call have been seen carrying another +// probe's values, and an answer that takes another probe's totals holds that +// probe's fetch past its deadline +__global server_specs = chan TrickleServer{cap: 16} + +// trickle_mirror_worker answers every connection on the listener with a body +// of total bytes, sent one byte each pause: each read lands well inside the +// socket timeout while the whole answer takes total pauses. connections are +// served one at a time — a probe makes one connection, and a client that +// hangs up ends its connection's answer at the next write, so the next +// probe's connection is accepted soon after. the loop ends when the listener +// closes +fn trickle_mirror_worker() { + spec := <-server_specs or { return } + mut l := spec.listener for { mut conn := l.accept() or { return } - spawn trickle_answer(mut conn, total, pause) + trickle_answer(mut conn, spec.total, spec.pause) } } @@ -229,7 +250,12 @@ fn trickle_answer(mut conn net.TcpConn, total int, pause time.Duration) { fn trickle_url(total int, pause time.Duration) !(&net.TcpListener, string) { mut l := net.listen_tcp(.ip, '127.0.0.1:0')! port := l.addr()!.port()! - spawn trickle_mirror(mut l, total, pause) + server_specs <- TrickleServer{ + listener: l + total: total + pause: pause + } + spawn trickle_mirror_worker() return l, 'http://127.0.0.1:${port}/core.db' } @@ -256,6 +282,31 @@ fn test_timed_get_bounds_the_whole_answer() ! { } } +// a mirror that answers a byte, then falls silent past the deadline gives the +// probe neither a progress callback to check the clock with nor a socket +// timeout to end the wait — each byte lands just inside it. the deadline must +// be held by the wait on the answer itself, so the probe ends at the deadline +// and not when the next chunk finally arrives (some 1.8s here) +fn test_timed_get_holds_the_deadline_when_no_chunk_arrives() ! { + saved := rank_timeout + defer { + rank_timeout = saved + } + rank_timeout = 1 + mut l, url := trickle_url(30, 900 * time.millisecond)! + defer { + l.close() or {} + } + for range in [false, true] { + start := time.now() + if _, bytes := timed_get(url, range) { + assert false, 'a silent mirror passed the timeout with ${bytes} bytes (range ${range})' + } + took := time.now() - start + assert took < 1500 * time.millisecond, 'the probe took ${took} (range ${range})' + } +} + // an answer that is whole within the timeout is measured as before fn test_timed_get_measures_a_prompt_answer() ! { saved := rank_timeout -- 2.56.1