Skip to content

Instantly share code, notes, and snippets.

@sorah
Created January 6, 2016 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorah/caf793880f6b3ff9e648 to your computer and use it in GitHub Desktop.
Save sorah/caf793880f6b3ff9e648 to your computer and use it in GitHub Desktop.
require 'socket'
require 'thread'
@queue = Queue.new
def log(*args)
@queue << args
end
th = 500.times.map do |i|
Thread.new(i) do |n|
sleep 0.1 * n
log "[#{i}] START"
begin
connclose = false
TCPSocket.open('127.0.0.1', 80) do |sock|
120.times do |try|
#log "[#{n}] ===== #{try}"
req = "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
#log "[#{n}] > #{req.inspect}"
sock.write req
header = false
until !(line = sock.gets) || line.end_with?("required.</p>\n")
header = true if line.chomp.chomp.empty?
connclose = true if line.match(/Connection: close/i)
unless header
# log "[#{n}] < #{line.inspect}"
end
end
log "[#{n}] !!!!!!!!!!!!!!!!!!!! Connection: close @ #{try}" if connclose
# log "[#{n}] < #{line.inspect}"
# log "[#{n}] ========="
sleep 1
end
end
rescue Interrupt, SignalException
next
rescue Errno::EPIPE, Errno::ECONNRESET => e
$stderr.puts "[#{n}] #{e.inspect} (connclose: #{connclose.inspect})" unless connclose
rescue Exception => e
$stderr.puts "[#{n}] #{e.inspect}"
e.backtrace.each do |l|
$stderr.puts "[#{n}] #{l}"
end
end
end
end
while args = @queue.pop
puts *args
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment