Skip to content

Commit ad25f94

Browse files
committed
default to wss and fallback to http
1 parent 281daa7 commit ad25f94

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ pub unsafe extern "C" fn sqlite3_open_v2(
6767
let connection = get_tokio().block_on(transport::DatabaseConnection::open(
6868
db_name,
6969
Box::new(GlobeStrategy),
70-
transport::ActiveStrategy::Websocket,
7170
));
7271
if connection.is_err() {
7372
return SQLITE_CANTOPEN;

src/sqlite.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,21 @@ async fn execute_sql_and_params(
355355
sql: &str,
356356
params: Vec<serde_json::Value>,
357357
) -> Result<RemoteSqliteResponse, SqliteError> {
358-
let result = db
359-
.connection
360-
.send(db.connection.get_json_request(db, sql, params))
361-
.await;
358+
if let transport::ActiveStrategy::Websocket = db.connection.strategy {
359+
let mut request = db.connection.get_json_request(db, sql, &params);
360+
match db.connection.send(&mut request).await {
361+
Ok(response) => return Ok(response),
362+
Err(_) => {
363+
db.connection.strategy = transport::ActiveStrategy::Http;
364+
if cfg!(debug_assertions) {
365+
println!("WebSocket failed, retrying with HTTP...");
366+
}
367+
}
368+
}
369+
}
370+
371+
let request = &mut db.connection.get_json_request(db, sql, &params);
372+
let result = db.connection.send(request).await;
362373

363374
if let Err(e) = result {
364375
return Err(SqliteError::new(e.to_string(), Some(SQLITE_ERROR)));

src/transport/http.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl HttpStrategy {
2121

2222
impl LibsqlInterface for HttpStrategy {
2323
async fn get_transaction_baton(&mut self, sql: &str) -> Result<String, SqliteError> {
24-
let request = serde_json::json!({
24+
let mut request = serde_json::json!({
2525
"requests": [
2626
{
2727
"type": "execute",
@@ -31,7 +31,8 @@ impl LibsqlInterface for HttpStrategy {
3131
}
3232
]
3333
});
34-
let result = self.send(request).await;
34+
35+
let result = self.send(&mut request).await;
3536
if let Err(e) = result {
3637
return Err(SqliteError::new(
3738
format!("Failed to get transaction baton: {}", e),
@@ -49,7 +50,7 @@ impl LibsqlInterface for HttpStrategy {
4950

5051
async fn send(
5152
&mut self,
52-
request: serde_json::Value,
53+
request: &mut serde_json::Value,
5354
) -> Result<RemoteSqliteResponse, SqliteError> {
5455
const MAX_ATTEMPTS: usize = 5;
5556
let mut last_error = String::new();
@@ -162,7 +163,7 @@ impl LibsqlInterface for HttpStrategy {
162163
fn get_json_request(
163164
&self,
164165
sql: &str,
165-
params: Vec<serde_json::Value>,
166+
params: &Vec<serde_json::Value>,
166167
baton: Option<&String>,
167168
is_transacting: bool,
168169
) -> serde_json::Value {

src/transport/mod.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub trait LibsqlInterface {
5858
fn get_json_request(
5959
&self,
6060
sql: &str,
61-
params: Vec<serde_json::Value>,
61+
params: &Vec<serde_json::Value>,
6262
baton: Option<&String>,
6363
is_transacting: bool,
6464
) -> serde_json::Value;
@@ -67,7 +67,7 @@ pub trait LibsqlInterface {
6767

6868
async fn send(
6969
&mut self,
70-
request: serde_json::Value,
70+
request: &mut serde_json::Value,
7171
) -> Result<RemoteSqliteResponse, SqliteError>;
7272
}
7373

@@ -78,17 +78,13 @@ pub enum ActiveStrategy {
7878
}
7979

8080
pub struct DatabaseConnection {
81-
http: HttpStrategy,
82-
websocket: WebSocketStrategy,
81+
pub http: HttpStrategy,
82+
pub websocket: WebSocketStrategy,
8383
pub strategy: ActiveStrategy,
8484
}
8585

8686
impl DatabaseConnection {
87-
pub async fn open(
88-
db_name: &str,
89-
auth: Box<dyn DbAuthStrategy>,
90-
strategy: ActiveStrategy,
91-
) -> Result<Self, SqliteError> {
87+
pub async fn open(db_name: &str, auth: Box<dyn DbAuthStrategy>) -> Result<Self, SqliteError> {
9288
let reqwest_client = reqwest::Client::builder()
9389
.user_agent("libsqlite3_turso/1.0.0")
9490
.timeout(std::time::Duration::from_secs(30))
@@ -106,21 +102,16 @@ impl DatabaseConnection {
106102
let http = HttpStrategy::new(reqwest_client, turso_config.clone());
107103
let mut websocket = WebSocketStrategy::new(turso_config.clone());
108104

109-
if let ActiveStrategy::Websocket = strategy {
110-
websocket.connect().await?;
105+
websocket.connect().await?;
111106

112-
//wait 10 seconds for the server to respond
113-
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
114-
115-
if cfg!(debug_assertions) {
116-
println!("WebSocket connection established for {}", db_name);
117-
}
107+
if cfg!(debug_assertions) {
108+
println!("WebSocket connection established for {}", db_name);
118109
}
119110

120111
Ok(Self {
121112
http,
122113
websocket,
123-
strategy,
114+
strategy: ActiveStrategy::Websocket,
124115
})
125116
}
126117

@@ -133,19 +124,19 @@ impl DatabaseConnection {
133124

134125
pub async fn send(
135126
&mut self,
136-
request: serde_json::Value,
127+
mut request: &mut serde_json::Value,
137128
) -> Result<RemoteSqliteResponse, SqliteError> {
138129
match self.strategy {
139-
ActiveStrategy::Http => self.http.send(request).await,
140-
ActiveStrategy::Websocket => self.websocket.send(request).await,
130+
ActiveStrategy::Http => self.http.send(&mut request).await,
131+
ActiveStrategy::Websocket => self.websocket.send(&mut request).await,
141132
}
142133
}
143134

144135
pub fn get_json_request(
145136
&self,
146137
db: &SQLite3,
147138
sql: &str,
148-
params: Vec<serde_json::Value>,
139+
params: &Vec<serde_json::Value>,
149140
) -> serde_json::Value {
150141
let baton_str = {
151142
let baton = db.transaction_baton.lock().unwrap();

src/transport/wss.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl LibsqlInterface for WebSocketStrategy {
233233

234234
async fn send(
235235
&mut self,
236-
mut request: serde_json::Value,
236+
request: &mut serde_json::Value,
237237
) -> Result<RemoteSqliteResponse, SqliteError> {
238238
if let WebSocketConnState::Disconnected = *self.websocket_state.lock().await {
239239
return Err(SqliteError::new(
@@ -309,7 +309,7 @@ impl LibsqlInterface for WebSocketStrategy {
309309
fn get_json_request(
310310
&self,
311311
sql: &str,
312-
params: Vec<serde_json::Value>,
312+
params: &Vec<serde_json::Value>,
313313
baton: Option<&String>,
314314
is_transacting: bool,
315315
) -> serde_json::Value {

0 commit comments

Comments
 (0)