- With issues:
- Use the search tool before opening a new issue.
- Please provide source code and commit sha if you found a bug.
- Review existing issues and provide feedback or react to them.
Description
When I make a request to my backend, I see in the logs that every handler gets executed twice
How to reproduce
package main main.go
routes.API(r)
package routes api.go
api.GET("/client", cache.CachePage(storeCache, time.Hour*12, func(c *gin.Context) {
client.GET(c)
}))
package controllers client.go
func GET(c *gin.Context) {
client := &models.Client{}
migrate.DB.Where("id = ?", 1).Find(&client)
fmt.Println(client)
c.JSON(http.StatusOK, gin.H{"clientInfo": client})
}
Expectations
[GIN] 2020/07/13 - 07:22:59 | 200 | 1.485762ms | ::1 | GET /v1/client
Actual result
[GIN] 2020/07/13 - 07:22:59 | 200 | 1.485762ms | ::1 | GET /v1/client
[GIN] 2020/07/13 - 07:22:59 | 200 | 1.550383ms | ::1 | GET /v1/client
Environment
- go version: 1.13
- gin version (or commit ref): v1.5.0
- operating system: Linux
Comment From: thinkerou
you should post one which can reproduce mini demo code, thanks
Comment From: gangqiang01
I see the same thing handler executed twice when the request takes too long
Comment From: gervasiomatt
I see the same thing handler executed twice when the request takes too long
Same. On long-running requests, I'm able to observe my handler being executed twice.
Comment From: khanakia
I am having the same issue my first handler GIN has 307 status and then redirecting again with 200 Status
But the problem is the even gin is redirecting using 307 status somehow my handler is running twice.
Comment From: lareza-farhan-wanaghi
In case anyone has the same issue, it was the timeouts for my case (specifically the WriteTimeout, since my program writes a file for more than 5 seconds)
func (app *application) serve() error {
srv := &http.Server{
Addr: ":8080",
Handler: app.routes(),
IdleTimeout: 30 * time.Second,
ReadTimeout: 15 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
fmt.Printf("Starting HTTP server on port %s\n", srv.Addr)
return srv.ListenAndServe()
}
Comment From: swarajkumarsingh
The Perfect Solution 💕
Isuue: gin logger is logging the request info twice
Solution: Gin by default always uses its logger, and if you try to add logger again r.Use(gin.Logger()), then the logs are written twice because gin logger is called twice. So the solution is that you should not call gin.Logger is gin is already using it.
Solution in Code:
func main() {
r := gin.Default()
// ** r.Use(gin.Logger()) - don't call logs gin longer again**
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "health ok",
})
})
r.GET("/scrape", controller.<function>)
log.Printf("Server Started, version: %s", version)
r.Run(":8080")
}
Thank you Linkedin: https://www.linkedin.com/in/swarajkumarsingh/