4 ms·
Probably best not to use `time.After`, because it indeed starts a timer that you have no control over unless you are waiting for the full time.
by cpuguy83 7y ago
Probably best not to use `time.After`, because it indeed starts a timer that you have no control over unless you are waiting for the full time.
- telendt 7y agoGood point. To those unaware, the time.After is equivalent to time.NewTimer(d).C, but "the underlying Timer is not recovered by the garbage collector until the timer fires" (quote from the doc). That slowAPICall function should look like: func slowAPICall(ctx context.Context) string { d := rand.Intn(5) t := time.NewTimer(time.Duration(d) * time.Second) defer t.Stop() ... }