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

Add gzip compression middleware to server #219

Open
yvrhdn opened this issue Jul 26, 2021 · 0 comments
Open

Add gzip compression middleware to server #219

yvrhdn opened this issue Jul 26, 2021 · 0 comments

Comments

@yvrhdn
Copy link

yvrhdn commented Jul 26, 2021

Hi, we noticed that Server by default does not compress its responses, even if the client accepts this. I.e. when a http client sends a request with Accept-Encoding: gzip, the server can choose to compress the response (using gzip in this case) which will save bandwidth and is usually more efficient.

I suggest adding this to the middleware that is added by default. I'm basically echoing @bboreham's comments:

  • If it's not enabled by default, most users won't be able to profit from this
  • The Go http client will request compression and decompress responses by default, so a user will not even notice compression is used.

To implement this we can use nytimes/gziphandler. It's interface matches very closely with middleware.Interface so the entire implementation would be pretty small. gziphandler will by default only compress responses larger than 1400 bytes.

package middleware

import (
	"net/http"

	"github.com/NYTimes/gziphandler"
)

type Gzip struct {
}

func (g Gzip) Wrap(next http.Handler) http.Handler {
	return gziphandler.GzipHandler(next)
}

gziphandler is already being used by Cortex, so it has been battle tested with Server.

If this makes sense I can create a PR for this with tests etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant