lib/fluent/plugin/output_node.rb in fluent-plugin-secure-forward-0.2.0 vs lib/fluent/plugin/output_node.rb in fluent-plugin-secure-forward-0.2.1
- old
+ new
@@ -57,10 +57,12 @@
renewed
end
def start
@thread = Thread.new(&method(:connect))
+ ## If you want to check code bug, turn this line enable
+ # @thread.abort_on_exception = true
end
def shutdown
log.debug "shutting down node #{@host}"
@state = :closed
@@ -187,11 +189,17 @@
def connect
log.debug "starting client"
addr = @sender.hostname_resolver.getaddress(@host)
log.debug "create tcp socket to node", :host => @host, :address => addr, :port => @port
- sock = TCPSocket.new(addr, @port)
+ begin
+ sock = TCPSocket.new(addr, @port)
+ rescue => e
+ log.warn "failed to connect for secure-forward", :error_class => e.class, :error => e, :host => @host, :address => addr, :port => @port
+ @state = :failed
+ return
+ end
log.trace "changing socket options"
opt = [1, @sender.send_timeout.to_i].pack('I!I!') # { int l_onoff; int l_linger; }
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, opt)
@@ -202,13 +210,21 @@
log.trace "initializing SSL contexts"
context = OpenSSL::SSL::SSLContext.new
# TODO: context.ca_file = (ca_file_path)
# TODO: context.ciphers = (SSL Shared key chiper protocols)
- log.debug "trying to connect ssl session", :host => @host, :ipaddr => addr, :port => @port
- sslsession = OpenSSL::SSL::SSLSocket.new(sock, context)
- # TODO: check connection failure
- sslsession.connect
+ log.debug "trying to connect ssl session", :host => @host, :address => addr, :port => @port
+ begin
+ sslsession = OpenSSL::SSL::SSLSocket.new(sock, context)
+ rescue => e
+ log.warn "failed to establish SSL connection", :host => @host, :address => addr, :port => @port
+ end
+
+ unless sslsession.connect
+ log.debug "failed to connect", :host => @host, :address => addr, :port => @port
+ @state = :failed
+ return
+ end
log.debug "ssl session connected", :host => @host, :port => @port
begin
unless @sender.allow_self_signed_certificate
log.debug "checking peer's certificate", :subject => sslsession.peer_cert.subject