Class: Fluent::ForwardInput::Handler

Inherits:
Coolio::Socket
  • Object
show all
Defined in:
lib/fluent/plugin/in_forward.rb

Instance Method Summary collapse

Constructor Details

#initialize(io, on_message) ⇒ Handler

Returns a new instance of Handler.



141
142
143
144
145
146
147
148
149
# File 'lib/fluent/plugin/in_forward.rb', line 141

def initialize(io, on_message)
  super(io)
  if io.is_a?(TCPSocket)
    opt = [1, @timeout.to_i].pack('I!I!')  # { int l_onoff; int l_linger; }
    io.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, opt)
  end
  $log.trace { "accepted fluent socket object_id=#{self.object_id}" }
  @on_message = on_message
end

Instance Method Details

#on_closeObject



187
188
189
# File 'lib/fluent/plugin/in_forward.rb', line 187

def on_close
  $log.trace { "closed fluent socket object_id=#{self.object_id}" }
end

#on_connectObject



151
152
# File 'lib/fluent/plugin/in_forward.rb', line 151

def on_connect
end

#on_read(data) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fluent/plugin/in_forward.rb', line 154

def on_read(data)
  first = data[0]
  if first == '{' || first == '['
    m = method(:on_read_json)
    @y = Yajl::Parser.new
    @y.on_parse_complete = @on_message
  else
    m = method(:on_read_msgpack)
    @u = MessagePack::Unpacker.new
  end

  (class<<self;self;end).module_eval do
    define_method(:on_read, m)
  end
  m.call(data)
end

#on_read_json(data) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/fluent/plugin/in_forward.rb', line 171

def on_read_json(data)
  @y << data
rescue
  $log.error "forward error: #{$!.to_s}"
  $log.error_backtrace
  close
end

#on_read_msgpack(data) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/fluent/plugin/in_forward.rb', line 179

def on_read_msgpack(data)
  @u.feed_each(data, &@on_message)
rescue
  $log.error "forward error: #{$!.to_s}"
  $log.error_backtrace
  close
end