compresshandler
Go compress http handler. Just fire and forget.
Package provides methods to wrap http handler for auto decompress compressed data & auto compress response with prefered client compression.
Example
import (
"net/http"
"github.com/alexdyukov/compresshandler"
)
func main() {
echo := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(r.Body)
})
compressConfig := compresshandler.Config{
GzipLevel: compresshandler.GzipDefaultCompression,
ZlibLevel: compresshandler.ZlibDefaultCompression,
BrotliLevel: compresshandler.BrotliDefaultCompression,
MinContentLength: 1400,
}
compress := compresshandler.NewNetHTTP(compressConfig)
http.ListenAndServe(":8080", compress(echo))
}
TODOs
- gin handler
- configurable content response type. We dont need to zip already zipped image or archives:
https://www.iana.org/assignments/media-types/media-types.xhtml
# default should be:
application/*json
application/*xml
application/*json
*javascript
*ecmascript
- found out better brotli implementation :
$ GOMAXPROCS=8 go test -bench='Bench' -benchmem -benchtime=100000x ./internal/{de,}compressor/
goos: linux
goarch: amd64
pkg: github.com/alexdyukov/compresshandler/internal/decompressor
cpu: AMD Ryzen 7 5800U with Radeon Graphics
BenchmarkBrotli-8 100000 3861 ns/op 24221 B/op 11 allocs/op
BenchmarkGzip-8 100000 323.9 ns/op 16 B/op 1 allocs/op
BenchmarkZlib-8 100000 305.2 ns/op 16 B/op 1 allocs/op
PASS
ok github.com/alexdyukov/compresshandler/internal/decompressor 0.459s
goos: linux
goarch: amd64
pkg: github.com/alexdyukov/compresshandler/internal/compressor
cpu: AMD Ryzen 7 5800U with Radeon Graphics
BenchmarkBrotli-8 100000 13012 ns/op 11 B/op 0 allocs/op
BenchmarkGzipCompressor-8 100000 118.7 ns/op 9 B/op 0 allocs/op
BenchmarkZlib-8 100000 108.8 ns/op 9 B/op 0 allocs/op
PASS
ok github.com/alexdyukov/compresshandler/internal/compressor 1.332s
License
MIT licensed. See the included LICENSE file for details.