Junior — Senior
Managing timers and contexts in Go
livecode
Task condition
It is necessary to analyze the provided code and eliminate possible issues related to resource leaks or unfinished timers.
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
for i := 0; i < 10; i++ {
go doRequest(ctx)
}
time.Sleep(time.Second)
cancel()
time.Sleep(time.Second * 2)
}
func doRequest(ctx context.Context) {
select {
case <-time.After(time.Second * 2):
fmt.Println("timer timeout")
case <-ctx.Done():
fmt.Println("context cancelled")
}
}