Class: Fluent::Plugin::TailInput::TailWatcher::FIFO

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/in_tail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_encoding, encoding, log, max_line_size = nil) ⇒ FIFO

Returns a new instance of FIFO.



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/fluent/plugin/in_tail.rb', line 1009

def initialize(from_encoding, encoding, log, max_line_size=nil)
  @from_encoding = from_encoding
  @encoding = encoding
  @need_enc = from_encoding != encoding
  @buffer = ''.force_encoding(from_encoding)
  @eol = "\n".encode(from_encoding).freeze
  @max_line_size = max_line_size
  @skip_current_line = false
  @skipping_current_line_bytesize = 0
  @log = log
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



1021
1022
1023
# File 'lib/fluent/plugin/in_tail.rb', line 1021

def buffer
  @buffer
end

#encodingObject (readonly)

Returns the value of attribute encoding.



1021
1022
1023
# File 'lib/fluent/plugin/in_tail.rb', line 1021

def encoding
  @encoding
end

#from_encodingObject (readonly)

Returns the value of attribute from_encoding.



1021
1022
1023
# File 'lib/fluent/plugin/in_tail.rb', line 1021

def from_encoding
  @from_encoding
end

#max_line_sizeObject (readonly)

Returns the value of attribute max_line_size.



1021
1022
1023
# File 'lib/fluent/plugin/in_tail.rb', line 1021

def max_line_size
  @max_line_size
end

Instance Method Details

#<<(chunk) ⇒ Object



1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/fluent/plugin/in_tail.rb', line 1023

def <<(chunk)
  # Although "chunk" is most likely transient besides String#force_encoding itself
  # won't affect the actual content of it, it is also probable that "chunk" is
  # a reused buffer and changing its encoding causes some problems on the caller side.
  #
  # Actually, the caller here is specific and "chunk" comes from IO#partial with
  # the second argument, which the function always returns as a return value.
  #
  # Feeding a string that has its encoding attribute set to any double-byte or
  # quad-byte encoding to IO#readpartial as the second arguments results in an
  # assertion failure on Ruby < 2.4.0 for unknown reasons.
  orig_encoding = chunk.encoding
  chunk.force_encoding(from_encoding)
  @buffer << chunk
  # Thus the encoding needs to be reverted back here
  chunk.force_encoding(orig_encoding)
end

#convert(s) ⇒ Object



1041
1042
1043
1044
1045
1046
1047
1048
1049
# File 'lib/fluent/plugin/in_tail.rb', line 1041

def convert(s)
  if @need_enc
    s.encode!(@encoding, @from_encoding)
  else
    s
  end
rescue
  s.encode!(@encoding, @from_encoding, :invalid => :replace, :undef => :replace)
end

#read_lines(lines) ⇒ Object



1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'lib/fluent/plugin/in_tail.rb', line 1051

def read_lines(lines)
  idx = @buffer.index(@eol)
  has_skipped_line = false

  until idx.nil?
    # Using freeze and slice is faster than slice!
    # See https://github.com/fluent/fluentd/pull/2527
    @buffer.freeze
    rbuf = @buffer.slice(0, idx + 1)
    @buffer = @buffer.slice(idx + 1, @buffer.size)
    idx = @buffer.index(@eol)

    is_long_line = @max_line_size && (
      @skip_current_line || rbuf.bytesize > @max_line_size
    )

    if is_long_line
      @log.warn "received line length is longer than #{@max_line_size}"
      if @skip_current_line
        @log.debug("The continuing line is finished. Finally discarded data: ") { convert(rbuf).chomp }
      else
        @log.debug("skipped line: ") { convert(rbuf).chomp }
      end
      has_skipped_line = true
      @skip_current_line = false
      @skipping_current_line_bytesize = 0
      next
    end

    lines << convert(rbuf)
  end

  is_long_current_line = @max_line_size && (
    @skip_current_line || @buffer.bytesize > @max_line_size
  )

  if is_long_current_line
    @log.debug(
      "The continuing current line length is longer than #{@max_line_size}." +
      " The received data will be discarded until this line is finished." +
      " Discarded data: "
    ) { convert(@buffer).chomp }
    @skip_current_line = true
    @skipping_current_line_bytesize += @buffer.bytesize
    @buffer.clear
  end

  return has_skipped_line
end

#reading_bytesizeObject



1101
1102
1103
1104
# File 'lib/fluent/plugin/in_tail.rb', line 1101

def reading_bytesize
  return @skipping_current_line_bytesize if @skip_current_line
  @buffer.bytesize
end