Prolem: from one goroutine A I will spawn multiple goroutines B1, B1,… Bk and I want to:
- make A wait until all Bi have ended
- be able to cancel, from A, an individual Bi
option 1: have each Bi take a context.Context as parameter, and in a for loop do a bit of work while checking if context is done, and at the same time have every Bi take a sync.WaitGroup as input, and defer wg.Done()
My parent goroutine A will do wg.Wait() to wait for all Bi to end. How can I have A wait for (3 seconds, or somme other event like 95% of Bi finishing), see which of the Bi goroutines are still running and just close them ?
option 2: have all Bi take a context.Context, but also have them return a finished channel, each Bi defers close(finish)
I can have lots of flexibility in A, waiting for some condition the deliberately cancelling whatever Bi is still left.
How do oyu solve this problem, what is the idiomatic way to do this ?