compresshandler

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2022 License: MIT Imports: 9 Imported by: 0

README

compresshandler

Go compress http handler. Just fire and forget.

GoDoc CI

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.

Documentation

Index

Constants

View Source
const (
	GzipBestSpeed            = compressor.GzipBestSpeed
	GzipBestCompression      = compressor.GzipBestCompression
	GzipDefaultCompression   = (GzipBestCompression - GzipBestSpeed) / 2
	ZlibBestSpeed            = compressor.ZlibBestSpeed
	ZlibBestCompression      = compressor.ZlibBestCompression
	ZlibDefaultCompression   = (ZlibBestCompression - ZlibBestSpeed) / 2
	BrotliBestSpeed          = compressor.BrotliBestSpeed
	BrotliBestCompression    = compressor.BrotliBestCompression
	BrotliDefaultCompression = (BrotliBestCompression - BrotliBestSpeed) / 2
)

Variables

This section is empty.

Functions

func NewFastHTTP added in v1.4.0

func NewFastHTTP(config Config) func(next fasthttp.RequestHandler) fasthttp.RequestHandler

func NewNetHTTP

func NewNetHTTP(config Config) func(next http.Handler) http.Handler

Types

type Config

type Config struct {
	GzipLevel        int
	ZlibLevel        int
	BrotliLevel      int
	MinContentLength int
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL