go语言并发编程实战 Go语言中处理并发错误的常用方法总结 go 高并发编程

go语言并发编程实战 Go语言中处理并发错误的常用方法总结 go 高并发编程

目录
  • 一、 panic 只会触发当前 goroutine 中的 defer 操作
    • 1.1 示例代码
    • 1.2 代码说明
    • 1.3 正确处理
  • 二、多 goroutine 中收集错误和结局
    • 2.1 怎样批量收集错误信息?
    • 2.2 那如果也需要结局呢?
  • 三、 errgroup 包
    • 3.1 errgroup 包简介
    • 3.2 用 errgroup 包实战一下
    • 3.3 使用 errgroup 包中的 WithContext 技巧
  • 四、拓展资料

    一、 panic 只会触发当前 goroutine 中的 defer 操作

    很多开发者初次接触 Go 时容易误解 panic 的影响范围。下面我们先来看一个错误的代码示例:

    1.1 示例代码

    package mainimport ( “fmt” “time”)func main() // 在主 goroutine 中设置 defer,用于捕获 panic // 注意:这个 defer 只能捕获发生在主 goroutine 中的 panic defer func() // recover() 只能捕获当前 goroutine 内的 panic, // 如果 panic 发生在其他 goroutine 中,该 defer 无法捕获 if e := recover(); e != nil fmt.Println(“捕获到 panic:”, e) } }() // 启动子 goroutine,演示 panic 的传播范围 go func() // 输出提示信息,表示子 goroutine 开始执行 fmt.Println(“子 goroutine 开始”) // 主动触发 panic,注意这里的 panic 发生在子 goroutine 内, // 因此主 goroutine 中的 defer 无法捕获该 panic panic(“Goroutine 发生 panic”) }() // 主 goroutine 等待一段时刻,确保子 goroutine 有足够时刻执行 time.Sleep(2 time.Second) // 输出主 goroutine 结束信息 fmt.Println(“主 goroutine 结束”)}

    运行这段代码,我们会发现,会直接报错了:

    子 goroutine 开始panic: Goroutine 发生 panicgoroutine 18 [running]:main.main.func2() ~/golang-tutorial/tt.go:25 +0x59created by main.main in goroutine 1 ~/golang-tutorial/tt.go:20 +0x3bexit status 2

    1.2 代码说明

    • 主 goroutine 中的 defer: 主函数开始时设置了一个 defer 函数,目的是在发生 panic 时捕获并打印错误信息。然而,由于 recover 只能捕获当前 goroutine 内的 panic,当子 goroutine 内发生 panic 时,这个 defer 不会生效。
    • 子 goroutine 中的 panic: 在子 goroutine 中调用 panic 后,由于没有设置独立的 recover 逻辑,该 goroutine 会直接崩溃,panic 信息不会传递到主 goroutine 中。 这样可以清楚地看到,即使主 goroutine 使用了 defer 进行错误捕获,也无法捕捉到其他 goroutine 中发生的 panic。
    • 延时等待: 主 goroutine 使用 time.Sleep 等待一定时刻,以确保子 goroutine 有机会执行并触发 panic,从而验证 panic 的影响范围。

    既然程序会直接崩溃,那么,怎样解决这个难题呢?

    1.3 正确处理

    我们只需要在子 goroutine 中使用 recover 就可以了:

    package mainimport ( “fmt” “time”)func main() defer func() if e := recover(); e != nil fmt.Println(“捕获到 panic:”, e) } }() go func() defer func() if e := recover(); e != nil fmt.Println(“子 goroutine 捕获到 panic:”, e) } }() fmt.Println(“子 goroutine 开始”) panic(“Goroutine 发生 panic”) }() time.Sleep(2 time.Second) fmt.Println(“主 goroutine 结束”)}

    运行以上代码,可以发现,打印出的结局为:

    子 goroutine 开始子 goroutine 捕获到 panic: Goroutine 发生 panic主 goroutine 结束

    这就说明:panic 只会触发当前 goroutine 内的 defer 操作,不能跨 goroutine 捕获或恢复其他 goroutine 中的 panic。

    二、多 goroutine 中收集错误和结局

    假设我们有个需求,需要同时使用多个 goroutine 通过 http.Get 去请求下面内容四个地址,其中只有 https://httpbin.org/get 能够正常响应,其余地址均为故意写错的地址:

    • https://httpbin1.org/get
    • https://httpbin.org/get
    • https://httpbin2.org/get
    • https://httpbin3.org/get

    2.1 怎样批量收集错误信息?

    在并发请求中,可以通过错误通道( error channel )来收集各个 goroutine 中发生的错误。例如:

    package mainimport ( “fmt” “net/http” “sync”)func main() urls := []string “https://httpbin1.org/get”, “https://httpbin.org/get”, “https://httpbin2.org/get”, “https://httpbin3.org/get”, } var wg sync.WaitGroup // 创建一个带缓冲的错误通道,大致为 URL 数量 errCh := make(chan error, len(urls)) // 遍历所有 URL,分别启动 goroutine 发起请求 for _, url := range urls wg.Add(1) go func(url string) defer wg.Done() // 保证 goroutine 结束时减少计数 resp, err := http.Get(url) if err != nil // 如果请求出错,将错误发送到错误通道中 errCh <- fmt.Errorf(“请求 %s 失败: %v”, url, err) return } defer resp.Body.Close() // 打印成功信息 fmt.Printf(“请求 %s 成功,情形码: %dn”, url, resp.StatusCode) }(url) } // 等待所有 goroutine 执行完毕 wg.Wait() // 关闭错误通道 close(errCh) // 遍历错误通道,输出所有错误信息 for err := range errCh fmt.Println(“错误信息:”, err) }}

    在这个示例中,我们通过一个 channel errCh 来存储每个 goroutine 产生的错误,待所有 goroutine 执行完毕后,再统一处理错误信息。

    2.2 那如果也需要结局呢?

    如果希望每个请求的结局和可能的错误信息,我们可以定义一个结构体,将请求的结局与错误信息封装在一起,再通过 channel 收集:

    package mainimport ( “fmt” “io” “net/http” “sync”)// Result 用于封装每个请求的结局和错误信息type Result struct URL string // 请求的 URL StatusCode int // 返回的 HTTP 情形码 Err error // 请求经过中发生的错误 Content []byte // 返回的内容}func main() urls := []string “https://httpbin1.org/get”, “https://httpbin.org/get”, “https://httpbin2.org/get”, “https://httpbin3.org/get”, } var wg sync.WaitGroup // 创建带缓冲的结局通道,大致为 URL 数量 resCh := make(chan Result, len(urls)) // 遍历 URL,启动 goroutine 进行请求 for _, url := range urls wg.Add(1) go func(url string) defer wg.Done() resp, err := http.Get(url) result := ResultURL: url} if err != nil // 将错误结局封装后发送到结局通道 result.Err = err } else defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) // 将成功的结局封装后发送到结局通道 result.StatusCode = resp.StatusCode result.Content = body } resCh <- result }(url) } // 等待所有 goroutine 执行完毕 wg.Wait() close(resCh) // 遍历结局通道,输出每个请求的结局和错误信息 for res := range resCh if res.Err != nil fmt.Printf(“请求 %s 失败: %vn”, res.URL, res.Err) } else fmt.Printf(“请求 %s 成功,情形码: %d, 内容: %s n”, res.URL, res.StatusCode, string(res.Content)) } }}

    在这个示例中,每个 goroutine 都会将自己的请求结局封装到 Result 结构体中,通过通道传递回来,最终我们可以一一对应地输出结局和错误信息。

    三、 errgroup 包

    3.1 errgroup 包简介

    golang.org/x/sync/errgroup 包提供了一个便捷的方式来管理一组 goroutine,并能统一收集它们产生的错误。该包的主要功能有:

    • 错误收集与聚合: 当多个 goroutine 发生错误时,errgroup 会返回第一个遇到的错误。
    • 自动等待: 调用 g.Wait() 可以等待所有启动的 goroutine 执行完毕。
    • 与 context 结合: 通过 WithContext 技巧,可以为所有 goroutine 传入相同的 context,从而实现统一的取消逻辑。

    这些特性使得 errgroup 在需要并发执行多个任务且统一管理错误时非常有用。

    3.2 用 errgroup 包实战一下

    下面内容示例演示了怎样使用 errgroup 包来并发请求多个 URL:

    package mainimport ( “fmt” “net/http” “golang.org/x/sync/errgroup”)func main() urls := []string “https://httpbin1.org/get”, “https://httpbin.org/get”, “https://httpbin2.org/get”, “https://httpbin3.org/get”, } // 定义一个存储结局的切片,与 errgroup 共同使用 results := make([]string, len(urls)) var g errgroup.Group // 遍历所有 URL,启动 goroutine 执行 HTTP 请求 for i, url := range urls i, url := i, url // 为了避免闭包引用同一个变量 g.Go(func() error fmt.Println(“开始请求:”, url) resp, err := http.Get(url) if err != nil return fmt.Errorf(“请求 %s 失败: %v”, url, err) } defer resp.Body.Close() results[i] = fmt.Sprintf(“请求 %s 成功,情形码: %d”, url, resp.StatusCode) return nil }) } // 等待所有 goroutine 执行完毕 if err := g.Wait(); err != nil fmt.Println(“发生错误:”, err) } // 输出所有请求成功的结局 for _, res := range results fmt.Println(res) }}

    通过运行上面的代码,可能会打印出类似下面内容内容:

    开始请求: https://httpbin3.org/get开始请求: https://httpbin2.org/get开始请求: https://httpbin1.org/get开始请求: https://httpbin.org/get发生错误: 请求 https://httpbin3.org/get 失败: Get “https://httpbin3.org/get”: dial tcp: lookup httpbin3.org: no such host请求 https://httpbin.org/get 成功,情形码: 200

    我们可以得出下面内容重要的重点拎出来说:Wait 会阻塞直至由上述 Go 技巧调用的所有函数都返回,然而,如果有错误的话,只会记录第一个非 nil 的错误,也就是说,如果有多个错误的情况下,不会收集所有的错误。

    并且,通过源码得知:当遇到第一个错误时,如果之前设定了 cancel 技巧,那么还会调用 cancel 技巧,那么,怎样创建带有 cancel 技巧的 errgroup.Group 呢?

    3.3 使用 errgroup 包中的 WithContext 技巧

    有时我们希望在某个 goroutine 发生错误时,能够通知其他正在执行的任务提前取消。这时可以使用 errgroup.WithContext 技巧。下面内容示例展示了怎样实现这一点:

    package mainimport ( “context” “fmt” “net/http” “golang.org/x/sync/errgroup”)func main() urls := []string “https://httpbin1.org/get”, “https://httpbin.org/get”, “https://httpbin2.org/get”, “https://httpbin3.org/get”, } // 使用 context.Background 创建基本上下文,并通过 WithContext 包装 errgroup ctx := context.Background() g, ctx := errgroup.WithContext(ctx) // 定义存储结局的切片 results := make([]string, len(urls)) // 遍历所有 URL,启动 goroutine 发起请求 for i, url := range urls i, url := i, url g.Go(func() error fmt.Println(“开始请求:”, url) // 在发起请求前,根据 context 判断是否取消 req, err := http.NewRequestWithContext(ctx, “GET”, url, nil) if err != nil return err } resp, err := http.DefaultClient.Do(req) if err != nil return fmt.Errorf(“请求 %s 失败: %v”, url, err) } defer resp.Body.Close() results[i] = fmt.Sprintf(“请求 %s 成功,情形码: %d”, url, resp.StatusCode) return nil }) } // 如果有任一任务返回错误,将自动取消所有依赖于 ctx 的请求 if err := g.Wait(); err != nil fmt.Println(“错误发生:”, err) } for _, res := range results fmt.Println(res) }}

    运行以上的代码,打印结局如下:

    开始请求: https://httpbin3.org/get开始请求: https://httpbin.org/get开始请求: https://httpbin2.org/get开始请求: https://httpbin1.org/get错误发生: 请求 https://httpbin1.org/get 失败: Get “https://httpbin1.org/get”: dial tcp: lookup httpbin1.org: no such host

    在这个示例中,我们使用 errgroup.WithContext 创建了一个共享的上下文 ctx,所有的 HTTP 请求都与此 context 绑定。一旦某个请求发生错误并返回,其他 goroutine 中绑定该 context 的请求会立即收到取消信号,从而实现整体任务的协同取消。

    四、拓展资料

    这篇文章小编将从下面内容多少方面详细介绍了在 Go 语言中怎样处理并发错误:

    • panic 和 defer: 通过示例说明 panic 只会触发当前 goroutine 内的 defer 操作,并展示了即使主 goroutine 设置了 defer,也无法捕获子 goroutine 内的 panic。
    • 并发中错误收集: 通过简单示例展示了怎样在多个 goroutine 中分别收集错误信息,以及怎样关联请求结局与错误信息。
    • errgroup 包的使用: 介绍了 errgroup 包的核心功能,展示了怎样用 errgroup 包简化并发错误处理,同时详细演示了 WithContext 技巧的使用场景和效果。

    通过这些示例和详细解释,希望大家在实际开发中能够更加自信地处理并发任务中的错误难题,从而编写出更加健壮和易维护的代码。

    以上就是Go语言中处理并发错误的常用技巧拓展资料的详细内容,更多关于Go并发错误处理的资料请关注风君子博客其它相关文章!

    无论兄弟们可能感兴趣的文章:

    • Go并发编程中的错误恢复机制与代码持续执行实例探索
    • go高并发时append技巧偶现错误解决分析
    • 详解Go多协程并发环境下的错误处理
    • Golang中错误处理机制详解
    • Golang错误处理:异常捕捉和恢复机制
    版权声明

    为您推荐