如何使用Golang context实现协程取消_统一管理并发任务

Go 中 context 用于协程生命周期管理、取消信号传递、超时控制和请求值共享;需通过 WithCancel/WithTimeout/WithDeadline 构造并传播 context,goroutine 监听 Done() 响应,配合 sync.WaitGroup 确保任务彻底结束。

在 Go 中,context 是管理协程生命周期、传递取消信号、超时控制和跨协程共享请求范围值的核心机制。要实现协程取消和统一管理并发任务,关键在于正确构造和传播 context,并让所有参与的 goroutine 主动监听其 Done() 通道。

用 context.WithCancel 主动触发取消

当你需要手动控制一组任务的启停(比如用户点击“取消”按钮),应使用 context.WithCancel 创建可取消的上下文:

  • 父 context 调用 WithCancel 返回子 context 和 cancel 函数
  • 将子 context 传给所有子 goroutine,它们通过 ctx.Done() 监听取消信号
  • 主逻辑在适当时机调用 cancel(),所有监听该 context 的 goroutine 会同时收到通知

示例:启动 3 个任务,2 秒后统一取消

func main() {
  ctx, cancel := context.WithCancel(context.Background())
  defer cancel() // 防止泄漏

  for i := 0; i     go func(id int) {
      for {
        select {
        case           fmt.Printf("task %d working\n", id)
        case           fmt.Printf("task %d cancelled\n", id)
          return
        }
      }
    }(i)
  }

  time.Sleep(2 * time.Second)
  cancel() // 触发全部退出
  time.Sleep(500 * time.Millisecond) // 等待打印完成
}

用 context.WithTimeout 或 WithDeadline 实现自动超时

对有明确时限的任务(如 HTTP 请求、数据库查询),优先使用带超时的 context:

  • WithTimeout(parent, 5*time.Second):相对超时,从调用时刻起计时
  • WithDeadline(parent, time.Now().Add(5*time.Second)):绝对截止时间,更精确
  • 超时到达时,context 自动关闭 Done() 通道,无需手动调用 cancel

注意:超时 context 也支持提前手动 cancel,但一旦超时触发,再调 cancel 无副作用。

嵌套 context 实现分层取消与值传递

真实场景中,任务常有层级依赖(如主任务 → 子任务 → 子子任务)。可通过嵌套 context 实现精细控制:

  • 父任务创建 WithCancel context,传给子任务
  • 子任务再调用 WithValueWithTimeout 创建自己的子 context
  • 父 cancel 会级联关闭所有后代 context;子 cancel 不影响父及其他兄弟
  • WithValue 可安全传递只读请求数据(如 traceID、user ID),避免全局变量或额外参数

例如:主流程设 10s 总超时,其中某子任务最多允许 2s,可用 WithTimeout(ctx, 2*time.Second) 封装。

统一等待所有 goroutine 结束:使用 sync.WaitGroup + context

单纯 cancel context 并不等待 goroutine 退出。要确保资源清理完毕,需结合 sync.WaitGroup

  • 启动前 wg.Add(1),退出前 wg.Done()
  • 主 goroutine 调用 cancel() 后,用 wg.Wait() 阻塞直到全部完成
  • 务必确保每个 goroutine 在任意退出路径(包括正常结束、错误返回、context 取消)都调用 wg.Done()

这是实现“统一管理并发任务”的落地保障 —— 取消是信号,等待是收尾。