Skip to content

Commit

Permalink
fix: Add proper compression level handling to ZstdCompressor
Browse files Browse the repository at this point in the history
Signed-off-by: ddukbg <[email protected]>
  • Loading branch information
ddukbg committed Nov 2, 2024
1 parent 7e28a32 commit 904a043
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/fluent/plugin/out_s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -632,20 +632,32 @@ def compress(chunk, tmp)
end

class ZstdCompressor < Compressor
require 'zstd-ruby'

DEFAULT_LEVEL = 3

def initialize(options = {})
@level = (options.is_a?(Hash) && options[:level]) || DEFAULT_LEVEL
end

def ext
'zst'.freeze
end

def content_type
'application/x-zst'.freeze
end

def compress(chunk, tmp)
compressed_data = Zstd.compress(chunk.read, level: @level)
tmp.write(compressed_data)
rescue => e
log.warn "zstd compression failed: #{e.message}"
raise e
begin
data = chunk.read.gsub(/\r\n/, "\n").force_encoding('UTF-8')
compressed = Zstd.compress(data, level: @level)
tmp.binmode
tmp.write(compressed)
rescue => e
log.warn "zstd compression failed: #{e.message}"
raise e
end
end
end

Expand Down

0 comments on commit 904a043

Please sign in to comment.