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)
if ok {
w = gzw.Writer
gzw.Header().Set("Content-Type", "text/html;charset=utf-8")
}
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))
func (writ *WriterIntercept) WriteHeader(code int) {
func (wi *WriterIntercept) WriteHeader(code int) {
if code == 200 {
h := writ.ResponseWriter.Header()
h := wi.ResponseWriter.Header()
h.Set("Cache-Control", wiMaxAge)
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
@ -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") {
h := w.Header()
h.Set("Content-Encoding", "gzip")
h.Set("Content-Type", "text/html;charset=utf-8")
gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w}
defer func() {
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)
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
w.Header().Del("Content-Encoding")
}
err = routes.ShowAttachment(w,req,user,extraData)
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)
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
w.Header().Del("Content-Encoding")
}
err = panel.Backups(w,req,user,extraData)
co.RouteViewCounter.Bump3(89, cn)
@ -2736,7 +2731,6 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
}
req.URL.Path += extraData
@ -2756,7 +2750,6 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
}
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))
func (writ *WriterIntercept) WriteHeader(code int) {
func (wi *WriterIntercept) WriteHeader(code int) {
if code == 200 {
h := writ.ResponseWriter.Header()
h := wi.ResponseWriter.Header()
h.Set("Cache-Control", wiMaxAge)
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
@ -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") {
h := w.Header()
h.Set("Content-Encoding", "gzip")
h.Set("Content-Type", "text/html;charset=utf-8")
gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w}
defer func() {
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 {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
}
req.URL.Path += extraData
@ -948,7 +946,6 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
}
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)
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
w.Header().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
case "alerts": // A feed of events tailored for a specific user
if !user.Loggedin {
var etag string
/*_, ok := w.(c.GzipResponseWriter)
h := w.Header()
gzw, ok := w.(c.GzipResponseWriter)
if ok {
etag = c.GzipStartEtag
} else {
etag = c.StartEtag
}*/
_, ok := w.(c.GzipResponseWriter)
if ok {
etag = "\"1583653869-ng\""
} else {
etag = "\"1583653869-n\""
w = gzw.ResponseWriter
h.Del("Content-Encoding")
}
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 strings.Contains(match, etag) {
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)
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
w.Header().Del("Content-Encoding")
}
_, _ = io.WriteString(w, `{}`)
return nil
@ -158,9 +151,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
gzw, ok := w.(c.GzipResponseWriter)
if ok {
w = gzw.ResponseWriter
h := w.Header()
h.Del("Content-Type")
h.Del("Content-Encoding")
w.Header().Del("Content-Encoding")
}
_, _ = io.WriteString(w, `{}`)
return nil
@ -193,6 +184,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
sb.WriteString(`],"count":`)
sb.WriteString(strconv.Itoa(count))
sb.WriteString(`,"tc":`)
//rCreatedAt
sb.WriteString(strconv.Itoa(int(topCreatedAt)))
sb.WriteRune('}')