Documentation
¶
Overview ¶
Package compression implements the HTTP content codings nodecore speaks on both of its edges: the client-facing ingress and the upstream connectors.
Both edges need the same three things - decide which coding to use, encode a body, decode a body - so the codec pools live here once rather than in each edge. Levels are fixed at the cheapest useful setting of each codec: a proxy pays the compression cost on the hot path of every request, where CPU time costs more than the extra few percent of ratio.
Index ¶
Constants ¶
const Offer = "zstd, br, gzip"
Offer is the Accept-Encoding nodecore sends upstream, in preference order. A node that knows none of them simply answers identity - content negotiation degrades on its own, which is why this needs no config knob.
Variables ¶
var ErrUnsupportedEncoding = errors.New("unsupported content encoding")
ErrUnsupportedEncoding reports a Content-Encoding nodecore cannot decode. It is never the client's fault on the upstream edge - nodecore offers only the codings in Offer, so anything else is a misbehaving node.
Functions ¶
func ReleaseWriter ¶
func ReleaseWriter(w Writer)
ReleaseWriter returns a writer to its pool. It resets the writer onto io.Discard first so a pooled encoder never pins the response it just finished writing to.
func WrapReader ¶
WrapReader returns a reader that decodes r according to contentEncoding. An empty or identity encoding passes r through untouched.
The returned Close releases the pooled codec and MUST be called; it does not close r, whose lifetime stays with the caller. An empty body is nothing to decode, whatever coding it claims. Every coding has to say so: left to themselves they disagree, gzip calling it a truncated header, zstd a missing magic and brotli a truncated stream, and a peer that labels an empty 204 with a coding reaches all three. echo's decompress middleware went out of its way to let one through ("ignore if body is empty") and so did Go's transparent gzip upstream, so this keeps that contract on both edges.
Types ¶
type Scheme ¶
type Scheme string
Scheme is a content coding nodecore can encode and decode.
const ( // Identity means "no compression"; it is the zero value so an // unparseable or absent Accept-Encoding degrades to plain bodies. Identity Scheme = "" Gzip Scheme = "gzip" Zstd Scheme = "zstd" // Brotli's content-coding token is "br" (RFC 7932), not "brotli": a // client asking for the latter is asking for a coding nobody implements. Brotli Scheme = "br" )
func Negotiate ¶
Negotiate picks the coding to encode a response with, given the client's Accept-Encoding (RFC 9110 §12.5.3). The highest q wins, and an exact tie goes to whichever coding comes first in preference. Anything unrecognised, refused with q=0, or absent yields Identity.
A coding the client names twice is settled by its last mention, which the RFC leaves open and which lets a merged header ("gzip, gzip;q=0", two field lines joined) express a refusal rather than contradict itself.
When nothing at all is acceptable - "identity;q=0" on its own, or "*;q=0" - this answers Identity rather than the 406 §12.5.3 permits. Serving a plain body is the response an RPC client can actually use, and the RFC allows disregarding negotiation when no available representation is acceptable.
type Writer ¶
type Writer interface {
io.WriteCloser
// Flush pushes everything written so far to the underlying writer, so a
// streamed chunk reaches the client without waiting for Close.
Flush() error
// Reset redirects the writer, discarding any state from a previous body.
Reset(w io.Writer)
}
Writer is a compressing writer for one response body. Every pooled codec satisfies it natively.