Created
April 18, 2012 19:29
-
-
Save jstorimer/2415944 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
require 'socket' | |
require 'kgio' | |
socket = Kgio::TCPServer.new '127.0.0.1', 4481 | |
concurrency = 3 | |
child_pids = [] | |
sig_queue = [] | |
concurrency.times do | |
child_pids << fork do | |
trap(:INT) { exit } | |
# heartbeat timestamp | |
loop do | |
connection = socket.kgio_tryaccept | |
if connection | |
connection.write connection.read | |
connection.close | |
end | |
end | |
end | |
end | |
trap(:CHLD) do | |
sig_queue << :CHLD | |
end | |
trap(:INT) do | |
sig_queue << :INT | |
end | |
loop do | |
case sig_queue.shift | |
when :INT | |
child_pids.each do |cpid| | |
Process.kill(:INT, cpid) | |
sleep 0.5 | |
end | |
exit | |
when :CHLD | |
puts 'child terminated unexpectedly' | |
end | |
end | |
#Process.waitall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment