From 7717116a3f5489f861eb52b318d558e50949ca95 Mon Sep 17 00:00:00 2001 From: zhangyi Date: Tue, 15 Oct 2024 11:56:54 +0800 Subject: [PATCH] Move the timeout middleware to the first. --- internal/httpapi/http.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/httpapi/http.go b/internal/httpapi/http.go index 30d7654bbc..7ac4d802c9 100644 --- a/internal/httpapi/http.go +++ b/internal/httpapi/http.go @@ -159,6 +159,7 @@ func setDKInfo(c *gin.Context) { c.Header("X-DataKit", fmt.Sprintf("%s/%s", datakit.Version, datakit.DatakitHostName)) } +// dkHTTPTimeout Caution: this middleware must be registered as the first one. func dkHTTPTimeout(du time.Duration) gin.HandlerFunc { return timeout.New( timeout.WithTimeout(du), @@ -181,6 +182,10 @@ func setupRouter(hs *httpServerConf) *gin.Engine { router := gin.New() + // Caution: timeout middleware MUST be registered as the first one, or may crash the process. + // DON'T CHANGE ITS ORDER! + router.Use(dkHTTPTimeout(hs.timeout)) + // use whitelist config if len(hs.apiConfig.PublicAPIs) != 0 { router.Use(apiWhiteListMiddleware(hs.apiConfig.PublicAPIs)) @@ -197,8 +202,6 @@ func setupRouter(hs *httpServerConf) *gin.Engine { router.Use(uhttp.CORSMiddlewareV2(hs.apiConfig.AllowedCORSOrigins)) - router.Use(dkHTTPTimeout(hs.timeout)) - if !hs.apiConfig.Disable404Page { router.NoRoute(page404) }