Skip to content

Commit

Permalink
Merge branch 'iss-2423-timeout-crash' into 'dev'
Browse files Browse the repository at this point in the history
Move the timeout middleware to the first.

See merge request cloudcare-tools/datakit!3233
  • Loading branch information
谭彪 committed Oct 18, 2024
2 parents e8e6620 + 7717116 commit f180eea
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions internal/httpapi/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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))
Expand All @@ -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)
}
Expand Down

0 comments on commit f180eea

Please sign in to comment.