News for hackers. Stuff that matters.
Re:Make resilient Go servers using timeouts, deadlines and c... by cpuguy83 on Sunday January 26, 2020 @12:27PM (#22153305)
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.
Re:Make resilient Go servers using timeouts, deadlines and c... by telendt on Sunday January 26, 2020 @04:45PM (#22155001)
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()
     ...
  }