Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Reuse shared Tokio runtime and fix favourites queries

Store Arc<tokio::runtime::Runtime> in Context and pass it into
handlers so they call context.rt.block_on instead of creating a
new Runtime per request. Adjust favourites SQL to select specific
table columns and order by favourites.created_at, and replace
eprintln! with tracing::error!

+14 -8
+5 -5
crates/library/src/repo/favourites.rs
··· 43 43 pub async fn all_tracks(pool: Pool<Sqlite>) -> Result<Vec<Track>, sqlx::Error> { 44 44 match sqlx::query_as::<_, Track>( 45 45 r#" 46 - SELECT * FROM favourites LEFT JOIN track ON favourites.track_id = track.id WHERE favourites.track_id IS NOT NULL 47 - ORDER BY created_at DESC 46 + SELECT track.* FROM favourites LEFT JOIN track ON favourites.track_id = track.id WHERE favourites.track_id IS NOT NULL 47 + ORDER BY favourites.created_at DESC 48 48 "#, 49 49 ) 50 50 .fetch_all(&pool) ··· 52 52 { 53 53 Ok(favourites) => Ok(favourites), 54 54 Err(e) => { 55 - eprintln!("Error fetching favourites: {:?}", e); 55 + tracing::error!("Error fetching favourites: {:?}", e); 56 56 Err(e) 57 57 } 58 58 } ··· 61 61 pub async fn all_albums(pool: Pool<Sqlite>) -> Result<Vec<Album>, sqlx::Error> { 62 62 match sqlx::query_as::<_, Album>( 63 63 r#" 64 - SELECT * FROM favourites LEFT JOIN album ON favourites.album_id = album.id WHERE favourites.album_id IS NOT NULL ORDER BY created_at DESC 64 + SELECT album.* FROM favourites LEFT JOIN album ON favourites.album_id = album.id WHERE favourites.album_id IS NOT NULL ORDER BY favourites.created_at DESC 65 65 "#, 66 66 ) 67 67 .fetch_all(&pool) ··· 69 69 { 70 70 Ok(favourites) => Ok(favourites), 71 71 Err(e) => { 72 - eprintln!("Error fetching favourites: {:?}", e); 72 + tracing::error!("Error fetching favourites: {:?}", e); 73 73 Err(e) 74 74 } 75 75 }
+3 -2
crates/server/src/handlers/mod.rs
··· 24 24 request: &Request, 25 25 response: &mut Response, 26 26 ) -> Result<(), Error> { 27 - let rt = tokio::runtime::Runtime::new().unwrap(); 28 - rt.block_on($module::$handler(context, request, response))?; 27 + context 28 + .rt 29 + .block_on($module::$handler(context, request, response))?; 29 30 Ok(()) 30 31 } 31 32 };
+6 -1
crates/server/src/http.rs
··· 34 34 35 35 pub struct Context { 36 36 pub pool: sqlx::Pool<Sqlite>, 37 + pub rt: Arc<tokio::runtime::Runtime>, 37 38 pub fs_cache: Arc<tokio::sync::Mutex<HashMap<String, Vec<Entry>>>>, 38 39 pub metadata_cache: Arc<tokio::sync::Mutex<HashMap<String, Mp3Entry>>>, 39 40 pub devices: Arc<Mutex<Vec<Device>>>, ··· 247 248 248 249 let pool = ThreadPool::new(4); 249 250 let active_connections = Arc::new(Mutex::new(0)); 250 - let rt = tokio::runtime::Runtime::new()?; 251 + let rt = Arc::new(tokio::runtime::Runtime::new()?); 251 252 let db_pool = rt.block_on(rockbox_library::create_connection_pool())?; 252 253 let fs_cache = Arc::new(tokio::sync::Mutex::new(HashMap::new())); 253 254 let metadata_cache = Arc::new(tokio::sync::Mutex::new(HashMap::new())); ··· 386 387 *active_connections += 1; 387 388 } 388 389 let mut cloned_self = self.clone(); 390 + let cloned_rt = rt.clone(); 389 391 let cloned_fs_cache = fs_cache.clone(); 390 392 let cloned_metadata_cache = metadata_cache.clone(); 391 393 let cloned_devices = devices.clone(); ··· 459 461 stream, 460 462 db_pool, 461 463 req_body, 464 + cloned_rt, 462 465 cloned_fs_cache, 463 466 cloned_metadata_cache, 464 467 cloned_devices, ··· 507 510 mut stream: TcpStream, 508 511 pool: sqlx::Pool<Sqlite>, 509 512 body: Option<String>, 513 + rt: Arc<tokio::runtime::Runtime>, 510 514 fs_cache: Arc<tokio::sync::Mutex<HashMap<String, Vec<Entry>>>>, 511 515 metadata_cache: Arc<tokio::sync::Mutex<HashMap<String, Mp3Entry>>>, 512 516 devices: Arc<Mutex<Vec<Device>>>, ··· 521 525 let mut response = Response::new(); 522 526 let context = Context { 523 527 pool, 528 + rt, 524 529 fs_cache, 525 530 metadata_cache, 526 531 devices,