Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sample代码里的context传递正确的吗 #49

Open
musereborn opened this issue Feb 20, 2024 · 0 comments
Open

sample代码里的context传递正确的吗 #49

musereborn opened this issue Feb 20, 2024 · 0 comments

Comments

@musereborn
Copy link

// TransactionMiddleware filter gin invocation
// NOTE: when use gin,must set gin.ContextWithFallback true when gin version >= 1.8.1
func TransactionMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
xid := ctx.GetHeader(constant.XidKey)
if xid == "" {
xid = ctx.GetHeader(constant.XidKeyLowercase)
}

	if len(xid) == 0 {
		log.Errorf("Gin: header not contain header: %s, global transaction xid", constant.XidKey)
		ctx.AbortWithStatus(http.StatusBadRequest)
		return
	}

	newCtx := ctx.Request.Context()
	newCtx = tm.InitSeataContext(newCtx)
	tm.SetXID(newCtx, xid)
	ctx.Request = ctx.Request.WithContext(newCtx)

	log.Infof("global transaction xid is :%s", xid)
}

}

gin的中间件根据传递的xid新建context并且放到ctx.Request的上下文中
但是在示例代码中
func updateDataSuccessHandler(c *gin.Context) {
log.Infof("get tm updateData")
if err := updateDataSuccess(c); err != nil {
c.JSON(http.StatusBadRequest, "updateData failure")
return
}
c.JSON(http.StatusOK, "updateData ok")
}
传递的都是gin.Context, 不是应该传递c.Request.Context给处理函数呢,发现最后传给db驱动的也是gin.Context,为啥是这样呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant