From 77669d42a5ff5335afe9753ba76e5c5821a9e0c9 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 27 May 2020 14:15:02 +1000 Subject: [PATCH] add actionSuccess function to reduce boilerplate skip doPush if there is nothing to push optimise cdnpush string building use string builder for server push strings --- routes/common.go | 93 +++++++++++++++++++++++++++++++++-------------- routes/misc.go | 8 +--- routes/reply.go | 17 +-------- routes/reports.go | 10 +---- routes/topic.go | 18 +-------- 5 files changed, 72 insertions(+), 74 deletions(-) diff --git a/routes/common.go b/routes/common.go index 5d905129..69afba34 100644 --- a/routes/common.go +++ b/routes/common.go @@ -24,52 +24,64 @@ func ParseSEOURL(urlBit string) (slug string, id int, err error) { return halves[0], tid, err } -const slen1 = len(";rel=preload;as=script,") -const slen2 = len(";rel=preload;as=style,") +const slen1 = len(";rel=preload;as=script,") + 6 +const slen2 = len(";rel=preload;as=style,") + 7 -var pushCdnPool = sync.Pool{} +var pushStrPool = sync.Pool{} -func doPush(w http.ResponseWriter, header *c.Header) { +func doPush(w http.ResponseWriter, h *c.Header) { //fmt.Println("in doPush") + if len(h.Scripts) == 0 && len(h.ScriptsAsync) == 0 && len(h.Stylesheets) == 0 { + return + } if c.Config.EnableCDNPush { - var sb *strings.Builder - ii := pushCdnPool.Get() + var sb *strings.Builder = &strings.Builder{} + /*ii := pushStrPool.Get() if ii == nil { sb = &strings.Builder{} } else { sb = ii.(*strings.Builder) sb.Reset() - } + }*/ + sb.Grow((slen1 * (len(h.Scripts) + len(h.ScriptsAsync))) + ((slen2 + 7) * len(h.Stylesheets))) push := func(in []string) { - sb.Grow((slen1 + 6) * len(in)) - for _, path := range in { - sb.WriteString(";rel=preload;as=script,") + sb.WriteString(">;rel=preload;as=script") } } - push(header.Scripts) - //push(header.PreScriptsAsync) - push(header.ScriptsAsync) + push(h.Scripts) + //push(h.PreScriptsAsync) + push(h.ScriptsAsync) - if len(header.Stylesheets) > 0 { - sb.Grow((slen2 + 7) * len(header.Stylesheets)) - for _, path := range header.Stylesheets { - sb.WriteString(" 0 { + for i, path := range h.Stylesheets { + if i != 0 { + sb.WriteString(",;rel=preload;as=style,") + sb.WriteString(">;rel=preload;as=style") } } // TODO: Push avatars? if sb.Len() > 0 { sbuf := sb.String() - pushCdnPool.Put(sb) - w.Header().Set("Link", sbuf[:len(sbuf)-1]) + w.Header().Set("Link", sbuf) + //pushStrPool.Put(sb) } } else if !c.Config.DisableServerPush { //fmt.Println("push enabled") - if gzw, ok := w.(c.GzipResponseWriter); ok { + /*if bzw, ok := w.(c.BrResponseWriter); ok { + w = bzw.ResponseWriter + } else */if gzw, ok := w.(c.GzipResponseWriter); ok { w = gzw.ResponseWriter } pusher, ok := w.(http.Pusher) @@ -79,21 +91,33 @@ func doPush(w http.ResponseWriter, header *c.Header) { //panic("has pusher") //fmt.Println("has pusher") + var sb *strings.Builder = &strings.Builder{} + /*ii := pushStrPool.Get() + if ii == nil { + sb = &strings.Builder{} + } else { + sb = ii.(*strings.Builder) + sb.Reset() + }*/ + sb.Grow(6 * (len(h.Scripts) + len(h.ScriptsAsync) + len(h.Stylesheets))) push := func(in []string) { for _, path := range in { //fmt.Println("pushing /s/" + path) - // TODO: Avoid concatenating here - err := pusher.Push("/s/"+path, nil) + sb.WriteString("/s/") + sb.WriteString(path) + err := pusher.Push(sb.String(), nil) if err != nil { break } + sb.Reset() } } - push(header.Scripts) - //push(header.PreScriptsAsync) - push(header.ScriptsAsync) - push(header.Stylesheets) + push(h.Scripts) + //push(h.PreScriptsAsync) + push(h.ScriptsAsync) + push(h.Stylesheets) // TODO: Push avatars? + //pushStrPool.Put(sb) } } @@ -172,8 +196,21 @@ func renderTemplate3(tmplName, hookName string, w http.ResponseWriter, r *http.R if c.RunPreRenderHook("pre_render_"+hookName, w, r, h.CurrentUser, pi) { return nil } + /*defer func() { + c.StrSlicePool.Put(h.Scripts) + c.StrSlicePool.Put(h.PreScriptsAsync) + }()*/ return h.Theme.RunTmpl(tmplName, pi, w) } // TODO: Rename renderTemplate to RenderTemplate instead of using this hack to avoid breaking things var RenderTemplate = renderTemplate3 + +func actionSuccess(w http.ResponseWriter, r *http.Request, dest string, js bool) c.RouteError { + if !js { + http.Redirect(w, r, dest, http.StatusSeeOther) + } else { + _, _ = w.Write(successJSONBytes) + } + return nil +} diff --git a/routes/misc.go b/routes/misc.go index d7165019..f62e923c 100644 --- a/routes/misc.go +++ b/routes/misc.go @@ -131,11 +131,5 @@ func ChangeTheme(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError cookie := http.Cookie{Name: "current_theme", Value: newTheme, Path: "/", MaxAge: int(c.Year)} http.SetCookie(w, &cookie) - - if !js { - http.Redirect(w, r, "/", http.StatusSeeOther) - } else { - _, _ = w.Write(successJSONBytes) - } - return nil + return actionSuccess(w, r, "/", js) } diff --git a/routes/reply.go b/routes/reply.go index 53cf36fb..cd1ca121 100644 --- a/routes/reply.go +++ b/routes/reply.go @@ -513,13 +513,7 @@ func ReplyLikeSubmit(w http.ResponseWriter, r *http.Request, u *c.User, srid str if skip || rerr != nil { return rerr } - - if !js { - http.Redirect(w, r, "/topic/"+strconv.Itoa(reply.ParentID), http.StatusSeeOther) - } else { - _, _ = w.Write(successJSONBytes) - } - return nil + return actionSuccess(w, r, "/topic/"+strconv.Itoa(reply.ParentID), js) } func ReplyUnlikeSubmit(w http.ResponseWriter, r *http.Request, u *c.User, srid string) c.RouteError { @@ -558,7 +552,6 @@ func ReplyUnlikeSubmit(w http.ResponseWriter, r *http.Request, u *c.User, srid s } else if err != nil { return c.InternalErrorJSQ(err, w, r, js) } - err = reply.Unlike(u.ID) if err != nil { return c.InternalErrorJSQ(err, w, r, js) @@ -581,11 +574,5 @@ func ReplyUnlikeSubmit(w http.ResponseWriter, r *http.Request, u *c.User, srid s if skip || rerr != nil { return rerr } - - if !js { - http.Redirect(w, r, "/topic/"+strconv.Itoa(reply.ParentID), http.StatusSeeOther) - } else { - _, _ = w.Write(successJSONBytes) - } - return nil + return actionSuccess(w, r, "/topic/"+strconv.Itoa(reply.ParentID), js) } diff --git a/routes/reports.go b/routes/reports.go index 12af707a..50821de6 100644 --- a/routes/reports.go +++ b/routes/reports.go @@ -103,12 +103,6 @@ func ReportSubmit(w http.ResponseWriter, r *http.Request, user *c.User, sItemID return c.LocalError("Someone has already reported this!", w, r, user) } counters.PostCounter.Bump() - - if !js { - // TODO: Redirect back to where we came from - http.Redirect(w, r, "/", http.StatusSeeOther) - } else { - _, _ = w.Write(successJSONBytes) - } - return nil + // TODO: Redirect back to where we came from + return actionSuccess(w, r, "/", js) } diff --git a/routes/topic.go b/routes/topic.go index 25e05f13..a74fb6f2 100644 --- a/routes/topic.go +++ b/routes/topic.go @@ -979,13 +979,7 @@ func LikeTopicSubmit(w http.ResponseWriter, r *http.Request, user *c.User, stid if skip || rerr != nil { return rerr } - - if !js { - http.Redirect(w, r, "/topic/"+strconv.Itoa(tid), http.StatusSeeOther) - } else { - _, _ = w.Write(successJSONBytes) - } - return nil + return actionSuccess(w, r, "/topic/"+strconv.Itoa(tid), js) } func UnlikeTopicSubmit(w http.ResponseWriter, r *http.Request, user *c.User, stid string) c.RouteError { js := r.PostFormValue("js") == "1" @@ -993,7 +987,6 @@ func UnlikeTopicSubmit(w http.ResponseWriter, r *http.Request, user *c.User, sti if err != nil { return c.PreErrorJSQ(phrases.GetErrorPhrase("id_must_be_integer"), w, r, js) } - topic, err := c.Topics.Get(tid) if err == sql.ErrNoRows { return c.PreErrorJSQ("The requested topic doesn't exist.", w, r, js) @@ -1016,7 +1009,6 @@ func UnlikeTopicSubmit(w http.ResponseWriter, r *http.Request, user *c.User, sti } else if err != nil { return c.InternalErrorJSQ(err, w, r, js) } - err = topic.Unlike(user.ID) if err != nil { return c.InternalErrorJSQ(err, w, r, js) @@ -1039,11 +1031,5 @@ func UnlikeTopicSubmit(w http.ResponseWriter, r *http.Request, user *c.User, sti if skip || rerr != nil { return rerr } - - if !js { - http.Redirect(w, r, "/topic/"+strconv.Itoa(tid), http.StatusSeeOther) - } else { - _, _ = w.Write(successJSONBytes) - } - return nil + return actionSuccess(w, r, "/topic/"+strconv.Itoa(tid), js) }