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.

Merge pull request #13 from tsirysndr/thread-workers

use 4 worker threads to handle concurrent TCP connections

authored by

Tsiry Sandratraina and committed by
GitHub
afc4bc40 129409e4

+37 -1
+10
Cargo.lock
··· 5692 5692 "rockbox-rpc", 5693 5693 "rockbox-sys", 5694 5694 "serde_json", 5695 + "threadpool", 5695 5696 "tokio", 5696 5697 ] 5697 5698 ··· 7137 7138 "proc-macro2", 7138 7139 "quote", 7139 7140 "syn 2.0.77", 7141 + ] 7142 + 7143 + [[package]] 7144 + name = "threadpool" 7145 + version = "1.8.1" 7146 + source = "registry+https://github.com/rust-lang/crates.io-index" 7147 + checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" 7148 + dependencies = [ 7149 + "num_cpus", 7140 7150 ] 7141 7151 7142 7152 [[package]]
+1
crates/server/Cargo.toml
··· 14 14 rockbox-rpc = {path = "../rpc"} 15 15 rockbox-sys = {path = "../sys"} 16 16 serde_json = "1.0.128" 17 + threadpool = "1.8.1" 17 18 tokio = {version = "1.36.0", features = ["full"]}
+26 -1
crates/server/src/lib.rs
··· 6 6 net::{TcpListener, TcpStream}, 7 7 sync::{Arc, Mutex}, 8 8 thread, 9 + time::Duration, 9 10 }; 11 + use threadpool::ThreadPool; 10 12 11 13 #[no_mangle] 12 14 pub extern "C" fn debugfn(args: *const c_char) { ··· 39 41 addr.bright_green() 40 42 ); 41 43 44 + let pool = ThreadPool::new(4); 45 + let active_connections = Arc::new(Mutex::new(0)); 46 + 42 47 loop { 43 48 match listener.accept() { 44 49 Ok((stream, _)) => { 45 - handle_connection(stream); 50 + let active_connections = Arc::clone(&active_connections); 51 + { 52 + let mut active_connections = active_connections.lock().unwrap(); 53 + *active_connections += 1; 54 + } 55 + pool.execute(move || { 56 + handle_connection(stream); 57 + { 58 + let mut active_connections = active_connections.lock().unwrap(); 59 + *active_connections -= 1; 60 + } 61 + }); 46 62 } 47 63 Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => { 48 64 // No incoming connection, just sleep and retry ··· 53 69 break; 54 70 } 55 71 } 72 + 73 + // Check if there are no active connections (idle state) 74 + let active = *active_connections.lock().unwrap(); 75 + if active == 0 { 76 + rb::system::sleep(rb::HZ); 77 + } 78 + 79 + // Add a small sleep to avoid tight looping when idle 80 + thread::sleep(Duration::from_millis(100)); 56 81 } 57 82 } 58 83
docs/rockbox-server-architecture.jpg

This is a binary file and will not be displayed.