Created
October 22, 2021 18:50
-
-
Save CGA1123/e5ef2874abb11e5adfdd4c02e10332da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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