Got this from go vet:
the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak
The code is as follows:
func foo(ctx context.Context) {
for ... some loop {
c, _ := context.WithTimeout(ctx, time.Second)
err = enc.RequestWithContext(c, "some", someRequest, &response)
if err != nil {
continue
}
if response.Code == -1 {
break
}
}
}
So what is the point of cancelling it here after request? Just to make vet
happy? That context is anyway already either finished or timed out.
func foo(ctx context.Context) {
for ... some loop {
c, cancel := context.WithTimeout(ctx, time.Second)
err = enc.RequestWithContext(c, "some", someRequest, &response)
cancel()
if err != nil {
continue
}
if response.Code == -1 {
break
}
}
}