optimize code
This commit is contained in:
parent
2131e6862f
commit
f775a7f605
@ -5,6 +5,7 @@ import (
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
@ -36,3 +37,21 @@ func FlowLimit(maxRequestSleepNum, maxRequestNum int64, sleepTime []time.Duratio
|
||||
c.Next()
|
||||
}, fn
|
||||
}
|
||||
|
||||
func WPFlowLimit(maxRequestSleepNum, maxRequestNum int64, sleepTime []time.Duration) (func(ctx *gin.Context), func(int64, int64, []time.Duration)) {
|
||||
statPath := map[string]struct{}{
|
||||
"wp-includes": {},
|
||||
"wp-content": {},
|
||||
"favicon.ico": {},
|
||||
}
|
||||
next, reFn := FlowLimit(maxRequestSleepNum, maxRequestNum, sleepTime)
|
||||
return func(c *gin.Context) {
|
||||
f := strings.Split(strings.TrimLeft(c.FullPath(), "/"), "/")
|
||||
_, ok := statPath[f[0]]
|
||||
if len(f) > 0 && ok {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
next(c)
|
||||
}, reFn
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ func SetupRouter() *gin.Engine {
|
||||
wpconfig.SetTemplateFs(theme.TemplateFs)
|
||||
}, 90.5)
|
||||
|
||||
siteFlowLimitMiddleware, siteFlow := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
||||
siteFlowLimitMiddleware, siteFlow := middleware.WPFlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
||||
reload.Append(func() {
|
||||
c = config.GetConfig()
|
||||
siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime)
|
||||
|
Loading…
Reference in New Issue
Block a user