5
Description
在给gin
服务添加超时控制的时候,由于一个接口需要返回重定向,然后导致了进程panic
,怀疑是
// Redirect returns an HTTP redirect to the specific location.
func (c *Context) Redirect(code int, location string) {
c.Render(-1, render.Redirect{
Code: code,
Location: location,
Request: c.Request,
})
}
返回-1导致的,请问一下这里为啥写死是-1? 报错信息
Error:invalid http status code: -1
Call Stack:/gopath/pkg/mod/[github.com/gin-contrib/timeout@v0.0.3/timeout.go:63](http://github.com/gin-contrib/timeout@v0.0.3/timeout.go:63) (0xe89731)
How to reproduce
func timeoutResponse(c *gin.Context) {
c.JSON(http.StatusGatewayTimeout, gin.H{
"code": 12,
"msg": "timeout",
})
}
// Timeout timeout中间件
// sample: [0, 100] 表示采样比例,可以通过修改采样比例来灰度
func Timeout(sample int32) gin.HandlerFunc {
num := rand.Int31n(101)
if num < sample {
return timeout.New(
timeout.WithTimeout(6*time.Second),
timeout.WithHandler(func(c *gin.Context) {
c.Next()
}),
timeout.WithResponse(timeoutResponse),
)
}
return func(c *gin.Context) {
c.Next()
}
}
Expectations
返回504 timeout
Actual result
panic
Environment
- go version: 1.18
- gin version (or commit ref): 1.8.1
- operating system: linux