We use Ruby to check domain availability, but the code runs painfully slow. We caching the Whois object across requests would accelerate performance, but other posts on SO suggest this would only marginally increase performance, if at all.
The code is so simple. We’re not sure if other improvements could be made, or if we’re just stuck with slow lookups because of the Whois object.
The bulk_check method accepts an array of domains to look up. Everything else is fairly self-explanatory.
We’re on Rails 3.
Any suggestions?
Thanks!
def bulk_check
domains = params[:domains] || '[]'
callback = params[:callback] || ''
results = []
threads = []
# String -> Array
domains = ActiveSupport::JSON.decode domains
# Iterate over each domain and spawn new thread to check domain status
domains.each do |d|
threads << Thread.new(d) { |my_domain|
Thread.current['domain'] = my_domain
Thread.current['status'] = get_domain_status my_domain
}
end
# Wait for threads to finish and update results array
threads.each do |t|
t.join
results.push [ t['domain'], t['status'] ]
end
# Render results
respond_to do |type|
type.json { render :json => { :results => results }.to_json, :callback => callback }
end
end
def get_domain_status domain
begin
# Create Whois object
whois = Whois::Client.new
# Query Whois for domain data
result = whois.query domain
# Prep JSON response
status = result.available? ? 'available' : 'taken'
rescue Exception => e
puts "Exception in parsing '#{domain}' status: #{e.message}"
status = 'error'
end
# Return status
return status
end
Since this is not really a CPU intensive operation, but rather a “network intensive” – consider checking multiple domains simultaneously using Ruby threads or fibers.
For example, Celluloid gem makes it nice and easy: https://github.com/celluloid/celluloid/