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 ¶
const (
// DefaultMinSize is the smallest body worth compressing.
DefaultMinSize = 1024
)
Variables ¶
This section is empty.
Functions ¶
func Compressible ¶
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 (*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.
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.