Spawned goroutines ignore deadline
Review this Go function that fans out a per-shard query and waits for all results with a deadline.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 22 min
Mark a line and say what kind of problem it is.0 findings
1func queryAll(ctx context.Context, shards []Shard) ([]Row, error) {
2 out := make(chan Row, len(shards))
3 for _, s := range shards {
4 go func(s Shard) {
5 r, _ := s.Query(context.Background())
6 out <- r
7 }(s)
8 }
9 var rows []Row
10 for i := 0; i < len(shards); i++ {
11 select {
12 case r := <-out:
13 rows = append(rows, r)
14 case <-ctx.Done():
15 return nil, ctx.Err()
16 }
17 }
18 return rows, nil
19}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.