Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* Allow passing an array of hosts in client mode #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/logstash/outputs/tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LogStash::Outputs::Tcp < LogStash::Outputs::Base

# When mode is `server`, the address to listen on.
# When mode is `client`, the address to connect to.
config :host, :validate => :string, :required => true
config :host, :validate => :array, :required => true

# When mode is `server`, the port to listen on.
# When mode is `client`, the port to connect to.
Expand Down Expand Up @@ -71,7 +71,8 @@ def register
require "stud/try"
if server?
workers_not_supported

raise LogStash::ConfigurationError, "You cannot pass an array of hosts in server mode" if @host.is_a? Array and @host.count > 1
@host = @host.first
@logger.info("Starting tcp output listener", :address => "#{@host}:#{@port}")
@server_socket = TCPServer.new(@host, @port)
@client_threads = []
Expand Down Expand Up @@ -121,8 +122,17 @@ def register
private
def connect
Stud::try do
return TCPSocket.new(@host, @port)
@host.each do |shost|
begin
return TCPSocket.new(shost, @port)
rescue => e
@logger.warn("Connection failed", :host => @host, :port => @port,
:exception => e, :backtrace => e.backtrace)
end
end
end
@logger.error("All hosts unavailable", :hosts => @host, :port => @port)
raise StandardError, "Cannot connect - All hosts unavailable"
end # def connect

private
Expand Down