potato/internal/middleware/context_timeout.go

25 lines
468 B
Go
Raw Normal View History

2021-06-13 22:47:19 +08:00
/*
* @Date: 2021-06-13 22:35:30
* @LastEditors: viletyy
* @LastEditTime: 2021-06-13 22:37:09
* @FilePath: /potato/internal/middleware/context_timeout.go
*/
package middleware
import (
"context"
"time"
"github.com/gin-gonic/gin"
)
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()
}
}