sql 多重循环错误_进行多重错误管理

sql 多重循环错误

Error management in Go is always prone to debate and a recurrent topic in the annual survey about the biggest challenges developers are facing when using Go. However, when it comes to dealing with errors in a concurrent environment or combining multiple errors for the same goroutine, Go provides great packages that make the management of multiple errors easy. Let’s look at how to combine several errors generated by a single goroutine.

Go中的错误管理总是容易引起争议,并且在年度调查中经常出现有关开发人员在使用Go时面临的最大挑战的话题。 但是,当涉及在并发环境中处理错误或为同一个goroutine合并多个错误时,Go提供了出色的软件包,可轻松管理多个错误。 让我们看一下如何合并单个goroutine生成的多个错误。

一个goroutine,多个错误 (One goroutine, multiple errors)

Combining multiple errors into one can be use quite useful when you are working on a code that has a retrial policy, for instance. Here is a basic example where we need to gather generated errors:

例如,当您使用具有重试策略的代码时,将多个错误合并为一个错误可能会非常有用。 这是一个基本示例,我们需要收集生成的错误:

Image for post

This program reads and parses a CSV text, and displays the error found. It could be more convenient to group the errors to get a full report. To combine errors into one, we have a choice between two great packages:

该程序读取并解析CSV文本,并显示发现的错误。 将错误分组以获取完整报告可能更加方便。 要将错误合并为一个,我们可以选择两种出色的软件包:

Image for post

Then a report can be printed out:

然后可以打印出报告:

Image for post

The implementation here is similar, here is the output:

这里的实现是相似的,这里是输出:

Image for post

The errors are concatenated via semicolon without any other formatting.

错误通过分号连接,没有任何其他格式。

Regarding the performances of each package, here is a benchmark with the same program with a higher number of fails:

关于每个程序包的性能,以下是使用相同程序但失败次数较多的基准:

name                    time/op         alloc/op        allocs/op
HashiCorpMultiErrors-4 6.01µs ± 1% 6.78kB ± 0% 77.0 ± 0%
UberMultiErrors-4 9.26µs ± 1% 10.3kB ± 0% 126 ± 0%

The implementation of Uber is slightly slower and consumes more memory. However, this package has been designed to group errors together once gathered, rather than appending them every single time. When grouping the errors, the results are close, but the code is less elegant since it needs an extra step. Here are the new results:

Uber的实施速度稍慢,并且占用更多内存。 但是,此程序包旨在将错误汇总到一起,而不是每次都附加错误。 对错误进行分组时,结果是接近的,但是由于需要额外的步骤,因此代码不太美观。 以下是新结果:

name                    time/op         alloc/op        allocs/op
HashiCorpMultiErrors-4 6.01µs ± 1% 6.78kB ± 0% 77.0 ± 0%
UberMultiErrors-4 6.02µs ± 1% 7.06kB ± 0% 77.0 ± 0%

Both packages leverage the Go error interface with implementing the Error() string function in their custom implementation.

这两个软件包都通过Go error接口在其自定义实现中实现Error() string函数。

一个错误,多个goroutines (One error, multiple goroutines)

When dealing with multiple goroutines to perform a task, it is necessary to correctly manage results and error aggregation to ensure the correctness of your program.

处理多个goroutine以执行任务时,必须正确管理结果和错误汇总,以确保程序的正确性。

Let’s start with a program that use multiple goroutines to perform a series of actions; each one them last one second:

让我们从一个使用多个goroutine来执行一系列动作的程序开始。 每个人持续一秒钟:

Image for post

To illustrate the error propagation, the first action of the third goroutine will fail. Here is what is happening:

为了说明错误的传播,第三个goroutine的第一个动作将失败。 这是正在发生的事情:

Image for post

As expected, the program takes roughly three seconds since most of the goroutines need to go through three actions that take one second each:

正如预期的那样,该程序大约需要三秒钟,因为大多数goroutine需要执行三个动作,每个动作需要一秒钟:

go run .  0.30s user 0.19s system 14% cpu 3.274 total

However, we might want to make the goroutines dependent on each other and cancel them if one of them fails. The solution to avoid unnecessary work would be to add a context and, as soon a goroutine fails, it would cancel it:

但是,我们可能希望使goroutine相互依赖,并在其中一个失败时取消它们。 避免不必要的工作的解决方案是添加上下文,并且一旦goroutine失败,它将取消该上下文:

Image for post

This is exactly what errgroup is providing; an error and context propagation when working with a group of goroutines. Here is the new code using the package errgroup:

这正是errgroup提供的。 使用一组goroutine时发生错误和上下文传播。 这是使用errgroup包的新代码:

Image for post

The programs now run faster since it propagates the canceled context by the error:

程序现在运行得更快,因为它会通过错误传播已取消的上下文:

go run .  0.30s user 0.19s system 38% cpu 1.269 total

The other benefit provided by the package is that we do not need to worry anymore about the waiting group adding and marking goroutines as done. The package manages that for us, we just need to say when we are ready to wait for the end of the process.

该软件包提供的另一个好处是,我们不必再担心等待组将goroutine添加和标记为已完成。 该软件包为我们管理着这一点,我们只需要说出何时准备等待过程结束即可。

翻译自: https://medium.com/a-journey-with-go/go-multiple-errors-management-a67477628cf1

sql 多重循环错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值