没有取消传播的上下文

问题描述 投票:0回答:3

如何创建 Go 上下文的副本(如果愿意,可以是克隆),其中包含原始版本中存储的所有值,但不会在原始版本取消时被取消?

对我来说,这确实是一个有效的用例。假设我有一个 http 请求,并且在响应返回到客户端后,其上下文被取消,并且我需要在该请求结束时在一个单独的 goroutine 中运行一个异步任务,该 goroutine 很可能比父上下文的寿命更长。

func Handler(ctx context.Context) (interface{}, error) {
        result := doStuff(ctx)
        newContext := howDoICloneYou(ctx)
        go func() {
                doSomethingElse(newContext)
        }()
        return result
}

任何人都可以建议这应该如何完成吗?

当然,我可以跟踪可能放入上下文中的所有值,创建一个新的背景 ctx,然后迭代每个可能的值并复制...但这看起来很乏味,并且很难在大型代码库中管理.

go asynchronous goroutine cancellation
3个回答
22
投票

更新:Go 1.21 在上下文包中添加了 WithoutCancel


由于 context.Context 是一个接口,因此您可以简单地创建自己的永远不会取消的实现:

import (
    "context"
    "time"
)

type noCancel struct {
    ctx context.Context
}

func (c noCancel) Deadline() (time.Time, bool)       { return time.Time{}, false }
func (c noCancel) Done() <-chan struct{}             { return nil }
func (c noCancel) Err() error                        { return nil }
func (c noCancel) Value(key interface{}) interface{} { return c.ctx.Value(key) }

// WithoutCancel returns a context that is never canceled.
func WithoutCancel(ctx context.Context) context.Context {
    return noCancel{ctx: ctx}
}

9
投票

任何人都可以建议这应该如何完成吗?

是的。别这样做。

如果您需要不同的上下文,例如为您的异步后台任务创建一个new上下文。您传入的上下文和后台任务之一是不相关的,因此您不得尝试重用传入的上下文。

如果不相关的新上下文需要原始数据中的一些数据:复制您需要的内容并添加新内容。


7
投票

从 go 1.21 开始,此功能可直接在标准库中通过

context.WithoutCancel
:

使用
func WithoutCancel(parent Context) Context

WithoutCancel
返回父级取消时未取消的父级副本。返回的上下文不返回
Deadline
Err
,其
Done
通道为
nil
。在返回的上下文上调用
Cause
将返回
nil

© www.soinside.com 2019 - 2024. All rights reserved.