Skip to content

Commit b1efca1

Browse files
committed
fix: Improve ZstdCompressor implementation
Signed-off-by: ddukbg <wowrebong@gmail.com>
1 parent 7e28a32 commit b1efca1

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

lib/fluent/plugin/out_s3.rb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -632,20 +632,40 @@ def compress(chunk, tmp)
632632
end
633633

634634
class ZstdCompressor < Compressor
635+
require 'zstd-ruby'
636+
637+
DEFAULT_LEVEL = 3
638+
639+
def initialize(level = nil)
640+
@level = level || DEFAULT_LEVEL
641+
end
642+
635643
def ext
636644
'zst'.freeze
637645
end
638-
646+
639647
def content_type
640648
'application/x-zst'.freeze
641649
end
642-
650+
643651
def compress(chunk, tmp)
644-
compressed_data = Zstd.compress(chunk.read, level: @level)
645-
tmp.write(compressed_data)
646-
rescue => e
647-
log.warn "zstd compression failed: #{e.message}"
648-
raise e
652+
begin
653+
original_data = chunk.read
654+
log.debug "Original data size: #{original_data.bytesize}"
655+
656+
compressed = Zstd.compress(original_data, level: @level)
657+
log.debug "Compressed data size: #{compressed.bytesize}"
658+
659+
tmp.binmode
660+
bytes_written = tmp.write(compressed)
661+
log.debug "Bytes written: #{bytes_written}"
662+
tmp.flush
663+
tmp.close
664+
rescue => e
665+
log.warn "zstd compression failed: #{e.message}"
666+
log.warn e.backtrace.join("\n")
667+
raise e
668+
end
649669
end
650670
end
651671

0 commit comments

Comments
 (0)