终止具有 errgroup 的阻塞 goroutine

huangapple go评论70阅读模式
英文:

terminating blocking goroutines with errgroup

问题

我有两个在go routines中运行的任务。我正在使用errgroup。我不确定如何正确使用errgroup.WithContext。

在下面的代码中,task1返回错误,我希望在这种情况下终止task2(长时间运行)。请注意,这个示例中的time.sleep只是为了模拟我的问题。实际上,task1和task2正在做真正的工作,没有任何sleep调用。

package main

import (
	"context"
	"fmt"
	"golang.org/x/sync/errgroup"
	"time"
)

func task1(ctx context.Context) error {
	time.Sleep(5 * time.Second)
	fmt.Println("first finished, pretend error happened")
	return ctx.Err()
}

func task2(ctx context.Context) error {
	select {
	case <-ctx.Done():
		fmt.Println("task 1 is finished with error")
		return ctx.Err()
	default:
		fmt.Println("second started")
		time.Sleep(50 * time.Second)
		fmt.Println("second finished")
	}
	return nil
}

func test() (err error) {
	ctx := context.Background()
	g, gctx := errgroup.WithContext(ctx)

	g.Go(func() error {
		return task1(gctx)
	})

	g.Go(func() error {
		return task2(gctx)
	})

	err = g.Wait()
	if err != nil {
		fmt.Println("wait done")
	}

	return err
}

func main() {
	fmt.Println("main")
	err := test()
	if err != nil {
		fmt.Println("main err")
		fmt.Println(err.Error())
	}
}
英文:

I have two tasks that are running in go routines. I am using errgroup. I am not sure how to use the errgroup.WithContext correctly.

In the following code, task1 is returning the error and I would like to terminate task2 (long running) when that happens. Please note that in this example time.sleep is added just to simulate my problem. In reality task1 and task2 are doing real work and does not have any sleep call.

package main

import (
	&quot;context&quot;
	&quot;fmt&quot;
	&quot;golang.org/x/sync/errgroup&quot;
	&quot;time&quot;
)

func task1(ctx context.Context) error {
	time.Sleep(5 * time.Second)
	fmt.Println(&quot;first finished, pretend error happened&quot;)
	return ctx.Err()
}

func task2(ctx context.Context) error {
	select {
	case &lt;-ctx.Done():
		fmt.Println(&quot;task 1 is finished with error&quot;)
		return ctx.Err()
	default:
        fmt.Println(&quot;second started&quot;)
		time.Sleep(50 * time.Second)
		fmt.Println(&quot;second finished&quot;)
	}
	return nil
}

func test() (err error) {
	ctx := context.Background()
	g, gctx := errgroup.WithContext(ctx)

	g.Go(func() error {
		return task1(gctx)
	})

	g.Go(func() error {
		return task2(gctx)
	})

	err = g.Wait()
	if err != nil {
		fmt.Println(&quot;wait done&quot;)
	}

	return err
}

func main() {
	fmt.Println(&quot;main&quot;)
	err := test()
	if err != nil {
		fmt.Println(&quot;main err&quot;)
		fmt.Println(err.Error())
	}
}


答案1

得分: 2

这是要翻译的内容:

你的任务是正确处理context的取消,并且不要在select语句中使用time.Sleep

根据errgroup文档的说明:

WithContext返回一个新的Group和一个从ctx派生的关联Context。

派生的Context在传递给Go函数的第一次返回非nil错误或第一次Wait返回时被取消,以先发生的为准。

你确实在使用错误组,但是你的上下文处理需要重构。

这是你的任务2的重构版本:

func task2(ctx context.Context) error {
	errCh := make(chan bool)

	go func() {
		time.Sleep(50 * time.Second)
		errCh <- true
	}()

	select {
	case <-ctx.Done():
		return fmt.Errorf("context done: %w", ctx.Err())
	case <-errCh:
		return errors.New("task 2 failed")
	}
}

通过这样的select语句,你等待第一个通道发出信号。在这种情况下,它是上下文的过期,除非你将时间睡眠修改为更短。示例playground

英文:

It's up to your tasks to handle context cancellation properly and not time.Sleep inside a select.

As stated in errgroup documentation:
> WithContext returns a new Group and an associated Context derived from ctx.
>
> The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.

You are using error group right, but your context handling needs a refactor.

Here is a refacor of your task 2:

func task2(ctx context.Context) error {
	errCh := make(chan bool)

	go func() {
		time.Sleep(50 * time.Second)
		errCh &lt;- true
	}()

	select {
	case &lt;-ctx.Done():
		return fmt.Errorf(&quot;context done: %w&quot;, ctx.Err())
	case &lt;-errCh:
		return errors.New(&quot;task 2 failed&quot;)
	}
}

With such select, you wait for the first channel to emit. In this case, it is the context expiration, unless you modify time sleep to be lower. Example playground.

huangapple
  • 本文由 发表于 2022年9月26日 14:34:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/73850330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定