From 5e18c9babdf75048cc286eada299fc958fbe2ed1 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 18 Jan 2024 23:29:50 +0800 Subject: [PATCH] optimize code, fix bug and add some comments --- app/actions/themehook.go | 2 - app/middleware/flowLimit.go | 6 +- app/middleware/iplimit.go | 25 +- app/pkg/db/db.go | 8 +- app/theme/twentyfifteen/twentyfifteen.go | 20 +- app/theme/twentyseventeen/twentyseventeen.go | 22 +- app/theme/wp/calclass.go | 8 +- app/theme/wp/components.go | 164 +++++--- app/theme/wp/components/block/category.go | 114 +++--- app/theme/wp/components/widget/archive.go | 41 +- app/theme/wp/components/widget/category.go | 44 ++- app/theme/wp/components/widget/fn.go | 14 +- app/theme/wp/components/widget/meta.go | 47 ++- .../wp/components/widget/recentcomments.go | 37 +- app/theme/wp/components/widget/recentposts.go | 55 +-- app/theme/wp/components/widget/search.go | 54 +-- app/theme/wp/customheader.go | 21 +- app/theme/wp/customlogo.go | 6 +- app/theme/wp/detail.go | 18 +- app/theme/wp/index.go | 33 +- app/theme/wp/listpostplugins.go | 2 +- app/theme/wp/middleware/middleware.go | 17 +- app/theme/wp/pipe.go | 101 +++-- app/theme/wp/route/route.go | 77 ++-- app/theme/wp/stickyposts.go | 50 +-- app/theme/wp/wp.go | 130 +++--- cache/cachemanager/manger.go | 10 +- cache/map.go | 2 +- cache/map_test.go | 4 +- cache/reload/reload.go | 373 ++++++++++++------ cache/reload/reload_test.go | 8 +- helper/maps/map.go | 4 +- helper/slice/slice.go | 6 +- 33 files changed, 924 insertions(+), 599 deletions(-) diff --git a/app/actions/themehook.go b/app/actions/themehook.go index 083c1a5..aa7d1b4 100644 --- a/app/actions/themehook.go +++ b/app/actions/themehook.go @@ -10,8 +10,6 @@ func ThemeHook(scene string) func(*gin.Context) { return func(c *gin.Context) { t := theme.GetCurrentTemplateName() h := wp.NewHandle(c, scene, t) - h.Index = wp.NewIndexHandle(h) - h.Detail = wp.NewDetailHandle(h) templ, _ := theme.GetTemplate(t) h.SetTemplate(templ) theme.Hook(t, h) diff --git a/app/middleware/flowLimit.go b/app/middleware/flowLimit.go index ad62407..811a2ee 100644 --- a/app/middleware/flowLimit.go +++ b/app/middleware/flowLimit.go @@ -19,9 +19,9 @@ func FlowLimit(maxRequestSleepNum, maxRequestNum int64, sleepTime []time.Duratio } s := safety.Var[[]time.Duration]{} s.Store(sleepTime) - fn := func(msn, mn int64, st []time.Duration) { - atomic.StoreInt64(&maxRequestSleepNum, msn) - atomic.StoreInt64(&maxRequestNum, mn) + fn := func(sleepNum, maxNum int64, st []time.Duration) { + atomic.StoreInt64(&maxRequestSleepNum, sleepNum) + atomic.StoreInt64(&maxRequestNum, maxNum) s.Store(st) } return func(c *gin.Context) { diff --git a/app/middleware/iplimit.go b/app/middleware/iplimit.go index feb58ae..48804aa 100644 --- a/app/middleware/iplimit.go +++ b/app/middleware/iplimit.go @@ -25,14 +25,25 @@ func IpLimit(num int64) (func(ctx *gin.Context), func(int64)) { fn(num) return func(c *gin.Context) { + if atomic.LoadInt64(m.limitNum) <= 0 { + c.Next() + return + } ip := c.ClientIP() - s := false m.mux.RLock() i, ok := m.m[ip] m.mux.RUnlock() + + if !ok { + m.mux.Lock() + i = new(int64) + m.m[ip] = i + m.mux.Unlock() + } + defer func() { ii := atomic.LoadInt64(i) - if s && ii > 0 { + if ii > 0 { atomic.AddInt64(i, -1) if atomic.LoadInt64(i) == 0 { m.mux.Lock() @@ -42,20 +53,12 @@ func IpLimit(num int64) (func(ctx *gin.Context), func(int64)) { } }() - if !ok { - m.mux.Lock() - i = new(int64) - m.m[ip] = i - m.mux.Unlock() - } - - if atomic.LoadInt64(m.limitNum) > 0 && atomic.LoadInt64(i) >= atomic.LoadInt64(m.limitNum) { + if atomic.LoadInt64(i) >= atomic.LoadInt64(m.limitNum) { c.Status(http.StatusForbidden) c.Abort() return } atomic.AddInt64(i, 1) - s = true c.Next() }, fn } diff --git a/app/pkg/db/db.go b/app/pkg/db/db.go index db88b78..2f6b507 100644 --- a/app/pkg/db/db.go +++ b/app/pkg/db/db.go @@ -39,9 +39,11 @@ func InitDb() (*safety.Var[*sqlx.DB], error) { if preDb != nil { _ = preDb.Close() } - showQuerySql = reload.FnVal("showQuerySql", false, func() bool { - return config.GetConfig().ShowQuerySql - }) + if showQuerySql == nil { + showQuerySql = reload.BuildFnVal("showQuerySql", false, func() bool { + return config.GetConfig().ShowQuerySql + }) + } return safeDb, err } diff --git a/app/theme/twentyfifteen/twentyfifteen.go b/app/theme/twentyfifteen/twentyfifteen.go index 28bbb4c..8ac84c6 100644 --- a/app/theme/twentyfifteen/twentyfifteen.go +++ b/app/theme/twentyfifteen/twentyfifteen.go @@ -23,7 +23,7 @@ func configs(h *wp.Handle) { }) wp.InitPipe(h) middleware.CommonMiddleware(h) - h.Index.SetPageEle(plugins.TwentyFifteenPagination()) + setPaginationAndRender(h) h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10.005, CalCustomBackGround) h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10.0056, colorSchemeCss) h.CommonComponents() @@ -39,8 +39,22 @@ func configs(h *wp.Handle) { h.PushRender(constraints.AllScene, wp.NewHandleFn(wp.PreTemplate, 70.005, "wp.PreTemplate")) } +func setPaginationAndRender(h *wp.Handle) { + h.PushHandler(constraints.PipeRender, constraints.Detail, wp.NewHandleFn(func(hh *wp.Handle) { + d := hh.GetDetailHandle() + d.CommentRender = plugins.CommentRender() + d.CommentPageEle = plugins.TwentyFifteenCommentPagination() + }, 150, "setPaginationAndRender")) + + wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(func(hh *wp.Handle) { + i := hh.GetIndexHandle() + i.SetPageEle(plugins.TwentyFifteenPagination()) + }, 150, "setPaginationAndRender")) +} + func postThumb(h *wp.Handle) { - if h.Detail.Post.Thumbnail.Path != "" { - h.Detail.Post.Thumbnail = wpconfig.Thumbnail(h.Detail.Post.Thumbnail.OriginAttachmentData, "post-thumbnail", "") + d := h.GetDetailHandle() + if d.Post.Thumbnail.Path != "" { + d.Post.Thumbnail = wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "post-thumbnail", "") } } diff --git a/app/theme/twentyseventeen/twentyseventeen.go b/app/theme/twentyseventeen/twentyseventeen.go index 33d76fc..6cbf588 100644 --- a/app/theme/twentyseventeen/twentyseventeen.go +++ b/app/theme/twentyseventeen/twentyseventeen.go @@ -57,7 +57,7 @@ func configs(h *wp.Handle) { components.WidgetArea(h) pushScripts(h) h.PushRender(constraints.AllStats, wp.NewHandleFn(calCustomHeader, 10.005, "calCustomHeader")) - wp.SetComponentsArgs(h, widgets.Widget, map[string]string{ + wp.SetComponentsArgs(widgets.Widget, map[string]string{ "{$before_widget}": `
`, "{$after_widget}": `
`, }) @@ -67,13 +67,11 @@ func configs(h *wp.Handle) { ) videoHeader(h) h.SetData("colophon", colophon) - h.Detail.CommentRender = commentFormat - h.Detail.CommentPageEle = commentPageEle + setPaginationAndRender(h) h.CommonComponents() - h.Index.SetPageEle(paginate) wp.ReplyCommentJs(h) h.PushPostPlugin(postThumbnail) - wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) + wp.SetComponentsArgsForMap(widgets.Search, "{$form}", searchForm) wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 10.005, "wp.IndexRender")) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10.005, "wp.DetailRender")) h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Detail, 100.005, "wp.Detail"), wp.NewHandleFn(postThumb, 90.005, "{theme}.postThumb")) @@ -81,6 +79,18 @@ func configs(h *wp.Handle) { h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90.005, "wp.PreCodeAndStats")) } +func setPaginationAndRender(h *wp.Handle) { + h.PushHandler(constraints.PipeRender, constraints.Detail, wp.NewHandleFn(func(hh *wp.Handle) { + d := hh.GetDetailHandle() + d.CommentRender = commentFormat + d.CommentPageEle = commentPageEle + }, 150, "setPaginationAndRender")) + wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(func(hh *wp.Handle) { + i := hh.GetIndexHandle() + i.SetPageEle(paginate) + }, 150, "setPaginationAndRender")) +} + var searchForm = `