我应该在 hystrix.Do 的 hystrix-go 包的运行参数中使用哪个 ctx?

ctx我应该在包的功能run参数中使用哪个?来自上层,还是 context.Background()?谢谢。hystrix.Dohystrix-goctx


package main


import(

    "context"


    "github.com/myteksi/hystrix-go/hystrix"

)


func tb(ctx context.Context)error{

    return nil

}


func ta(ctx context.Context){

    hystrix.Do("cbName", func()error{

        // At this place, the ctx parameter of function tb,

        // Should I use ctx from ta function, or context.Background()?

        return tb(ctx)

    }, nil)

}



func main(){

    ta(context.Background())

}


摇曳的蔷薇
浏览 151回答 2
2回答

慕的地6264312

如果您使用上下文,在我看来您应该使用hystrix.DoC. 除了通过的任何上下文之外,没有理由使用任何东西,因为Do它是同步的,并且您希望在此代码中保留任何取消、截止日期(以及附加到您的上下文的任何其他内容)。func ta(ctx context.Context) {    err := hystrix.DoC(ctx, "cbName", func(ctx context.Context) error {       ... code that uses ctx here.    }, nil)    // handle err, which may be a hystrix error.}很难说这是否真的与 call 不同hystrix.Do,但这可能允许 hystrix 使用您的上下文,添加截止日期/取消本身。

一只甜甜圈

尽可能使用context.Context来自上层的参数作为参数。它允许端到端的机制来控制请求,调用者所要做的就是取消,或者在初始ctx时调用超时,它将适用于完整的请求路径。传递的初始上下文可能取决于您的要求。如果您不确定最初要使用什么上下文,在您确定之前,context.TODO可能是一个不错的选择。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go