Skip to content

Commit

Permalink
Merge pull request elastic#657 from logstash/tcp-output-client-close-fix
Browse files Browse the repository at this point in the history
Tcp output client remote-has-closed fix
  • Loading branch information
nickethier committed Sep 26, 2013
2 parents 0a70575 + f168711 commit 3ac1244
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/logstash/outputs/tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require "logstash/namespace"
require "thread"


# Write events over a TCP socket.
#
# Each event json is separated by a newline.
Expand Down Expand Up @@ -68,6 +67,7 @@ def write(msg)

public
def register
require "stud/try"
if server?
@logger.info("Starting tcp output listener", :address => "#{@host}:#{@port}")
@server_socket = TCPServer.new(@host, @port)
Expand All @@ -91,16 +91,24 @@ def register
@client_threads.reject! {|t| !t.alive? }
end
else
@client_socket = nil
client_socket = nil
@codec.on_event do |payload|
begin
connect unless @client_socket
@client_socket.write(payload)
client_socket = connect unless client_socket
r,w,e = IO.select([client_socket], [client_socket], [client_socket], nil)
# don't expect any reads, but a readable socket might
# mean the remote end closed, so read it and throw it away.
# we'll get an EOFError if it happens.
client_socket.sysread(16384) if r.any?

# Now send the payload
client_socket.syswrite(payload) if w.any?
rescue => e
@logger.warn("tcp output exception", :host => @host, :port => @port,
:exception => e, :backtrace => e.backtrace)
client_socket.close rescue nil
client_socket = nil
sleep @reconnect_interval
@client_socket = nil
retry
end
end
Expand All @@ -109,7 +117,9 @@ def register

private
def connect
@client_socket = TCPSocket.new(@host, @port)
Stud::try do
return TCPSocket.new(@host, @port)
end
end # def connect

private
Expand Down

0 comments on commit 3ac1244

Please sign in to comment.