alpha

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: BSD-3-Clause-Clear Imports: 4 Imported by: 0

Documentation

Overview

Package alpha contains alpha and/or 'application'-quality prototype implementations of various compresshttp middlewares. Whenever you're making software, start with the simplest thing you can and write some tests to make sure it works before you go crazy. "Worse is better" applies here: a simple implementation that you understand completely is better than a complex one that you don't.

Overview and usage

  • Use ServerAcceptGZIP to accept and decompress GZIP-encoded request bodies.
  • Use ServerWriteGZIP to compress responses with GZIP if the client indicates support via the 'Accept-Encoding: gzip' header.
  • Use ClientEncodeGZIPMiddleware to compress request bodies with GZIP.
  • Use ClientAcceptGZIPMiddleware to accept and decompress GZIP-encoded responses.

Note about application vs library use

Many, many applications just send small-to-medium sized JSON bodies over HTTP, and a single layer of gzip compression is often good enough. Don't go crazy engineering solutions to problems you don't have. If I assigned a junior engineer to handle HTTP compression for a simple web service, I would be MUCH more comfortable reviewing this package than the final compresshttp package, because this is simple enough to understand completely. That said, it wouldn't PASS that code review, since it's a bit TOO primitive, but it's a good starting point for simple use cases.

Package alpha contains alpha and/or 'application'-quality prototype implementations of various compresshttp middlewares. Whenever you're making software, start with the simplest thing you can and write some tests to make sure it works before you go crazy. "Worse is better" applies here: a simple implementation that you understand completely is better than a complex one that you don't.

Overview and usage

  • Use ServerAcceptGZIP to accept and decompress GZIP-encoded request bodies.
  • Use ServerWriteGZIP to compress responses with GZIP if the client indicates support via the 'Accept-Encoding: gzip' header.
  • Use ClientEncodeGZIPMiddleware to compress request bodies with GZIP.
  • Use ClientAcceptGZIPMiddleware to accept and decompress GZIP-encoded responses.

Note about application vs library use

Many, many applications just send small-to-medium sized JSON bodies over HTTP, and a single layer of gzip compression is often good enough. Don't go crazy engineering solutions to problems you don't have. If I assigned a junior engineer to handle HTTP compression for a simple web service, I would be MUCH more comfortable reviewing this package than the final compresshttp package, because this is simple enough to understand completely. That said, it wouldn't PASS that code review, since it's a bit TOO primitive, but it's a good starting point for simple use cases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientAcceptGZIPMiddleware added in v0.4.0

func ClientAcceptGZIPMiddleware(rt http.RoundTripper) http.RoundTripper

func ClientEncodeGZIPMiddleware

func ClientEncodeGZIPMiddleware(rt http.RoundTripper) http.RoundTripper

ClientEncodeGZIPMiddleware is a prototype middleware that compresses request bodies with GZIP.

Flaws

  • always compresses; doesn't skip small bodies
  • reads entire body into memory before compressing
  • hardcoded gzip level
  • doesn't support multiple layers of content-encoding: overwrites existing Content-Encoding header
  • ignores errors reading the original body

func ServerAcceptGZIP added in v0.4.0

func ServerAcceptGZIP(h http.Handler) http.HandlerFunc

ServerAcceptGZIP is a prototype middleware that detects and decompresses GZIP-encoded request bodies.

func ServerWriteGZIP added in v0.4.0

func ServerWriteGZIP(h http.Handler) http.HandlerFunc

ServerWriteGZIP is a prototype middleware that compresses responses with GZIP if the client indicates support via the 'Accept-Encoding: gzip' header

Types

type RoundTripFunc

type RoundTripFunc func(req *http.Request) (*http.Response, error)

func (RoundTripFunc) RoundTrip

func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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