Skip to content

Instantly share code, notes, and snippets.

@adamhooper
Last active July 31, 2016 17:29
Show Gist options
  • Save adamhooper/6a0752798fe0cd2922aa914f2c329198 to your computer and use it in GitHub Desktop.
Save adamhooper/6a0752798fe0cd2922aa914f2c329198 to your computer and use it in GitHub Desktop.
2016-07-31 Timeout::timeout() buggy code
require 'net/http'
require 'timeout'
require 'mysql2' # https://github.com/brianmario/mysql2
def download_to_database(url, sql_statement)
Timeout::timeout(5) do # BUG. Never do this.
res = Net::HTTP.get_response(url)
sql_statement.execute(url.to_s, res.code, res.body)
end
end
client = Mysql2::Client.new(...)
statement = client.prepare(
"INSERT INTO http_responses (url, status_code, body) VALUES (?, ?, ?)"
)
download_to_database(URI('http://example.com'), statement)
download_to_database(URI('http://example.org'), statement)
# ... et cetera
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment