Hacker News new | ask | show | jobs
by telendt 2335 days ago
Good 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()
     ...
  }