-
Notifications
You must be signed in to change notification settings - Fork 31
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
Synchronize access to @client_threads in LogStash::Outputs::Tcp #2
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,23 +75,32 @@ def register | |
@logger.info("Starting tcp output listener", :address => "#{@host}:#{@port}") | ||
@server_socket = TCPServer.new(@host, @port) | ||
@client_threads = [] | ||
@client_threads_lock = Mutex.new | ||
|
||
@accept_thread = Thread.new(@server_socket) do |server_socket| | ||
loop do | ||
client_thread = Thread.start(server_socket.accept) do |client_socket| | ||
client = Client.new(client_socket, @logger) | ||
Thread.current[:client] = client | ||
client.run | ||
@client_threads_lock.synchronize do | ||
@client_threads << Thread.start(server_socket.accept) do |client_socket| | ||
begin | ||
client = Client.new(client_socket, @logger) | ||
Thread.current[:client] = client | ||
client.run | ||
ensure | ||
@client_threads_lock.synchronize do | ||
@client_threads.delete(Thread.current) | ||
end | ||
end | ||
end | ||
end | ||
@client_threads << client_thread | ||
end | ||
end | ||
|
||
@codec.on_event do |payload| | ||
@client_threads.each do |client_thread| | ||
client_thread[:client].write(payload) | ||
# dup @client_threads to avoid holding the lock while writing to clients | ||
threads = @client_threads_lock.synchronize { @client_threads.dup } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a good idea but thinking about it, what is not thread-safe here is the array updating and not the ivar setting. in this case, the code that is executed the most is this maybe we could instead move all this in the client_treat stuff where instead of directly adding in the @client_threads = @client_threads + [client_thread] since setting an ivar is threadsafe, then in the on_event we could just do a |
||
threads.each do |thread| | ||
thread[:client].write(payload) | ||
end | ||
@client_threads.reject! {|t| !t.alive? } | ||
end | ||
else | ||
client_socket = nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of doing a "long" lock, I'd suggest keeping the
client_thread
var usage and simply do a short lock to update the@client_threads
ivar like that