Always implement proper cancellation mechanisms in concurrent code to prevent goroutine leaks and enable timely shutdown. Two key practices: 1. Add context checks at the beginning of loops in goroutines:
Always implement proper cancellation mechanisms in concurrent code to prevent goroutine leaks and enable timely shutdown. Two key practices:
for {
// Honor caller cancellation to avoid goroutine leaks
select {
case <-ctx.Done():
logger.Debug("operation cancelled")
return
default:
}
// Continue with operation that might block
data, err := ReadMessage(conn)
// ...
}
// After func (impl *transporterImpl) Send(ctx context.Context, m msg.Message) error ```
This pattern allows callers to cancel operations when a user exits manually or when operations exceed timeouts, preventing resource leaks and ensuring responsive application behavior during shutdown.
Enter the URL of a public GitHub repository