Skip to content

Instantly share code, notes, and snippets.

@CGA1123
Created October 22, 2021 18:50
Show Gist options
  • Save CGA1123/e5ef2874abb11e5adfdd4c02e10332da to your computer and use it in GitHub Desktop.
Save CGA1123/e5ef2874abb11e5adfdd4c02e10332da to your computer and use it in GitHub Desktop.
def query(conn, sql, timeout_ms:)
conn.send_query(sql)
if conn.block(timeout_ms / 1000.0) # seconds
conn.get_last_result
else
conn.cancel
conn.get_last_result
end
end
require "pg"
conn = PG::Connection.new
query(conn, "SELECT 1", timeout: 1)
query(conn, "SELECT 1", timeout: 1)
begin
query(conn, "SELECT pg_sleep(2)", timeout: 1)
rescue PG::QueryCanceled
puts "cancelled"
end
begin
query(conn, "SELECT pg_sleep(2)", timeout: 1)
rescue PG::QueryCanceled
puts "cancelled"
end
query(conn, "SELECT 1", timeout: 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment