Stop goroutines when a condition is met in one of them

Hi @Pisistrato,

Killing goroutines pre-emptively does not seem like a good idea to me. An operation that gets interrupted at an unpredictable point may cause data corruption or worse things.

In other words, the code that is to be interrupted should be aware of that interruption and should get a chance to clean up and leave a consistent state behind.

If a select case is a really long-running operation, try splitting the operation into independent steps with a determined outcome for each step (and maybe a rollback strategy). Then the select block can check more often for a stop signal while the code completes the given task incrementally.

And do some benchmarks first, to see if the code needs improvement at all. Premature optimization can be a waste of time and lead to code that is harder to read and maintain.

3 Likes