class Amber::Pipe::Compression

Overview

Gzips or deflates a pipeline's responses for clients that accept it.

The bytes on the wire match Crystal's HTTP::CompressHandler at the same level. The difference is where the compressor lives: each zlib stream (about 270 KB of state and buffers) comes from malloc, outside the garbage-collected heap, and goes back to a small pool when the response closes. HTTP::CompressHandler allocates that state from the collected heap for every response, which made it the largest allocation in a generated app.

A request with a Range header is answered uncompressed, because a byte range refers to the uncompressed body. Every response through the pipe carries Vary: Accept-Encoding. A response that already has a Content-Encoding (for example a pre-gzipped page from the page cache) passes through unchanged.

pipeline :web do
  plug Amber::Pipe::Compression.new
  plug Amber::Pipe::Error.new
end

level is the zlib level, 0 to 9, or -1 for zlib's default (6). idle_compressors is how many compressors the pool keeps between responses; one more in use at once is created and then released.

Defined in:

amber/pipes/compression.cr
amber/pipes/compression/compressed_output.cr
amber/pipes/compression/deflater.cr

Constant Summary

DEFAULT_IDLE_COMPRESSORS = 4

How many compressors the pool keeps between responses by default.

Constructors

Instance Method Summary

Instance methods inherited from class Amber::Pipe::Base

call(context : HTTP::Server::Context) call

Constructor Detail

def self.new(level : Int32 = ::Compress::Deflate::DEFAULT_COMPRESSION, idle_compressors : Int32 = DEFAULT_IDLE_COMPRESSORS) #

[View source]

Instance Method Detail

def call(context : HTTP::Server::Context) #
Description copied from class Amber::Pipe::Base

Execution of this handler.


[View source]
def pool : DeflaterPool #

The compressors this pipe lends to its responses.