问题
I have been playing around with Goroutines, Channels and WaitGroup today and I am finally starting to understand the concept, after just been reading about it for a while.
My problem is that I am unsure how I handle errors when working like this, mainly because of the WaitGroup I use. When using the WaitGroup, I start by adding the amount of goroutines that will be executed, but what if an error happens during one of these?
package main
import (
"errors"
"sync"
)
var waitGroup sync.WaitGroup
func main() {
c := make(chan int, 10)
waitGroup.Add(10)
go doSomething(c)
waitGroup.Wait()
}
func doSomething(c chan int) {
for i := 0; i < 10; i++ {
n, err := someFunctionThatCanError()
if err != nil {
// How do I end the routines and WaitGroups here?
}
c <- n
waitGroup.Done()
}
close(c)
}
func someFunctionThatCanError() (int, error) {
return 1, errors.New("an error")
}
Playground: https://play.golang.org/p/ZLsBSqdMD49
I have tried my best to provide an example that shows what I am talking about. A loop will run 10 times in doSomething()
and it will call waitGroup.Done()
on every iteration, but what if an error happens during all this, like shown with someFunctionThatCanError()
?
When I try to solve it now, by returning and/or cancelling the channel, I end up with deadlocks, so I am a little unsure where to go from here. I am also unsure of how to handel the WaitGroup that I assume is waiting for more things to happen.
Any help is really appreciated.
回答1:
Use golang.org/x/sync/errgroup to wait on and handle errors from goroutines.
package main
import (
"errors"
"log"
"sync"
"golang.org/x/sync/errgroup"
)
var waitGroup sync.WaitGroup
func main() {
c := make(chan int, 10)
var g errgroup.Group
g.Go(func() error {
return doSomething(c)
})
// g.Wait waits for all goroutines to complete
// and returns the first non-nil error returned
// by one of the goroutines.
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
func doSomething(c chan int) error {
defer close(c)
for i := 0; i < 10; i++ {
n, err := someFunctionThatCanError()
if err != nil {
return err
}
c <- n
}
return nil
}
func someFunctionThatCanError() (int, error) {
return 1, errors.New("an error")
}
Run it on the playground.
回答2:
I have modified your example i don't know if it is a clean solution but it avoids deadlocks and such.
func doSomething(c chan int) {
defer close(c) // ensure channel is closed
for i := 0; i < 10; i++ {
n, err := someFunctionThatCanError(i)
if err != nil {
// calculate remaining waits and quickly release them
for j := i; j < 10; j++ {
waitGroup.Done()
}
return
}
c <- n
waitGroup.Done()
}
}
func someFunctionThatCanError(i int) (int, error) {
fmt.Println("running", i)
if i > 3 {
return 1, errors.New("an error")
}
return 1, nil
}
Here is the playground
来源:https://stackoverflow.com/questions/59095501/how-to-handle-errors-and-terminate-goroutine-using-waitgroup