Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/fluent/plugin/in_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ def start_tcp_server(tls: false)
# The received data is binary (ASCII-8BIT), so match the delimiter as bytes.
delimiter = octet_count_frame ? " " : @delimiter.b
delimiter_size = delimiter.bytesize
# A partial delimiter does not count toward the message length limit.
buffer_limit = @message_length_limit + delimiter_size - 1
server_create_connection(
tls ? :in_syslog_tls_server : :in_syslog_tcp_server, @port,
bind: @bind,
Expand Down Expand Up @@ -273,10 +275,8 @@ def start_tcp_server(tls: false)
end
buffer.slice!(0, pos) if pos > 0

# 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 buffer.bytesize > @message_length_limit
# Discard an oversized message without waiting for its delimiter.
if buffer.bytesize > buffer_limit
# The rest of a long message fills the buffer again on each read, so log only the first clear.
unless discard_till_next_delimiter
log.info "The buffer size exceeds 'message_length_limit', cleared:", limit: @message_length_limit, size: buffer.bytesize
Expand Down
12 changes: 6 additions & 6 deletions lib/fluent/plugin/in_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def start
# The received data is binary (ASCII-8BIT), so match the delimiter as bytes.
delimiter = @delimiter.b
del_size = delimiter.bytesize
# A partial delimiter does not count toward the message length limit.
buffer_limit = @message_length_limit + del_size - 1 if @message_length_limit
if @_extract_enabled && @_extract_tag_key
server_create_connection(:in_tcp_server_single_emit, @port, bind: @bind, resolve_name: !!@source_hostname_key, send_keepalive_packet: @send_keepalive_packet) do |conn|
discard_till_next_delimiter = false
Expand Down Expand Up @@ -157,9 +159,8 @@ def start
end
end
buf.slice!(0, pos) if pos > 0
# 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
# Discard an oversized message without waiting for its delimiter.
if buffer_limit && buf.bytesize > buffer_limit
# The rest of a long message fills the buffer again on each read, so log only the first clear.
unless discard_till_next_delimiter
log.info "The buffer size exceeds 'message_length_limit', cleared:", limit: @message_length_limit, size: buf.bytesize
Expand Down Expand Up @@ -215,9 +216,8 @@ def start
end
router.emit_stream(@tag, es)
buf.slice!(0, pos) if pos > 0
# 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
# Discard an oversized message without waiting for its delimiter.
if buffer_limit && buf.bytesize > buffer_limit
# The rest of a long message fills the buffer again on each read, so log only the first clear.
unless discard_till_next_delimiter
log.info "The buffer size exceeds 'message_length_limit', cleared:", limit: @message_length_limit, size: buf.bytesize
Expand Down
28 changes: 28 additions & 0 deletions test/plugin/test_in_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,34 @@ def test_default_message_length_limit
)
end

test 'traditional: accept a message at the limit when the delimiter is split' do
port = unused_port(protocol: :tcp)
at_limit = '<6>Sep 10 00:00:00 localhost logger: at-limit'
d = create_driver([
ipv4_config(port),
"<transport tcp>\n</transport>",
'delimiter "\\r\\n"',
"message_length_limit #{at_limit.bytesize}",
].join("\n"))

d.run(expect_emits: 1, timeout: 20) do
TCPSocket.open('127.0.0.1', port) do |sock|
sock.write(at_limit + "\r")
# Wait for the prefix to reach the receive callback before sending LF.
waiting(10) do
connections = d.instance.instance_variable_get(:@_server_connections)
until connections.first&.instance_variable_get(:@callback_connection)&.buffer&.end_with?("\r")
sleep 0.01
end
end
sock.write("\n#{NORMAL_MESSAGE}\r\n")
waiting(10) { sleep 0.01 until d.events.any? { |event| event[2]["message"] == "hello" } }
end
end

assert_equal(["at-limit", "hello"], d.events.map { |event| event[2]["message"] })
end

test 'traditional: a message split into multiple chunks is reassembled' do
port = unused_port(protocol: :tcp)
d = create_tcp_driver(port)
Expand Down
30 changes: 30 additions & 0 deletions test/plugin/test_in_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,36 @@ def create_tcp_socket(host, port, &block)
)
end

test "accept a message at the limit when the delimiter is split" do |data|
payload = '{"k":"at-limit"}'
d = create_driver(base_config + %!
bind 127.0.0.1
message_length_limit #{payload.bytesize}
delimiter "\\r\\n"
<parse>
@type json
</parse>
#{data[:extract]}
!)

d.run(expect_records: 1, timeout: 20) do
create_tcp_socket('127.0.0.1', @port) do |sock|
sock.write(payload + "\r")
# Wait for the prefix to reach the receive callback before sending LF.
waiting(10) do
connections = d.instance.instance_variable_get(:@_server_connections)
until connections.first&.instance_variable_get(:@callback_connection)&.buffer&.end_with?("\r")
sleep 0.01
end
end
sock.write("\n{\"k\":\"after\"}\r\n")
waiting(10) { sleep 0.01 until d.events.any? { |event| event[2]["k"] == "after" } }
end
end

assert_equal(["at-limit", "after"], d.events.map { |event| event[2]["k"] })
end

test "do not cut a multi-byte delimiter when clearing the buffer" do |data|
message_length_limit = 12
d = create_driver(base_config + %!
Expand Down
Loading