Skip to content

Commit

Permalink
Add require_ack_response option
Browse files Browse the repository at this point in the history
  • Loading branch information
okkez committed Mar 8, 2016
1 parent 8d8de10 commit d9eab0f
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions lib/fluent/logger/fluent_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def initialize(tag_prefix = nil, *args)
@host = options[:host]
@port = options[:port]

@require_ack_response = options[:require_ack_response]
@ack_response_timeout = options[:ack_response_timeout] || 190

@mon = Monitor.new
@pending = nil
@connect_error_history = []
Expand Down Expand Up @@ -108,7 +111,7 @@ def close
if @pending
begin
@pending.each do |tag, record|
send_data([tag, record].to_msgpack)
send_data(tag, record)
end
rescue => e
set_last_error(e)
Expand Down Expand Up @@ -171,7 +174,7 @@ def write(tag, time, map)

begin
@pending.each do |tag, record|
send_data([tag, record].to_msgpack)
send_data(tag, record)
end
@pending = nil
true
Expand All @@ -189,11 +192,17 @@ def write(tag, time, map)
}
end

def send_data(data)
def send_data(tag, record)
unless connect?
connect!
end
@con.write data
if @require_ack_response
option = {}
option['chunk'] = generate_chunk
@con.write [tag, record, option].to_msgpack
else
@con.write [tag, record].to_msgpack
end
#while true
# puts "sending #{data.length} bytes"
# if data.length > 32*1024
Expand All @@ -208,6 +217,21 @@ def send_data(data)
# data = data[n..-1]
#end

if @require_ack_response && @ack_response_timeout > 0
if IO.select([@con], nil, nil, @ack_response_timeout)
raw_data = @con.recv(1024)

if raw_data.empty?
raise "Closed connection"
else
response = MessagePack.unpack(raw_data)
if response['ack'] != option['chunk']
raise "ack in response and chunk id in sent data are different"
end
end
end
end

true
end

Expand Down Expand Up @@ -246,6 +270,10 @@ def set_last_error(e)
# TODO: Check non GVL env
@last_error[Thread.current.object_id] = e
end

def generate_chunk
Base64.encode64(([SecureRandom.random_number(1 << 32)] * 4).pack('NNNN')).chomp
end
end
end
end

0 comments on commit d9eab0f

Please sign in to comment.