You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
948 B
40 lines
948 B
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"strconv"
|
|
"strings"
|
|
"ycmediakit/internal/pkg/result"
|
|
"ycmediakit/internal/pkg/server/systemServer"
|
|
)
|
|
|
|
// HttpInterceptor 可自定义鉴权等操作
|
|
// TODO
|
|
func HttpInterceptor() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// ProbePayloadInterceptor 探测 ffmpeg 命令服务器是否有资源得以运行
|
|
// TODO
|
|
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()
|
|
}
|
|
}
|
|
|