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

2.3.2 配置管理 配置文件热更新后问题 #29

Open
cyj19 opened this issue Dec 24, 2021 · 0 comments
Open

2.3.2 配置管理 配置文件热更新后问题 #29

cyj19 opened this issue Dec 24, 2021 · 0 comments

Comments

@cyj19
Copy link

cyj19 commented Dec 24, 2021

配置文件热更新后,对于某些已使用的地方是无效的
main.go

       //  对于以下变量即使global.ServerSetting发生变化也是不会更新的
	gin.SetMode(global.ServerSetting.RunMode)

	router := routers.NewRouter()
	s := &http.Server{
		Addr:           ":" + global.ServerSetting.HttpPort,
		Handler:        router,
		ReadTimeout:    global.ServerSetting.ReadTimeout,
		WriteTimeout:   global.ServerSetting.WriteTimeout,
		MaxHeaderBytes: 1 << 20,
	}
       ...

router.go

        ...
        //  因为超时中间件参数是值传递,所以即使global.AppSetting.DefaultContextTimeout发生改变,该中间件还是只会使用首次注册时的参数值
	r.Use(middleware.ContextTimeout(global.AppSetting.DefaultContextTimeout))

解决:
修改context_timeout.go
ContextTimeout的值传递改为引用传递

func ContextTimeout(d *time.Duration) gin.HandlerFunc {
	return func(c *gin.Context) {
		ctx, cancel := context.WithTimeout(c.Request.Context(), *d)
                defer cancel()

		c.Request = c.Request.WithContext(ctx)
		c.Next()
	}
}
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