From 17d9322fadf7fad28cfa44118c7831c7a00d96ea Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Thu, 9 Jan 2025 15:21:58 +0900 Subject: [PATCH] in_tail: reduce the lifespan of the buffer area (#4763) **Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: When log files are rotated at high speed, the lifespan of `IOHandler` objects may become extended. Along with this, the lifespan of `@iobuf` also increases. Since `@iobuf` uses 8 KB of memory, if many such objects exist simultaneously, memory usage might significantly increase. **Docs Changes**: **Release Note**: Signed-off-by: Shizuo Fujita --- lib/fluent/plugin/in_tail.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 703d9d01c1..23a2af7c4b 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -1119,7 +1119,6 @@ def initialize(watcher, path:, read_lines_limit:, read_bytes_limit_per_second:, @receive_lines = receive_lines @open_on_every_update = open_on_every_update @fifo = FIFO.new(from_encoding || Encoding::ASCII_8BIT, encoding || Encoding::ASCII_8BIT, log, max_line_size) - @iobuf = ''.force_encoding('ASCII-8BIT') @lines = [] @io = nil @notify_mutex = Mutex.new @@ -1201,6 +1200,7 @@ def handle_notify end with_io do |io| + iobuf = ''.force_encoding('ASCII-8BIT') begin read_more = false has_skipped_line = false @@ -1211,7 +1211,7 @@ def handle_notify @start_reading_time ||= Fluent::Clock.now group_watcher&.update_reading_time(@path) - data = io.readpartial(BYTES_TO_READ, @iobuf) + data = io.readpartial(BYTES_TO_READ, iobuf) @eof = false @number_bytes_read += data.bytesize @fifo << data