optimise cdn push with sync pool

This commit is contained in:
Azareal 2020-04-21 10:08:54 +10:00
parent 244b75c5fe
commit 47d88f1744
1 changed files with 15 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
c "github.com/Azareal/Gosora/common" c "github.com/Azareal/Gosora/common"
@ -23,14 +24,22 @@ func ParseSEOURL(urlBit string) (slug string, id int, err error) {
return halves[0], tid, err return halves[0], tid, err
} }
var slen1 = len("</s/>;rel=preload;as=script,") const slen1 = len("</s/>;rel=preload;as=script,")
var slen2 = len("</s/>;rel=preload;as=style,") const slen2 = len("</s/>;rel=preload;as=style,")
var pushCdnPool = sync.Pool{}
func doPush(w http.ResponseWriter, header *c.Header) { func doPush(w http.ResponseWriter, header *c.Header) {
//fmt.Println("in doPush") //fmt.Println("in doPush")
if c.Config.EnableCDNPush { if c.Config.EnableCDNPush {
// TODO: Cache these in a sync.Pool? var sb *strings.Builder
var sb strings.Builder ii := pushCdnPool.Get()
if ii == nil {
sb = &strings.Builder{}
} else {
sb = ii.(*strings.Builder)
sb.Reset()
}
push := func(in []string) { push := func(in []string) {
sb.Grow((slen1 + 6) * len(in)) sb.Grow((slen1 + 6) * len(in))
for _, path := range in { for _, path := range in {
@ -55,12 +64,12 @@ func doPush(w http.ResponseWriter, header *c.Header) {
if sb.Len() > 0 { if sb.Len() > 0 {
sbuf := sb.String() sbuf := sb.String()
pushCdnPool.Put(sb)
w.Header().Set("Link", sbuf[:len(sbuf)-1]) w.Header().Set("Link", sbuf[:len(sbuf)-1])
} }
} else if !c.Config.DisableServerPush { } else if !c.Config.DisableServerPush {
//fmt.Println("push enabled") //fmt.Println("push enabled")
gzw, ok := w.(c.GzipResponseWriter) if gzw, ok := w.(c.GzipResponseWriter); ok {
if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
} }
pusher, ok := w.(http.Pusher) pusher, ok := w.(http.Pusher)