-
-
Save authorNari/4158509 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $ ruby a.rb | |
# $ nc localhost 8899 | |
# $ pkill -TERM -f 'ruby this.rb' | |
require 'monitor' | |
require 'socket' | |
@monitor = Monitor.new | |
@threads = {} | |
Signal.trap("TERM") do | |
puts "trapped SIGTERM" | |
@monitor.synchronize do | |
puts "safe termination..." | |
@threads.values.each(&:join) | |
puts "...done" | |
exit | |
end | |
end | |
server = TCPServer.new(8899) | |
loop do | |
@monitor.synchronize do | |
th = Thread.new(server.accept) do |client| | |
client.puts "Hell" | |
sleep 20 | |
client.puts "World..." | |
client.close | |
@threads.delete(Thread.current) | |
end | |
@threads[th] = th | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment