package middleware import ( "github.com/gin-gonic/gin" "strconv" "strings" "ycmediakit/internal/pkg/server/systemServer" "ycmediakit/internal/pkg/unit/keepalive" "ycmediakit/internal/pkg/unit/result" ) // HttpInterceptor 可自定义鉴权等操作 func HttpInterceptor() gin.HandlerFunc { return func(c *gin.Context) { c.Next() } } // KeepAliveToHlsInterceptor 保活 toHls 服务 func KeepAliveToHlsInterceptor(kas *keepalive.Supervisor) gin.HandlerFunc { return func(c *gin.Context) { kas.CheckIn(c.Request.RequestURI) c.Next() } } // ProbePayloadInterceptor 探测 ffmpeg 命令服务器是否有资源得以运行 func ProbePayloadInterceptor(ps *systemServer.PayloadServer) gin.HandlerFunc { return func(c *gin.Context) { cfg := ps.Cfg if strings.Contains(c.FullPath(), "/start") { for i := 0; i < cfg.MaxRetryTime; i++ { <-ps.Wait() p := ps.GetPayload() if p.Cpu.Usage <= cfg.MaxCpuUsage && p.Memory.Usage <= cfg.MaxMemoryUsage { c.Next() return } } errMsg := "cpu or memory overload, retry time " + strconv.Itoa(cfg.MaxRetryTime) result.Forbidden.WithMsg(errMsg).Failure(c) c.Abort() return } c.Next() } }