try to avoid as many upfront gzip allocs to avoid wasting resources when they're not needed

avoid gzip for guests on routeAPI
This commit is contained in:
Azareal 2020-03-12 16:52:26 +10:00
parent 17c8f7604a
commit aa32bd335c
5 changed files with 20 additions and 39 deletions

View File

@ -307,6 +307,7 @@ func (t *Theme) RunTmpl(template string, pi interface{}, w io.Writer) error {
gzw, ok := w.(GzipResponseWriter) gzw, ok := w.(GzipResponseWriter)
if ok { if ok {
w = gzw.Writer w = gzw.Writer
gzw.Header().Set("Content-Type", "text/html;charset=utf-8")
} }
getTmpl := t.GetTmpl(template) getTmpl := t.GetTmpl(template)

View File

@ -788,13 +788,13 @@ func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept {
} }
var wiMaxAge = "max-age=" + strconv.Itoa(int(c.Day)) var wiMaxAge = "max-age=" + strconv.Itoa(int(c.Day))
func (writ *WriterIntercept) WriteHeader(code int) { func (wi *WriterIntercept) WriteHeader(code int) {
if code == 200 { if code == 200 {
h := writ.ResponseWriter.Header() h := wi.ResponseWriter.Header()
h.Set("Cache-Control", wiMaxAge) h.Set("Cache-Control", wiMaxAge)
h.Set("Vary", "Accept-Encoding") h.Set("Vary", "Accept-Encoding")
} }
writ.ResponseWriter.WriteHeader(code) wi.ResponseWriter.WriteHeader(code)
} }
// HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS // HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
@ -1202,7 +1202,6 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
h := w.Header() h := w.Header()
h.Set("Content-Encoding", "gzip") h.Set("Content-Encoding", "gzip")
h.Set("Content-Type", "text/html;charset=utf-8")
gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w} gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w}
defer func() { defer func() {
if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" { if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" {
@ -1277,9 +1276,7 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
gzw, ok := w.(c.GzipResponseWriter) gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() w.Header().Del("Content-Encoding")
h.Del("Content-Type")
h.Del("Content-Encoding")
} }
err = routes.ShowAttachment(w,req,user,extraData) err = routes.ShowAttachment(w,req,user,extraData)
co.RouteViewCounter.Bump3(6, cn) co.RouteViewCounter.Bump3(6, cn)
@ -1845,9 +1842,7 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
gzw, ok := w.(c.GzipResponseWriter) gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() w.Header().Del("Content-Encoding")
h.Del("Content-Type")
h.Del("Content-Encoding")
} }
err = panel.Backups(w,req,user,extraData) err = panel.Backups(w,req,user,extraData)
co.RouteViewCounter.Bump3(89, cn) co.RouteViewCounter.Bump3(89, cn)
@ -2736,7 +2731,6 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding") h.Del("Content-Encoding")
} }
req.URL.Path += extraData req.URL.Path += extraData
@ -2756,7 +2750,6 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding") h.Del("Content-Encoding")
} }
req.URL.Path = "/s/favicon.ico" req.URL.Path = "/s/favicon.ico"

View File

@ -472,13 +472,13 @@ func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept {
} }
var wiMaxAge = "max-age=" + strconv.Itoa(int(c.Day)) var wiMaxAge = "max-age=" + strconv.Itoa(int(c.Day))
func (writ *WriterIntercept) WriteHeader(code int) { func (wi *WriterIntercept) WriteHeader(code int) {
if code == 200 { if code == 200 {
h := writ.ResponseWriter.Header() h := wi.ResponseWriter.Header()
h.Set("Cache-Control", wiMaxAge) h.Set("Cache-Control", wiMaxAge)
h.Set("Vary", "Accept-Encoding") h.Set("Vary", "Accept-Encoding")
} }
writ.ResponseWriter.WriteHeader(code) wi.ResponseWriter.WriteHeader(code)
} }
// HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS // HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
@ -886,7 +886,6 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
h := w.Header() h := w.Header()
h.Set("Content-Encoding", "gzip") h.Set("Content-Encoding", "gzip")
h.Set("Content-Type", "text/html;charset=utf-8")
gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w} gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w}
defer func() { defer func() {
if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" { if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" {
@ -928,7 +927,6 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding") h.Del("Content-Encoding")
} }
req.URL.Path += extraData req.URL.Path += extraData
@ -948,7 +946,6 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding") h.Del("Content-Encoding")
} }
req.URL.Path = "/s/favicon.ico" req.URL.Path = "/s/favicon.ico"

View File

@ -63,9 +63,7 @@ func (r *RouteImpl) NoGzip() *RouteImpl {
return r.LitBeforeMultiline(`gzw, ok := w.(c.GzipResponseWriter) return r.LitBeforeMultiline(`gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() w.Header().Del("Content-Encoding")
h.Del("Content-Type")
h.Del("Content-Encoding")
}`) }`)
} }

View File

@ -72,20 +72,15 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
// TODO: Split this into it's own function // TODO: Split this into it's own function
case "alerts": // A feed of events tailored for a specific user case "alerts": // A feed of events tailored for a specific user
if !user.Loggedin { if !user.Loggedin {
var etag string h := w.Header()
/*_, ok := w.(c.GzipResponseWriter) gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
etag = c.GzipStartEtag w = gzw.ResponseWriter
} else { h.Del("Content-Encoding")
etag = c.StartEtag
}*/
_, ok := w.(c.GzipResponseWriter)
if ok {
etag = "\"1583653869-ng\""
} else {
etag = "\"1583653869-n\""
} }
w.Header().Set("ETag", etag) etag := "\"1583653869-n\""
//etag = c.StartEtag
h.Set("ETag", etag)
if match := r.Header.Get("If-None-Match"); match != "" { if match := r.Header.Get("If-None-Match"); match != "" {
if strings.Contains(match, etag) { if strings.Contains(match, etag) {
w.WriteHeader(http.StatusNotModified) w.WriteHeader(http.StatusNotModified)
@ -108,9 +103,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
gzw, ok := w.(c.GzipResponseWriter) gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() w.Header().Del("Content-Encoding")
h.Del("Content-Type")
h.Del("Content-Encoding")
} }
_, _ = io.WriteString(w, `{}`) _, _ = io.WriteString(w, `{}`)
return nil return nil
@ -158,9 +151,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
gzw, ok := w.(c.GzipResponseWriter) gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
h := w.Header() w.Header().Del("Content-Encoding")
h.Del("Content-Type")
h.Del("Content-Encoding")
} }
_, _ = io.WriteString(w, `{}`) _, _ = io.WriteString(w, `{}`)
return nil return nil
@ -193,6 +184,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
sb.WriteString(`],"count":`) sb.WriteString(`],"count":`)
sb.WriteString(strconv.Itoa(count)) sb.WriteString(strconv.Itoa(count))
sb.WriteString(`,"tc":`) sb.WriteString(`,"tc":`)
//rCreatedAt
sb.WriteString(strconv.Itoa(int(topCreatedAt))) sb.WriteString(strconv.Itoa(int(topCreatedAt)))
sb.WriteRune('}') sb.WriteRune('}')