Smlr v1.0.1 Smlr View Source

Supports gzip, deflate, br, and zstd

Smlr is a plug for phoenix to compress output to the client, and optionally cache it.

It currently supports Gzip, deflate, br, and Zstd algorithms. The backend is pluggable making it easy for you to add your own compressors.

Local caching of the compressed output is available via cachex, you are able to limit the number of cached items, and or have a ttl for them.

Metrics are implemented via telemetry.

Example

## in your router simply add
plug(Smlr)
## At the end of your plug chain.

## To compress your websocket traffic add
compress: true

## To turn on the caching support add a  line like this to your config.exs
config :smlr, cache_opts: %{enable: true, timeout: :infinity, limit: nil}

## to your endpoint as described here https://hexdocs.pm/phoenix/Phoenix.Endpoint.html#socket/3-examples

Installation

Add the following to the deps block in mix.exs:

{:smlr, "~>1.0.0"}

Configuration

Configuration can be specified in the opts argument to all Smlr functions, by setting config values with e.g., Application.put_env, or by a combination of the two.

The following configuration options are supported:

  • :enable enable or disable the plug entirely. Default true.
  • :compressors A list of Modules that follow the Smlr.Compressor behavior. Default elixir [ Smlr.Compressor.Gzip, Smlr.Compressor.Deflate, Smlr.Compressor.Brotli, Smlr.Compressor.Zstd ]
  • :compressor_opts A list of tuples, {Module, level} where level is the compression number to set. Default []
  • :cache_opts A map of options to be set for cachex. Default elixir cache_opts: %{ enable: false, timeout: :infinity, limit: nil }
  • :ignore_client_weight Compress via the order of the compressors list rather than what the client indicated their preference is. Default false

Metrics

Metrics are offered via the Telemetry library. The following metrics are emitted:

  • [:smlr, :request, :pass] Client and Server had no compatible compressors, or client did not request compression
  • [:smlr, :request, :compress] Compressed response
  • [:smlr, :request, :cache] Served compress content from cache
  • [:smlr, :request, :cache, :not_started] Cachex was called but had not been started yet.
  • [:smlr, :request, :cache, :miss]
  • [:smlr, :request, :cache, :hit]
  • [:smlr, :request, :cache, :set]

Contributing

Thanks for considering contributing to this project, and to the free software ecosystem at large!

Interested in contributing a bug report? Terrific! Please open a GitHub issue and include as much detail as you can. If you have a solution, even better -- please open a pull request with a clear description and tests.

Have a feature idea? Excellent! Please open a GitHub issue for discussion.

Want to implement an issue that's been discussed? Fantastic! Please open a GitHub pull request and write a clear description of the patch. We'll merge your PR a lot sooner if it is well-documented and fully tested.

Authorship and License

Copyright 2020, Matthew O'Gorman.

This software is released under the MIT License.

Link to this section Summary

Functions

Call function. we check to see if the client has requested compression if it has, we register call back and compress before sending

Init function sets all default variables and .

Link to this section Functions

Call function. we check to see if the client has requested compression if it has, we register call back and compress before sending

Link to this function

compress_response(conn, opts)

View Source

Init function sets all default variables and .