Skip to content

Commit

Permalink
rework gzip compressor so it is lazily created for 200 OK requests only
Browse files Browse the repository at this point in the history
* fix issue when panicking (before Write) gzip writer is closed, causing
header to be written and default status of 200 OK being written.
* update recovery middleware to set 500 Internal Server Error
  • Loading branch information
stuartcarnie committed Jun 5, 2017
1 parent 9520a0b commit 55d1ba6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 63 deletions.
104 changes: 104 additions & 0 deletions services/httpd/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package httpd

import (
"compress/gzip"
"io"
"net/http"
"strings"
"sync"
)

type lazyGzipResponseWriter struct {
io.Writer
http.ResponseWriter
http.Flusher
http.CloseNotifier
wroteHeader bool
}

// gzipFilter determines if the client can accept compressed responses, and encodes accordingly.
func gzipFilter(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
inner.ServeHTTP(w, r)
return
}

gw := &lazyGzipResponseWriter{ResponseWriter: w, Writer: w}

if f, ok := w.(http.Flusher); ok {
gw.Flusher = f
}

if cn, ok := w.(http.CloseNotifier); ok {
gw.CloseNotifier = cn
}

defer gw.Close()

inner.ServeHTTP(gw, r)
})
}

func (w *lazyGzipResponseWriter) WriteHeader(code int) {
if w.wroteHeader {
return
}

w.wroteHeader = true
if code == http.StatusOK {
w.Header().Set("Content-Encoding", "gzip")
// Add gzip compressor
if _, ok := w.Writer.(*gzip.Writer); !ok {
w.Writer = getGzipWriter(w.Writer)
}
}

w.ResponseWriter.WriteHeader(code)
}

func (w *lazyGzipResponseWriter) Write(p []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK)
}
return w.Writer.Write(p)
}

func (w *lazyGzipResponseWriter) Flush() {
// Flush writer, if supported
if f, ok := w.Writer.(interface {
Flush()
}); ok {
f.Flush()
}

// Flush the HTTP response
if w.Flusher != nil {
w.Flusher.Flush()
}
}

func (w *lazyGzipResponseWriter) Close() error {
if gw, ok := w.Writer.(*gzip.Writer); ok {
putGzipWriter(gw)
}

return nil
}

var gzipWriterPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(nil)
},
}

func getGzipWriter(w io.Writer) *gzip.Writer {
gz := gzipWriterPool.Get().(*gzip.Writer)
gz.Reset(w)
return gz
}

func putGzipWriter(gz *gzip.Writer) {
gz.Close()
gzipWriterPool.Put(gz)
}
64 changes: 1 addition & 63 deletions services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"runtime/debug"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -1110,68 +1109,6 @@ func authenticate(inner func(http.ResponseWriter, *http.Request, meta.User), h *
})
}

type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

// WriteHeader sets the provided code as the response status. If the
// specified status is 204 No Content, then the Content-Encoding header
// is removed from the response, to prevent clients expecting gzipped
// encoded bodies from trying to deflate an empty response.
func (w gzipResponseWriter) WriteHeader(code int) {
if code != http.StatusNoContent {
w.Header().Set("Content-Encoding", "gzip")
}
w.ResponseWriter.WriteHeader(code)
}

func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}

func (w gzipResponseWriter) Flush() {
w.Writer.(*gzip.Writer).Flush()
if w, ok := w.ResponseWriter.(http.Flusher); ok {
w.Flush()
}
}

func (w gzipResponseWriter) CloseNotify() <-chan bool {
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
}

// gzipFilter determines if the client can accept compressed responses, and encodes accordingly.
func gzipFilter(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
inner.ServeHTTP(w, r)
return
}
gz := getGzipWriter(w)
defer putGzipWriter(gz)
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
inner.ServeHTTP(gzw, r)
})
}

var gzipWriterPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(nil)
},
}

func getGzipWriter(w io.Writer) *gzip.Writer {
gz := gzipWriterPool.Get().(*gzip.Writer)
gz.Reset(w)
return gz
}

func putGzipWriter(gz *gzip.Writer) {
gz.Close()
gzipWriterPool.Put(gz)
}

// cors responds to incoming requests and adds the appropriate cors headers
// TODO: corylanou: add the ability to configure this in our config
func cors(inner http.Handler) http.Handler {
Expand Down Expand Up @@ -1246,6 +1183,7 @@ func (h *Handler) recovery(inner http.Handler, name string) http.Handler {
logLine := buildLogLine(l, r, start)
logLine = fmt.Sprintf("%s [panic:%s] %s", logLine, err, debug.Stack())
h.CLFLogger.Println(logLine)
http.Error(w, http.StatusText(http.StatusInternalServerError), 500)
}
}()

Expand Down

0 comments on commit 55d1ba6

Please sign in to comment.