DEV Community

ilya mozerov
ilya mozerov

Posted on

A timeout that was the handshake, not the work

Mishe runs small checks on timers. One of them, the privacy scan, reads every article we have on dev.to, drafts included, and holds each one against a list of rules. It takes a few minutes and has a hard ceiling of 540 seconds. One night the ceiling was hit and the producer failed. The obvious reading was that the scan had grown too big. The real cause was smaller, and it sat in a place nobody had been looking.

What the timeout said

The run was about 300 sequential requests. Each one went through Python's standard HTTPS client with no shared state, so every request opened a brand-new TLS connection before it asked for anything. From this host, that setup cost 1 to 5 seconds per request. Three hundred requests at that cost is most of an hour, so the ceiling was not a surprise once the cost was visible. The scan was not slow at its work. It was slow at saying hello.

The timeout message only said "too long". It did not say why. The reason was in the per-request numbers, which nobody had printed.

The change

The fix is small. All the reads now go through one helper that keeps a single HTTPS connection open for the whole run. The error contract stays the same: an answer of 300 or more still raises an HTTP error with no retry, and a transport failure still raises a URL error that is retried. The callers did not change.

With one connection kept alive, a request answered in about 0.35 seconds. The first live run after the change finished green in 270 seconds. A later run on the new code took 212 seconds of its 540-second budget.

The index check had the same shape. Its run hit a 300-second ceiling because every read paid its own handshake. Three sequential reads now take about 1.5 seconds. The check reads its own counts back from the account and compares them with the public index page, and the run after the fix came back green in 28 seconds.

What it did not fix

Reusing a connection does not make requests free. dev.to answered one rate-limit response (429) during the probe, so the number of requests still matters. Keeping the connection open removed the waiting, but the scan still asks for every article every hour. If the account grows, the scan will get slower again, and the honest next step is to read fewer things, not to set a longer ceiling.

The lesson for readers

Three things, in the order they helped.

First, a timeout is a symptom, not a diagnosis. Before changing the work or the limit, print the cost of one unit of work and the cost of its setup. Here the setup was most of the time.

Second, the fix can be tested by timing the same request twice, once fresh and once on a kept connection. If the two numbers differ by an order of magnitude, the setup was the cause. If they do not, look elsewhere.

Third, keep the failure contract the same while you change the mechanism. The callers still see the same kinds of error, so the retry rules and the "unknown" verdicts behave as before. Only the time spent changed.

The check that caught this is public in the sense that its verdicts are: each run writes a signal that reads GREEN, RED or UNKNOWN, and a stale or failed run reads UNKNOWN rather than quietly green. That is what made a ceiling-hit visible at all.

Top comments (0)