nytimes / gziphandler

Go middleware to gzip HTTP responses
https://godoc.org/github.com/NYTimes/gziphandler
Apache License 2.0
868 stars 129 forks source link

How to force flushing from the wrapped handler? #87

Open wmertens opened 5 years ago

wmertens commented 5 years ago

I would like to quickly flush from one of my handlers but the response is too small and it ignores the flush.

As a workaround, I set the minsize to 0. What can I do to force flushing?

jprobinson commented 5 years ago

I'm not quite sure I follow the problem, sorry. Could you please provide more context as to what you're trying to do?

The MinSize value is set because gzip provides no benefit if the response is small enough. I don't believe it has anything to do with actually "flushing" the response in HTTP terms.

wmertens commented 5 years ago

I'm running into https://github.com/nytimes/gziphandler/blob/dd0439581c7657cb652dfe5c71d7d48baf39541d/gzip.go#L252-L255 - it seems more logical to me to always flush when a flush is requested…

jprobinson commented 5 years ago

If there's nothing to flush....what would we flush there? It gets back to the point that if a response is too small, there's no benefit to gzip it and if there's nothing yet written, there's nothing to flush?

It sounds like having no MinSize actually is what you want then?

wmertens commented 5 years ago

Well, I would argue that when I call .Flush(), it shouldn't be ignored, and if not enough bytes came in to justify compression yet, it should either just turn off compression for this response, or turn it on to be on the safe side in case it will be a long-lived response, but not simply ignore the flush.

Minsize is a workaround, so now other responses that don't need flushing but are still small will still get the compression overhead.

It took me some digging to find why it wasn't flushing… At the very least it could be mentioned?