Conversation
bundle exec benchmark/compression.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Compression
SSE data (JSON + HTML) is highly compressible, and long-lived connections benefit significantly from compression. This SDK supports opt-in Brotli and gzip compression for SSE streams.
Enabling compression
Per-instance:
Or globally:
When enabled, the SDK negotiates compression with the client via the
Accept-Encodingheader and sets the appropriateContent-Encodingresponse header. If the client does not support compression, responses are sent uncompressed.Brotli vs gzip
Brotli (
:br) is preferred by default as it offers better compression ratios. It requires the host app to require thebrotligem. Gzip uses Ruby built-inzliband requires no extra dependencies.To use Brotli, add the gem to your
Gemfile:Configuration options
You can also set these per-instance:
Per-encoder options
Options are passed directly to the underlying compressor via the array form. Available options depend on the encoder.
Gzip (via
Zlib::Deflate)::levelZlib::DEFAULT_COMPRESSIONZlib::BEST_SPEEDandZlib::BEST_COMPRESSIONalso work.:mem_level8:strategyZlib::DEFAULT_STRATEGYZlib::FILTERED,Zlib::HUFFMAN_ONLY,Zlib::RLE,Zlib::FIXED.Brotli (via
Brotli::Compressor, requires thebrotligem)::quality11:lgwin22:lgblock0(auto):mode:generic:generic,:text, or:font.:textis a good choice for SSE (UTF-8 HTML/JSON).Proxy considerations
Even with
X-Accel-Buffering: no(set by default), some proxies like Nginx may buffer compressed responses. You may need to addproxy_buffering offto your Nginx configuration when using compression with SSE.Benchmarks
There's a simple benchmark script in
./benchmarks/compression.rbRun with
bundle exec benchmarks/compression.rb