英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论