Parent context.Context vs gin.Contect.Request.Context vs gin.Context

huangapple go评论65阅读模式
英文:

Parent context.Context vs gin.Contect.Request.Context vs gin.Context

问题

在我们公司,我们有一个将父上下文传递给 HTTP 处理程序的模式。目前,我们将 parentContext 传递给处理程序中需要它们的方法,例如 db.Query(parentCtx...)。我的问题是,我们应该在这里使用 gin 的 Context 还是 gin 的 Request 上下文呢?

英文:

At my company we have a pattern of passing in parent context into our http handlers. Currently we are passing in parentContext into methods in the handler that need them db.Query(parentCtx...).
My question is should we be using the gin Context or the gin Request context here instead.

答案1

得分: 2

如果您想在连接关闭时终止处理,请使用Request.Context。根据Done的文档:

> 如果您希望在连接关闭时中止工作,应改用Request.Context().Done(),它始终返回nil(chan,将永远等待)。

英文:

If you want to terminate processing if the connection is closed, use the Request.Context. Based on the documentation for Done:

> Done always returns nil (chan which will wait forever), if you want to abort your work when the connection was closed you should use Request.Context().Done() instead.

huangapple
  • 本文由 发表于 2021年11月8日 10:57:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/69878225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定