Skip to content

Commit

Permalink
Add support for compression in http api
Browse files Browse the repository at this point in the history
The need has been spotted in issue #3687.
Using "NYTimes/gziphandler", the http api responses can now be compressed if required.
The Go API requires compressed response if possible and handle the compressed response.
We here change only the http api (not the UI for instance).
  • Loading branch information
Yoann committed Apr 3, 2018
1 parent c95bc27 commit 0f6e05d
Show file tree
Hide file tree
Showing 7 changed files with 736 additions and 1 deletion.
4 changes: 4 additions & 0 deletions agent/agent_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,10 @@ func (s *HTTPServer) AgentMonitor(resp http.ResponseWriter, req *http.Request) (

// Send header so client can start streaming body
resp.WriteHeader(http.StatusOK)

// 0 byte write is needed before the Flush call so that if we are using
// a gzip stream it will go ahead and write out the HTTP response header
resp.Write([]byte(""))
flusher.Flush()

// Stream logs until the connection is closed.
Expand Down
7 changes: 6 additions & 1 deletion agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-cleanhttp"
"github.com/mitchellh/mapstructure"
"github.com/NYTimes/gziphandler"
)

// MethodNotAllowedError should be returned by a handler when the HTTP method is not allowed.
Expand Down Expand Up @@ -64,6 +65,7 @@ func registerEndpoint(pattern string, methods []string, fn unboundEndpoint) {
if endpoints[pattern] != nil || allowedMethods[pattern] != nil {
panic(fmt.Errorf("Pattern %q is already registered", pattern))
}

endpoints[pattern] = fn
allowedMethods[pattern] = methods
}
Expand Down Expand Up @@ -111,7 +113,10 @@ func (s *HTTPServer) handler(enableDebug bool) http.Handler {
metrics.MeasureSince(append([]string{"consul"}, key...), start)
metrics.MeasureSince(key, start)
}
mux.HandleFunc(pattern, wrapper)

gzipWrapper, _ := gziphandler.GzipHandlerWithOpts(gziphandler.MinSize(0))
gzipHandler := gzipWrapper(http.HandlerFunc(wrapper))
mux.Handle(pattern, gzipHandler)
}

mux.HandleFunc("/", s.Index)
Expand Down
201 changes: 201 additions & 0 deletions vendor/github.com/NYTimes/gziphandler/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions vendor/github.com/NYTimes/gziphandler/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0f6e05d

Please sign in to comment.