Skip to main content

compression_layer_from_env

Function compression_layer_from_env 

Source
pub(crate) fn compression_layer_from_env() -> Option<CompressionLayer>
Expand description

根据 COMPRESSION_ALGORITHMS 环境变量构造 CompressionLayer。 默认(未设置时)关闭压缩;显式设为 “all” 启用全部算法,设为 “gzip,brotli,…” 按需选择;设为 “”、“none” 或 “off” 等价于不启用。

CompressionLayer 使用 tower-http 的 DefaultPredicate,开箱即用即:

  • 跳过 image/* content-type(WebP/PNG/JPEG/GIF 等已是压缩格式,再压浪费 CPU, 唯一例外是 image/svg+xml,作为 XML 文本可被压缩);
  • 跳过 gRPC 与 text/event-stream(SSE);
  • 跳过小于 32 字节的响应。

因此无需在此处对图片响应做额外的 content-type 过滤。另:图片实际挂在 static_routes(无中间件),根本不经此层,详见 main.rs 路由 merge 处。