Always propagate contexts consistently throughout the call chain and handle context cancellation properly in concurrent operations. When a context is available in the current scope, reuse it instead of creating a new background context. For operations that may block (like time.Sleep), use context-aware alternatives to prevent goroutine leaks when requests...
Always propagate contexts consistently throughout the call chain and handle context cancellation properly in concurrent operations. When a context is available in the current scope, reuse it instead of creating a new background context. For operations that may block (like time.Sleep), use context-aware alternatives to prevent goroutine leaks when requests are cancelled.
Example of proper context cancellation handling:
// Instead of:
time.Sleep(res.Delay)
// Use:
select {
case <-ctx.Done():
return Result{Ok: false}, nil
case <-time.After(res.Delay):
}
This ensures that goroutines don’t remain active after their associated request context has been cancelled, preventing resource leaks and improving application responsiveness.
Enter the URL of a public GitHub repository