fix: timouts no longer rely on progress updates
@@ -248,39 +248,110 @@ fn stdin_text() !string {
248248 return lines.join('\n')
249249}
250250
251+// FetchOutcome is what a fetch hands back over its job's reply channel: the
252+// measurement of an answer that came whole, the failure text of one that did
253+// not
254+struct FetchOutcome {
255+ ms i64
256+ size int
257+ err string
258+}
259+
260+// FetchJob is one fetch for a fetcher thread: the request to make and the
261+// channel to answer on. the job crosses to the fetcher as a value: the
262+// arguments of a spawned call and heap objects handed across threads have
263+// been seen carrying another probe's values, so a fetch travels by copy and
264+// the fetcher spawns with no arguments at all
265+struct FetchJob {
266+ url string
267+ range bool
268+ limit i64
269+ deadline time.Time
270+ reply chan FetchOutcome
271+}
272+
273+// fetch_jobs is the queue the fetcher threads serve
274+__global fetch_jobs = chan FetchJob{cap: 256}
275+
251276// timed_get fetches a url and returns the milliseconds it took and the size
252277// of the answer. range asks for the first byte only, which measures latency.
253278// the timeout comes from the config, and it bounds the probe twice over: each
254279// read and write on its own, and the answer as a whole. the socket timeouts
255280// alone start over with every read, so a mirror that trickles its db a few
256-// bytes at a time would hold a speed probe for as long as it cared to; the
257-// deadline is looked at on each chunk that arrives, and an answer still
258-// arriving past it is the probe's failure
281+// bytes at a time would hold a speed probe for as long as it cared to. the
282+// request therefore runs on a fetcher thread and the wait on its answer is
283+// bounded by the deadline directly — a bound that holds even when no chunk
284+// arrives to fire on_progress, as from a mirror that falls silent. the
285+// callback stays as the fetcher's own early exit: the first chunk past the
286+// deadline unwinds it, so a stalled fetch does not linger past its read_timeout
259287fn timed_get(url string, range bool) !(i64, int) {
260288 limit := rank_timeout
261- deadline := time.now().add(limit * time.second)
289+ job := FetchJob{
290+ url: url
291+ range: range
292+ limit: limit
293+ deadline: time.now().add(limit * time.second)
294+ reply: chan FetchOutcome{cap: 1}
295+ }
296+ fetch_jobs <- job
297+ spawn fetch_worker()
298+ mut res := FetchOutcome{}
299+ mut timed := false
300+ select {
301+ res = <-job.reply {
302+ }
303+ // the timeout branch counts nanoseconds, the unit of time.Duration
304+ job.deadline - time.now() {
305+ timed = true
306+ }
307+ }
308+ if timed {
309+ return error('no whole answer within ${limit}s')
310+ }
311+ if res.err != '' {
312+ return error(res.err)
313+ }
314+ return res.ms, res.size
315+}
316+
317+// fetch_worker serves one queued fetch and dies. it takes no arguments on
318+// purpose — the job comes off the queue — and one fetcher per fetch keeps the
319+// pairing exact: every queued job has a thread waiting to pop it, whichever
320+// thread answers which
321+fn fetch_worker() {
322+ job := <-fetch_jobs or { return }
262323 mut req := http.Request{
263- url: url
264- read_timeout: limit * time.second
265- write_timeout: limit * time.second
266- on_progress: fn [deadline, limit] (_ &http.Request, _ []u8, _ u64) ! {
267- if time.now() > deadline {
268- return error('no whole answer within ${limit}s')
324+ url: job.url
325+ read_timeout: job.limit * time.second
326+ write_timeout: job.limit * time.second
327+ on_progress: fn [job] (_ &http.Request, _ []u8, _ u64) ! {
328+ if time.now() > job.deadline {
329+ return error('no whole answer within ${job.limit}s')
269330 }
270331 }
271332 }
272- if range {
273- req.header.add_custom('Range', 'bytes=0-0')!
333+ if job.range {
334+ req.header.add_custom('Range', 'bytes=0-0') or {
335+ job.reply <- FetchOutcome{ err: err.msg() }
336+ return
337+ }
274338 }
275339 start := time.now()
276- resp := req.do()!
340+ resp := req.do() or {
341+ job.reply <- FetchOutcome{ err: err.msg() }
342+ return
343+ }
277344 // an answer that needs no body — a 404, a 403 — is a mirror that does
278345 // not serve the repo db: a probe without body size is ranked on air
279346 // unless the status is turned into the probe's failure
280347 if resp.status_code < 200 || resp.status_code >= 300 {
281- return error('http ${resp.status_code} from ${url}')
348+ job.reply <- FetchOutcome{ err: 'http ${resp.status_code} from ${job.url}' }
349+ return
350+ }
351+ job.reply <- FetchOutcome{
352+ ms: (time.now() - start).milliseconds()
353+ size: resp.body.len
282354 }
283- return (time.now() - start).milliseconds(), resp.body.len
284355}
285356
286357fn probe_latency(m Mirror, spec RankOs, override string) Probe {
@@ -1,3 +1,4 @@
1+@[has_globals]
12module main
23
34import blip.vlib.blip.machine
@@ -198,14 +199,34 @@ fn test_probe_batch_empty() {
198199 assert out.len == 0
199200}
200201
201-// trickle_mirror answers every connection on the listener with a body of
202-// total bytes, sent one byte each pause: each read lands well inside the
203-// socket timeout while the whole answer takes total pauses. it ends when the
204-// listener closes, and a client that hangs up ends its own connection
205-fn trickle_mirror(mut l net.TcpListener, total int, pause time.Duration) {
202+// TrickleServer is one trickle url's listener and the body shape it serves,
203+// handed to its serving thread by channel
204+struct TrickleServer {
205+ listener &net.TcpListener
206+ total int
207+ pause time.Duration
208+}
209+
210+// the trickle server's spec crosses to its serving thread by channel as a
211+// value, and the thread spawns with no arguments, like the fetcher's jobs in
212+// mirrors.v: the arguments of a spawned call have been seen carrying another
213+// probe's values, and an answer that takes another probe's totals holds that
214+// probe's fetch past its deadline
215+__global server_specs = chan TrickleServer{cap: 16}
216+
217+// trickle_mirror_worker answers every connection on the listener with a body
218+// of total bytes, sent one byte each pause: each read lands well inside the
219+// socket timeout while the whole answer takes total pauses. connections are
220+// served one at a time — a probe makes one connection, and a client that
221+// hangs up ends its connection's answer at the next write, so the next
222+// probe's connection is accepted soon after. the loop ends when the listener
223+// closes
224+fn trickle_mirror_worker() {
225+ spec := <-server_specs or { return }
226+ mut l := spec.listener
206227 for {
207228 mut conn := l.accept() or { return }
208- spawn trickle_answer(mut conn, total, pause)
229+ trickle_answer(mut conn, spec.total, spec.pause)
209230 }
210231}
211232
@@ -229,7 +250,12 @@ fn trickle_answer(mut conn net.TcpConn, total int, pause time.Duration) {
229250fn trickle_url(total int, pause time.Duration) !(&net.TcpListener, string) {
230251 mut l := net.listen_tcp(.ip, '127.0.0.1:0')!
231252 port := l.addr()!.port()!
232- spawn trickle_mirror(mut l, total, pause)
253+ server_specs <- TrickleServer{
254+ listener: l
255+ total: total
256+ pause: pause
257+ }
258+ spawn trickle_mirror_worker()
233259 return l, 'http://127.0.0.1:${port}/core.db'
234260}
235261
@@ -256,6 +282,31 @@ fn test_timed_get_bounds_the_whole_answer() ! {
256282 }
257283}
258284
285+// a mirror that answers a byte, then falls silent past the deadline gives the
286+// probe neither a progress callback to check the clock with nor a socket
287+// timeout to end the wait — each byte lands just inside it. the deadline must
288+// be held by the wait on the answer itself, so the probe ends at the deadline
289+// and not when the next chunk finally arrives (some 1.8s here)
290+fn test_timed_get_holds_the_deadline_when_no_chunk_arrives() ! {
291+ saved := rank_timeout
292+ defer {
293+ rank_timeout = saved
294+ }
295+ rank_timeout = 1
296+ mut l, url := trickle_url(30, 900 * time.millisecond)!
297+ defer {
298+ l.close() or {}
299+ }
300+ for range in [false, true] {
301+ start := time.now()
302+ if _, bytes := timed_get(url, range) {
303+ assert false, 'a silent mirror passed the timeout with ${bytes} bytes (range ${range})'
304+ }
305+ took := time.now() - start
306+ assert took < 1500 * time.millisecond, 'the probe took ${took} (range ${range})'
307+ }
308+}
309+
259310// an answer that is whole within the timeout is measured as before
260311fn test_timed_get_measures_a_prompt_answer() ! {
261312 saved := rank_timeout