Open leeqvip opened 2 years ago
You should pass context
as first parameter to your goroutine,otherwise, you will get a different context in the goroutine, and the ip will naturally be different,that's what context exists for.
@leeqvip context is pooled object, pass context.Copy as first param.
func Handler(c *gin.Context) {
ip1 := c.ClientIP()
go func(cc *gin.Context) {
ip2 := cc.ClientIP()
fmt.Println(ip1, ip2)
}(c.Copy())
c.JSON(200, nil)
}
Description
c.ClientIP() is incorrect under goroutine
How to reproduce
Expectations
In most cases, the two are equal, but under high concurrency, the two are not equal, and the ip is rewritten by other requests
Actual result
Environment