Skip to content

Commit f969b7b

Browse files
committed
[Change] Replace Mutex with RwLock
1 parent 159e2b0 commit f969b7b

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/main.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use axum::{ routing::get, extract::{ State, Path }, http::StatusCode, response::
22
use libsql::{ Builder, Database };
33
use std::sync::{ Arc };
44
use std::vec::{ Vec };
5-
use tokio::sync::{ Mutex };
5+
use tokio::sync::{ RwLock };
66

77
#[shuttle_runtime::main]
88
async fn main(
@@ -30,7 +30,7 @@ async fn main(
3030

3131
// Add local db to shared state
3232
let app_state = Arc::new(AppState {
33-
db: Arc::new(Mutex::new(db)),
33+
db: Arc::new(RwLock::new(db)),
3434
remote_url: db_url,
3535
auth: auth,
3636
sync_auth: sync_auth
@@ -52,7 +52,7 @@ async fn main(
5252

5353
#[derive(Clone)]
5454
struct AppState {
55-
db: Arc<Mutex<Database>>,
55+
db: Arc<RwLock<Database>>,
5656
remote_url: String,
5757
auth: String,
5858
sync_auth: String
@@ -81,7 +81,7 @@ struct ModListEntry {
8181
async fn mod_list(
8282
State(state): State<Arc<AppState>>
8383
) -> Result<Json<Vec<ModListEntry>>, StatusCode> {
84-
let connection = match state.db.lock().await.connect() {
84+
let connection = match state.db.read().await.connect() {
8585
Ok(val) => val,
8686
Err(_err) => {
8787
println!("Connection failed");
@@ -141,7 +141,7 @@ async fn mod_data(
141141
State(state): State<Arc<AppState>>,
142142
Path(mod_name): Path<String>
143143
) -> Result<Json<ModEntry>, StatusCode> {
144-
let connection = match state.db.lock().await.connect() {
144+
let connection = match state.db.read().await.connect() {
145145
Ok(val) => val,
146146
Err(_err) => {
147147
println!("Connection failed");
@@ -208,7 +208,7 @@ async fn sync_local(
208208
return Redirect::to("/api");
209209
}
210210

211-
let conn = state.db.lock().await.connect().expect("Loacl connection failed");
211+
let conn = state.db.read().await.connect().expect("Local connection failed");
212212

213213
println!("Dropping old tables");
214214
conn.execute("DROP TABLE info", ()).await.expect("Drop 'info' failed");

0 commit comments

Comments
 (0)