Skip to content

#go

2 field notes tagged #go. All notes.

Read the standard library before reaching for a package

I spent an afternoon evaluating rate limiter packages before someone pointed at golang.org/x/time/rate, which does exactly what I needed and is about two hundred lines I could actually read end to end.

This keeps happening. Go’s standard library and the x/ repos cover more than I expect, and the code is short enough to read rather than trust. Now the first step is a search there, and only then in the wider ecosystem.

The goroutine leak was a missing cancel

Memory climbing slowly over days, nothing obvious in the heap profile. It was in the goroutine profile instead: thousands of them parked on a channel receive that would never fire.

ctx, cancel := context.WithTimeout(parent, 2*time.Second)
defer cancel() // this line was the whole bug

WithTimeout allocates a timer and registers the child with its parent. Without cancel(), neither is released until the timeout fires — and if the parent outlives the process, effectively never. go vet catches the obvious cases; this one was behind a helper function, so it didn’t.