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

View File

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

View File

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