Goroutine Leak Prevention
The cardinal rule of Go concurrency: Never start a goroutine without knowing how and when it will stop.
pkg/net/query_timeout.go
func queryWithTimeout(ctx context.Context, target string) (string, error) {
ch := make(chan string, 1) // Buffered to prevent leak if timeout wins
go func() {
res := performNetworkIO(target)
ch <- res
}()
select {
case res := <-ch:
return res, nil
case <-ctx.Done():
return "", ctx.Err()
}
}Personal engineering field notes compiled from production systems. Patterns and benchmarks are updated as runtimes evolve.