Module: Bitfab::Compress
- Defined in:
- lib/bitfab/compress.rb
Overview
Request body compression for Bitfab API requests.
Defined Under Namespace
Classes: PreparedRequest
Constant Summary collapse
- DISABLE_COMPRESSION_ENV =
"BITFAB_DISABLE_COMPRESSION"- MIN_COMPRESSED_BYTES =
Below this, compressing costs more than the saved bytes are worth, so small requests (function lookups, replay status polls, single-span batches) ride uncompressed.
8_192
Class Method Summary collapse
-
.encode_request_body(body) ⇒ Object
Returns the body to send and the Content-Encoding it carries, or nil when the body is sent as-is.
- .prepare_request_body(body) ⇒ Object
Class Method Details
.encode_request_body(body) ⇒ Object
Returns the body to send and the Content-Encoding it carries, or nil when the body is sent as-is. Compression is best-effort: any failure sends the original body rather than dropping the span.
22 23 24 25 |
# File 'lib/bitfab/compress.rb', line 22 def encode_request_body(body) prepared = prepare_request_body(body) [prepared.body, prepared.content_encoding] end |
.prepare_request_body(body) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bitfab/compress.rb', line 27 def prepare_request_body(body) size = body.bytesize return PreparedRequest.new(body, nil, size, size) if ENV[DISABLE_COMPRESSION_ENV] return PreparedRequest.new(body, nil, size, size) if size < MIN_COMPRESSED_BYTES compressed = Zlib.gzip(body) return PreparedRequest.new(body, nil, size, size) if compressed.bytesize >= size PreparedRequest.new(compressed, "gzip", size, compressed.bytesize) rescue PreparedRequest.new(body, nil, body.bytesize, body.bytesize) end |