如何使用 WaitGroup 处理错误和终止 Goroutine

How to handle errors and terminate Goroutine using WaitGroup

我今天一直在研究 Goroutines、Channels 和 WaitGroup,在阅读了一段时间之后,我终于开始理解这个概念了。

我的问题是我不确定在这样工作时如何处理错误,主要是因为我使用了 WaitGroup。使用 WaitGroup 时,我首先添加将要执行的 goroutine 的数量,但是如果其中一个发生错误怎么办?

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")
}

游乐场:https://play.golang.org/p/ZLsBSqdMD49

我已尽力提供一个例子来说明我在说什么。一个循环将在 doSomething() 中 运行 10 次,并且它会在每次迭代时调用 waitGroup.Done(),但是如果在所有这些过程中发生错误怎么办,如 someFunctionThatCanError() 所示?

当我现在尝试解决它时,通过返回 and/or 取消频道,我最终陷入僵局,所以我有点不确定从这里去哪里。我也不确定如何处理我认为正在等待更多事情发生的 WaitGroup。

非常感谢任何帮助。

我已经修改了你的例子,我不知道它是否是一个干净的解决方案,但它避免了死锁等问题。

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
}

这是playground

使用 golang.org/x/sync/errgroup 等待并处理来自 goroutine 的错误。

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.