Using WaitGroup and channels in Go

Issue

I’m working on a freight service using Go that call many webservices, but I’m not sure how can I implement the concurrency model. This aproach just worked, but sometimes it locks. I believe there are some issue with channels and WaitGroup. I realy need to use WaitGroups or only the channels are enouth to routines lock.

// Call carriers quote webservice
var wg sync.WaitGroup
error := make(chan error)
quote := make(chan []freight.Quote)
for _, c := range carriers {
    go c.Quote(&wg, obj, quote, error)
}
wg.Wait()

// Collect the results
quotes:= make([]freight.Quote, 0)
for i := 1; i < len(carriers); i++ {
    err := <-error
    quoteAws:= <-quote

    if err != nil {
        log.Println(err)
    }
    if quoteAws != nil {
        quotes= append(quotes, quoteAws...)
    }
}
close(error)
close(quote)

func (carrier CarrierA) Quote(wg *sync.WaitGroup, obj Volume, quotes chan []Quote, err chan error) 
{
    // Deal with waitgroup
    wg.Add(1)
    defer wg.Done()
    
    // Quote the freigth
    err <- nil
    quotes <- quotesResult
    return
}

Solution

Use slices to collect the errors and quotes. Use a wait group to wait for the goroutines to complete.

errs := make([]error, len(carriers))
quotes := make([]freight.Quote, len(carriers))
var wg sync.WaitGroup
for i, c := range carriers {
    wg.Add(1)
    go func(i int, c Carrier) {
        defer wg.Done()
        quotes[i], errs[i] = c.Quote(args)
    }(i, c)
}
wg.Wait()

Answered By – Bayta Darell

Answer Checked By – Robin (GoLangFix Admin)

Leave a Reply

Your email address will not be published.