Skip to content

Commit

Permalink
Use slice
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 committed Jan 14, 2025
1 parent 26812c8 commit 0690657
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/fluent/compat/socket_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def on_read(data)
@callback.call(msg, @addr)
pos = i + @delimiter.length
end
@buffer.slice!(0, pos) if pos > 0
@buffer = @buffer.slice(pos..) if pos > 0
rescue => e
@log.error "unexpected error", error: e
close
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def start_tcp_server(tls: false)
message_handler(msg, conn)
end
end
buffer.slice!(0, pos) if pos > 0
buffer.slice(pos..) if pos > 0
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ def read_lines(lines)
@buffer.freeze
slice_position = idx + 1
rbuf = @buffer.slice(0, slice_position)
@buffer = @buffer.slice(slice_position, @buffer.size - slice_position)
@buffer = @buffer.slice(slice_position..)
idx = @buffer.index(@eol)

is_long_line = @max_line_size && (
Expand Down
10 changes: 8 additions & 2 deletions lib/fluent/plugin/in_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ def start
router.emit(tag, time, record)
end
end
buf.slice!(0, pos) if pos > 0
if pos > 0
buf = buf.slice(pos..)
conn.buffer = buf
end
# If the buffer size exceeds the limit here, it means that the next message will definitely exceed the limit.
# So we should clear the buffer here. Otherwise, it will keep storing useless data until the next delimiter comes.
if !@message_length_limit.nil? && @message_length_limit < buf.bytesize
Expand Down Expand Up @@ -198,7 +201,10 @@ def start
end
end
router.emit_stream(@tag, es)
buf.slice!(0, pos) if pos > 0
if pos > 0
buf = buf.slice(pos..)
conn.buffer = buf
end
# If the buffer size exceeds the limit here, it means that the next message will definitely exceed the limit.
# So we should clear the buffer here. Otherwise, it will keep storing useless data until the next delimiter comes.
if !@message_length_limit.nil? && @message_length_limit < buf.bytesize
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin_helper/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ def on_writable
@mutex.synchronize do
# Consider write_nonblock with {exception: false} when IO::WaitWritable error happens frequently.
written_bytes = @_handler_socket.write_nonblock(@_handler_write_buffer)
@_handler_write_buffer.slice!(0, written_bytes)
@_handler_write_buffer = @_handler_write_buffer.slice(written_bytes..)
end

# No need to call `super` in a synchronized context because TLSServer doesn't use the inner buffer(::IO::Buffer) of Coolio::IO.
Expand Down

0 comments on commit 0690657

Please sign in to comment.