Help us shape the future of Go! Take the 2026 Go Developer Survey

compress

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package compress is runtime content-negotiating response compression (zstd, br, gzip) with pooled encoders and an optional cache for immutable assets.

Index

Constants

View Source
const (
	// DefaultMinSize is the smallest body worth compressing.
	DefaultMinSize = 1024
)

Variables

This section is empty.

Functions

func Compressible

func Compressible(contentType string) bool

Compressible reports whether a Content-Type is worth compressing. Streaming framings (SSE, event streams, gRPC, Connect streams) are excluded.

Types

type Compressor

type Compressor struct {
	// contains filtered or unexported fields
}

Compressor compresses responses. It is safe for concurrent use.

func New

func New(cfg Config) *Compressor

New builds a Compressor from cfg.

func (*Compressor) Begin

func (c *Compressor) Begin(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, func(), bool)

Begin negotiates for r: served=true means a cached response was written; otherwise use the returned writer and run finish (nil when not compressing) after the handler returns.

func (*Compressor) EchoMiddleware

func (c *Compressor) EchoMiddleware() func(echo.HandlerFunc) echo.HandlerFunc

EchoMiddleware adapts the Compressor to Echo. It swaps the writer inside the concrete *echo.Response so type assertions on c.Response() keep working.

func (*Compressor) Handler

func (c *Compressor) Handler(next http.Handler) http.Handler

Handler wraps next with response compression.

type Config

type Config struct {
	// Cacheable marks requests whose compressed output is cached by path and coding.
	Cacheable func(*http.Request) bool
	// Encodings restricts the enabled codings (default: all).
	Encodings []Encoding
	// MinSize is the minimum body size to compress (default 1 KiB).
	MinSize int
	// ZstdLevel, BrotliLevel and GzipLevel override encoder levels.
	ZstdLevel   zstd.EncoderLevel
	BrotliLevel int
	GzipLevel   int
	// CacheMaxBytes bounds the cache (default 64 MiB).
	CacheMaxBytes int64
	// AlwaysVary sets Vary: Accept-Encoding even when the client sent none.
	AlwaysVary bool
	// RewriteCRC32 recomputes X-Amz-Crc32 over the compressed bytes.
	RewriteCRC32 bool
}

Config tunes a Compressor. Zero values pick benchmarked defaults.

type Encoding

type Encoding string

Encoding is a supported content coding token.

const (
	Zstd   Encoding = "zstd"
	Brotli Encoding = "br"
	Gzip   Encoding = "gzip"
)

Supported content codings.

func Negotiate

func Negotiate(values []string, enabled func(Encoding) bool) Encoding

Negotiate picks the response coding for an Accept-Encoding header per RFC 9110 section 12.5.3; enabled filters the server-supported set. Returns "" for identity.

Jump to

Keyboard shortcuts

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