Skip to content

Instantly share code, notes, and snippets.

@MarcoSero
Created September 27, 2012 19:11
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 MarcoSero/3795843 to your computer and use it in GitHub Desktop.
Save MarcoSero/3795843 to your computer and use it in GitHub Desktop.
A Ruby (multithreaded) Cloud App Roulette
require 'net/http'
require 'thread'
$o = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten;
$n_found = 0
$mutex = Mutex.new
def generate
generated = (0...4).map{ $o[rand($o.length)] }.join;
end
def thread_body
Net::HTTP.start('cl.ly', 80) do |http|
generated = generate
response = http.head("/" + generated)
if response.code == "200"
$mutex.synchronize do
$n_found += 1
end
puts "http://cl.ly/" + generated
end
end
end
found = false
i = 0
while !found && i < 64000
thread = Thread.new do
i += 1
thread_body
end
thread.run
$mutex.synchronize do
if $n_found >= 20
found = true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment