Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gzip to http responses for query/batch-query #232

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pkg/api/handler_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package api
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to just call this file gzip.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, will make that change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up renaming to gzip_handler.go, I balked at gzip.go since it seemed like a name that could be used a lot elsewhere.


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

type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

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

func gzipWrapper(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check that path ends in /query or /batch-query
if !strings.HasSuffix(r.URL.Path, "/query") && !strings.HasSuffix(r.URL.Path, "/batch-query") {
handler.ServeHTTP(w, r)
return
}

if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
handler.ServeHTTP(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
handler.ServeHTTP(gzw, r)
})
}
4 changes: 3 additions & 1 deletion pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ func (s *Server) startHTTP() error {
return errors.Wrap(err, "creating grpc-gateway listener")
}

// Add two handler wrappers to mux: gzipWrapper and allowCORS
server := http.Server{
Addr: addr,
Handler: allowCORS(mux),
Handler: allowCORS(gzipWrapper(mux)),
// Handler: gzipWrapper(allowCORS(mux)),
}

tracing.GoPanicWrap(s.ctx, &s.wg, "http", func(ctx context.Context) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func Test_QueryNoTopics(t *testing.T) {
require.Equal(t, codes.InvalidArgument, grpcErr.Code())
require.EqualError(t, err, `rpc error: code = InvalidArgument desc = content topics required`)
} else {
require.EqualError(t, err, "400 Bad Request: {\"code\":3, \"message\":\"content topics required\", \"details\":[]}")
require.Regexp(t, `400 Bad Request: {"code\":3,\s?"message":"content topics required",\s?"details":\[\]}`, err.Error())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somehow the whitespaces get stripped now? I noticed the test above already accounts for this via regexp so did the same here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right I think this is what's happening golang/protobuf#1121

}
require.Nil(t, queryRes)
})
Expand Down