Skip to content

Commit

Permalink
Merge pull request #1885 from tursodatabase/interrupt
Browse files Browse the repository at this point in the history
libsql: Add support for Connection::interrupt()
  • Loading branch information
penberg authored Dec 17, 2024
2 parents b8f5e89 + 4a996c8 commit 58b016a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libsql/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub(crate) trait Conn {

async fn transaction(&self, tx_behavior: TransactionBehavior) -> Result<Transaction>;

fn interrupt(&self) -> Result<()>;

fn is_autocommit(&self) -> bool;

fn changes(&self) -> u64;
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions libsql/src/hrana/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ impl Conn for HttpConnection<HttpSender> {
})
}

fn interrupt(&self) -> crate::Result<()> {
// Interrupt is a no-op for remote connections.
Ok(())
}

fn is_autocommit(&self) -> bool {
self.is_autocommit()
}
Expand Down Expand Up @@ -343,6 +348,11 @@ impl Conn for HranaStream<HttpSender> {
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
}
Expand Down
5 changes: 5 additions & 0 deletions libsql/src/local/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
4 changes: 4 additions & 0 deletions libsql/src/local/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl Conn for LibsqlConnection {
})
}

fn interrupt(&self) -> Result<()> {
self.conn.interrupt()
}

fn is_autocommit(&self) -> bool {
self.conn.is_autocommit()
}
Expand Down
5 changes: 5 additions & 0 deletions libsql/src/replication/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 58b016a

Please sign in to comment.