完善 中间件 reload

This commit is contained in:
xing 2022-11-16 10:51:39 +08:00
parent c83429600b
commit dc871c1488
3 changed files with 29 additions and 12 deletions

View File

@ -80,7 +80,7 @@ func flushCache() {
common.FlushCache()
plugins.FlushCache()
actions.FlushCache()
log.Println("清除缓存成功")
log.Println("all cache flushed")
}
func reload() {
@ -95,7 +95,10 @@ func reload() {
logs.ErrPrintln(err, "获取网站设置WpOption失败")
err = config.InitTerms()
logs.ErrPrintln(err, "获取WpTerms表失败")
if middleWareReloadFn != nil {
middleWareReloadFn()
}
flushCache()
log.Println("reload complete")
}

View File

@ -3,19 +3,27 @@ package middleware
import (
"github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/safety"
"net/http"
"strings"
"sync/atomic"
"time"
)
func FlowLimit(maxRequestSleepNum, maxRequestNum int64, sleepTime []time.Duration) func(ctx *gin.Context) {
func FlowLimit(maxRequestSleepNum, maxRequestNum int64, sleepTime []time.Duration) (func(ctx *gin.Context), func(int64, int64, []time.Duration)) {
var flow int64
statPath := map[string]struct{}{
"wp-includes": {},
"wp-content": {},
"favicon.ico": {},
}
s := safety.Var[[]time.Duration]{}
s.Store(sleepTime)
fn := func(msn, mn int64, st []time.Duration) {
atomic.StoreInt64(&maxRequestSleepNum, msn)
atomic.StoreInt64(&maxRequestNum, mn)
s.Store(st)
}
return func(c *gin.Context) {
f := strings.Split(strings.TrimLeft(c.FullPath(), "/"), "/")
_, ok := statPath[f[0]]
@ -25,10 +33,11 @@ func FlowLimit(maxRequestSleepNum, maxRequestNum int64, sleepTime []time.Duratio
}
n := atomic.LoadInt64(&flow)
if n >= maxRequestSleepNum && n <= maxRequestNum {
t := helper.RandNum(sleepTime[0], sleepTime[1])
if n >= atomic.LoadInt64(&maxRequestSleepNum) && n <= atomic.LoadInt64(&maxRequestNum) {
ss := s.Load()
t := helper.RandNum(ss[0], ss[1])
time.Sleep(t)
} else if n > maxRequestNum {
} else if n > atomic.LoadInt64(&maxRequestNum) {
c.String(http.StatusForbidden, "请求太多了,服务器君表示压力山大==!, 请稍后访问")
c.Abort()
return
@ -39,5 +48,5 @@ func FlowLimit(maxRequestSleepNum, maxRequestNum int64, sleepTime []time.Duratio
}()
c.Next()
}
}, fn
}

View File

@ -40,11 +40,12 @@ func SetupRouter() (*gin.Engine, func()) {
},
}).SetTemplate()
validServerName, reloadValidServerNameFn := middleware.ValidateServerNames()
fl, flReload := middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime)
r.Use(
gin.Logger(),
validServerName,
middleware.RecoverAndSendMail(gin.DefaultErrorWriter),
middleware.FlowLimit(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime),
fl,
middleware.SetStaticFileCache,
)
//gzip 因为一般会用nginx做反代时自动使用gzip,所以go这边本身可以不用
@ -53,9 +54,7 @@ func SetupRouter() (*gin.Engine, func()) {
"/wp-includes/", "/wp-content/",
})))
}
fn := func() {
reloadValidServerNameFn()
}
f := static.Fs{FS: static.FsEx, Path: "wp-includes"}
r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(static.FsEx))
r.StaticFS("/wp-includes", http.FS(f))
@ -78,9 +77,15 @@ func SetupRouter() (*gin.Engine, func()) {
r.GET("/p/:id/feed", actions.PostFeed)
r.GET("/feed", actions.Feed)
r.GET("/comments/feed", actions.CommentsFeed)
r.POST("/comment", middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.SleepTime), actions.PostComment)
cfl, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.SleepTime)
r.POST("/comment", cfl, actions.PostComment)
if gin.Mode() != gin.ReleaseMode {
pprof.Register(r, "dev/pprof")
}
fn := func() {
reloadValidServerNameFn()
c := config.Conf.Load()
flReload(c.MaxRequestSleepNum, c.MaxRequestNum, c.SleepTime)
}
return r, fn
}