compresshandler

package module
Version: v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2022 License: MIT Imports: 11 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"
        "github.com/klauspost/compress/gzip"
        "github.com/klauspost/compress/zlib"
        "github.com/andybalholm/brotli"
)

func main() {
        echo := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                w.Write(r.Body)
        })

        compressConfig := compresshandler.Config{
                GzipLevel:        gzip.DefaultCompression,
                ZlibLevel:        zlib.DefaultCompression,
                BrotliLevel:      brotli.DefaultCompression,
                MinContentLength: 1400,
        }

        compress := compresshandler.NewNetHTTP(compressConfig)

        http.ListenAndServe(":8080", compress(echo))
}

TODOs

  • fasthttp handler
  • 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,}compressors/
goos: linux
goarch: amd64
pkg: github.com/alexdyukov/compresshandler/internal/decompressors
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/decompressors    0.459s
goos: linux
goarch: amd64
pkg: github.com/alexdyukov/compresshandler/internal/compressors
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/compressors      1.332s

License

MIT licensed. See the included LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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