gin-blog/internal/middleware/context_timeout.go

18 lines
306 B
Go
Raw Normal View History

package middleware
import (
"context"
"github.com/gin-gonic/gin"
"time"
)
func ContextTimeout(t time.Duration) func(c *gin.Context) {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), t)
defer cancel()
c.Request = c.Request.WithContext(ctx)
c.Next()
}
}