compresshandler

package module
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 9 Imported by: 0

README

compresshandler

Go auto compress and decompress handlers for net/http and fasthttp

GoDoc CI

This package provides a middleware for net/http and fasthttp that auto decompress request body and auto compress response body with prefered client compression. Supports all IANA's initially registred tokens

Restrictions

Server decompressor

According to RFCs there is no 'Accept-Encoding' header at server side response. It means you cannot tell clients (browsers, include headless browsers like curl/python's request) that your server accept compressed requests. But some of the backends (for example mod_deflate) support compressed http requests, thats why the same feature exists in this package.

Encoding support

There is other compression algorithm: LZW and Zstd. But overall score for encoding+transfer+decoding is the same. If you really want to increase content transfer performance, its better to use minification + compression (this package) + http2, rather then jumps between algos, because:

  • users does not care which one we use, because only TTI (time to interactive) counts. There is no difference between 0.28sec TTI and 0.30sec TTI
  • operation team does not care which one we use, because only total cost of io/cpu/ram counts. There is no win-win algo, who dramatically decrease it from 10k$ into 1k$
  • other developers too lazy to enable any non gzip compression/decompression support, because time is money

Usage

net/http
package main

import (
        "io"
        "net/http"

        "github.com/alexdyukov/compresshandler"
)

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

        compressConfig := compresshandler.Config{
                GzipLevel:        compresshandler.GzipDefaultCompression,
                ZlibLevel:        compresshandler.ZlibDefaultCompression,
                BrotliLevel:      compresshandler.BrotliDefaultCompression,
                MinContentLength: 1400,
        }

        compress := compresshandler.NewNetHTTP(compressConfig)

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

import (
        "github.com/alexdyukov/compresshandler"
        "github.com/valyala/fasthttp"
)

func main() {
        echo := func(ctx *fasthttp.RequestCtx) {
                ctx.SetBody(ctx.Request.Body())
        }

        compressConfig := compresshandler.Config{
                GzipLevel:        compresshandler.GzipDefaultCompression,
                ZlibLevel:        compresshandler.ZlibDefaultCompression,
                BrotliLevel:      compresshandler.BrotliDefaultCompression,
                MinContentLength: 1400,
        }

        compress := compresshandler.NewFastHTTP(compressConfig)

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

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