Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "time"
- "github.com/gin-gonic/gin"
- )
- func main() {
- Port := ":8080"
- route := gin.Default()
- v1 := route.Group("/api")
- v1.Use(
- timeoutFilterV3(1 * time.Second),
- )
- {
- v1.GET("/testTimeoutFilter", testTimeoutFilter)
- }
- route.Run(Port)
- }
- func testTimeoutFilter(ctx *gin.Context) {
- time.Sleep(3 * time.Second)
- ctx.JSON(200, "ok")
- }
- func timeoutFilterV3(t time.Duration) func(c *gin.Context) {
- return func(c *gin.Context) {
- /*finish := make(chan struct{})
- go func() {
- defer func() {
- if err := recover(); err != nil {
- c.JSON(500, "error")
- c.Abort()
- finish <- struct{}{}
- }
- }()
- c.Next()
- finish <- struct{}{}
- }()*/
- select {
- case <-time.After(t):
- c.JSON(504, "timeout")
- c.Abort()
- }
- c.Next()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement