ctx我应该在包的功能run参数中使用哪个?来自上层,还是 context.Background()?谢谢。hystrix.Dohystrix-goctxpackage mainimport( "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())}
2 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
如果您使用上下文,在我看来您应该使用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 使用您的上下文,添加截止日期/取消本身。
一只甜甜圈
TA贡献1836条经验 获得超5个赞
尽可能使用context.Context来自上层的参数作为参数。它允许端到端的机制来控制请求,调用者所要做的就是取消,或者在初始ctx时调用超时,它将适用于完整的请求路径。
传递的初始上下文可能取决于您的要求。如果您不确定最初要使用什么上下文,在您确定之前,context.TODO可能是一个不错的选择。
- 2 回答
- 0 关注
- 150 浏览
添加回答
举报
0/150
提交
取消
