From 4a996c8331ab96775c09d248d12f3cfc3eb5cbe6 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 17 Dec 2024 19:14:42 +0200 Subject: [PATCH] libsql: Add support for Connection::interrupt() Let's expose SQLite capability of interrupting long-running queries. --- libsql/src/connection.rs | 7 +++++++ libsql/src/hrana/hyper.rs | 10 ++++++++++ libsql/src/local/connection.rs | 5 +++++ libsql/src/local/impls.rs | 4 ++++ libsql/src/replication/connection.rs | 5 +++++ 5 files changed, 31 insertions(+) diff --git a/libsql/src/connection.rs b/libsql/src/connection.rs index 08966f5aad..61798c6305 100644 --- a/libsql/src/connection.rs +++ b/libsql/src/connection.rs @@ -21,6 +21,8 @@ pub(crate) trait Conn { async fn transaction(&self, tx_behavior: TransactionBehavior) -> Result; + fn interrupt(&self) -> Result<()>; + fn is_autocommit(&self) -> bool; fn changes(&self) -> u64; @@ -185,6 +187,11 @@ impl Connection { self.conn.transaction(tx_behavior).await } + /// Cancel ongoing operations and return at earliest opportunity. + pub fn interrupt(&self) -> Result<()> { + self.conn.interrupt() + } + /// Check weather libsql is in `autocommit` or not. pub fn is_autocommit(&self) -> bool { self.conn.is_autocommit() diff --git a/libsql/src/hrana/hyper.rs b/libsql/src/hrana/hyper.rs index 2f40586eeb..b02ce0ce23 100644 --- a/libsql/src/hrana/hyper.rs +++ b/libsql/src/hrana/hyper.rs @@ -163,6 +163,11 @@ impl Conn for HttpConnection { }) } + fn interrupt(&self) -> crate::Result<()> { + // Interrupt is a no-op for remote connections. + Ok(()) + } + fn is_autocommit(&self) -> bool { self.is_autocommit() } @@ -343,6 +348,11 @@ impl Conn for HranaStream { todo!("sounds like nested transactions innit?") } + fn interrupt(&self) -> crate::Result<()> { + // Interrupt is a no-op for remote connections. + Ok(()) + } + fn is_autocommit(&self) -> bool { false // for streams this method is callable only when we're within explicit transaction } diff --git a/libsql/src/local/connection.rs b/libsql/src/local/connection.rs index f88ddc789f..56ff743ee5 100644 --- a/libsql/src/local/connection.rs +++ b/libsql/src/local/connection.rs @@ -355,6 +355,11 @@ impl Connection { Transaction::begin(self.clone(), tx_behavior) } + pub fn interrupt(&self) -> Result<()> { + unsafe { ffi::sqlite3_interrupt(self.raw) }; + Ok(()) + } + pub fn is_autocommit(&self) -> bool { unsafe { ffi::sqlite3_get_autocommit(self.raw) != 0 } } diff --git a/libsql/src/local/impls.rs b/libsql/src/local/impls.rs index 2338317a34..f1f8a6d153 100644 --- a/libsql/src/local/impls.rs +++ b/libsql/src/local/impls.rs @@ -54,6 +54,10 @@ impl Conn for LibsqlConnection { }) } + fn interrupt(&self) -> Result<()> { + self.conn.interrupt() + } + fn is_autocommit(&self) -> bool { self.conn.is_autocommit() } diff --git a/libsql/src/replication/connection.rs b/libsql/src/replication/connection.rs index 593bd634a1..f87e2bc595 100644 --- a/libsql/src/replication/connection.rs +++ b/libsql/src/replication/connection.rs @@ -503,6 +503,11 @@ impl Conn for RemoteConnection { }) } + fn interrupt(&self) -> Result<()> { + // Interrupt is a no-op for remote connections. + Ok(()) + } + fn is_autocommit(&self) -> bool { self.is_state_init() }