I want to run multiple goroutines, make some processing, put a result into channel, and when there's at least one goroutine finished, finish all others and return result from channel.
So, I tried to implement this using wait group, but seems I haven't use wait group properly.
package optimizer
import (
"github.com/tevino/abool"
"errors"
"sync"
"runtime"
"log"
)
type Optimizer struct {
Handlers []ofdHandler.Handler
}
func Make(handlers []ofdHandler.Handler, maxProcs int) Optimizer {
runtime.GOMAXPROCS(maxProcs)
return Optimizer{Handlers: handlers}
}
func (o Optimizer) Optimize(params operations.GetV1ReceiptsParams) (*models.Receipt, error) {
var wg sync.WaitGroup
wg.Add(len(o.Handlers))
results := make(chan *models.Receipt)
isCalculated := abool.NewBool(false)
for _, handler := range o.Handlers {
go func(handler ofdHandler.Handler) {
log.Println("Starting handler: ", handler.GetName())
defer wg.Done()
if isCalculated.IsSet() {
log.Println("Result is calculated, exiting goroutine...")
return
}
receipt, err := handler.Handle(params)
if err != nil {
log.Println(err)
return
}
if isCalculated.IsSet() {
log.Println("Result is calculated, exiting goroutine...")
return
}
log.Println("Writing result to channel...")
isCalculated.Set()
results <- receipt
}(handler)
}
log.Println("Waiting...")
wg.Wait()
if receipt, ok := <-results; ok {
return receipt, nil
}
return nil, errors.New("couldn't optimize with current list of Handlers")
}