From dc871c14883764cf7f6785c2d43d8c35241c7b84 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 16 Nov 2022 10:51:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E4=BB=B6=20reload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 7 +++++-- middleware/flowLimit.go | 19 ++++++++++++++----- route/route.go | 15 ++++++++++----- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 237a7d3..a4ad683 100644 --- a/main.go +++ b/main.go @@ -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表失败") - middleWareReloadFn() + if middleWareReloadFn != nil { + middleWareReloadFn() + } + flushCache() log.Println("reload complete") } diff --git a/middleware/flowLimit.go b/middleware/flowLimit.go index 5b0dbef..80ad034 100644 --- a/middleware/flowLimit.go +++ b/middleware/flowLimit.go @@ -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 } diff --git a/route/route.go b/route/route.go index 0fc209b..121b7d9 100644 --- a/route/route.go +++ b/route/route.go @@ -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 }