Go traps · Chapter 16 of 16
An HTTP call with no deadline
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Defer
- 3 tasks
- Go
- In the all labs plan
The first 2 chapters of Go traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.go: a tariff service that never answers, on purpose
Write reproduce() in repro.go, which returns a string. Start an httptest.Server whose handler waits on a channel until reproduce lets it go, call FetchTariff(ctx, server.URL, kind) from tariff.go in a goroutine with a ctx whose deadline is 50 ms (context.WithTimeout), and wait for it at most 1 second with a select and time.After. Return "still waiting" if it has not returned by then, or the text of the error it returned (fmt.Sprint of the error). Let the handler go before the server's Close: Close waits for every request still being handled.
The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original tariff.go it must return "still waiting", and beside a fixed tariff.go it must return an error's text that contains context deadline exceeded. So it must really call FetchTariff against a server that does not answer. No other waiting and no input: reproduce() must finish within 3 seconds.
- Task 2
tariff.go: a hung tariff service cannot hold a Start for ever
Fix FetchTariff(ctx context.Context, base, kind string) (int, error) in tariff.go so it respects ctx. A good answer gives the price in paise a kWh and a nil error; when ctx's deadline passes before the server answers, it returns soon after with an error that errors.Is reports as context.DeadlineExceeded; with a ctx that is already cancelled it returns at once with an error that errors.Is reports as context.Canceled; a status other than 200 is an error. Keep the URL (base + "/tariffs/" + kind), the JSON field paise_per_kwh, the error texts, and StartCharging with its three lines.
The check uses its own httptest servers: one that answers, one that answers 500, and one that holds every request until the check lets it go, with an 80 ms deadline; every call must return within a second. A client Timeout alone is not a fix: the check's deadlines are far shorter than any sensible Timeout, and it cancels ctx too.
- Task 3
tariff_test.go: a test that would have caught it
Write a Go test file, tariff_test.go, that would have caught this bug before tonight's outage. The check runs your file three times, each in a fresh build: beside the original tariff.go, where at least one test must fail; beside the fixed tariff.go, where every test must pass; and beside a wrong fix that only gives the client a 10 second Timeout, where at least one test must fail.
So start an httptest.Server whose handler waits until the test lets it go, call FetchTariff in a goroutine with a ctx deadline of 50 ms, and fail when it has not returned within a second (a select with time.After) or returned an error that is not the deadline's. The test must give up on its own, and let the handler go before the server's Close: the whole file is stopped after 8 seconds. A second test with a server that answers, so a FetchTariff that always fails does not pass, is a good idea.
What you will learn
- repro.go: a tariff service that never answers, on purpose
- tariff.go: a hung tariff service cannot hold a Start for ever
- tariff_test.go: a test that would have caught it
More in Defer
Build "An HTTP call with no deadline" yourself
BijliGo runs its EV chargers on Go, and Joseph, its senior engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Go traps are free. This one comes with the all labs plan.