37 lines
657 B
Go
37 lines
657 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.company.lan/gopkg/gin"
|
|
"git.company.lan/gopkg/gin-contrib/timeout"
|
|
)
|
|
|
|
func testResponse(c *gin.Context) {
|
|
c.String(http.StatusRequestTimeout, "timeout")
|
|
}
|
|
|
|
func timeoutMiddleware() gin.HandlerFunc {
|
|
return timeout.New(
|
|
timeout.WithTimeout(500*time.Millisecond),
|
|
timeout.WithHandler(func(c *gin.Context) {
|
|
c.Next()
|
|
}),
|
|
timeout.WithResponse(testResponse),
|
|
)
|
|
}
|
|
|
|
func main() {
|
|
r := gin.New()
|
|
r.Use(timeoutMiddleware())
|
|
r.GET("/slow", func(c *gin.Context) {
|
|
time.Sleep(800 * time.Millisecond)
|
|
c.Status(http.StatusOK)
|
|
})
|
|
if err := r.Run(":8080"); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|