Execute the whole request in a thread
This commit is contained in:
parent
34b8307426
commit
64e0637349
63
src/main.rs
63
src/main.rs
@ -28,7 +28,7 @@ fn main() {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut context = Context::default();
|
||||
let context: Arc<Mutex<Context>> = Arc::default();
|
||||
let mut pool = ThreadPool::new(4);
|
||||
|
||||
let mut args = env::args();
|
||||
@ -52,7 +52,9 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
handle_connection(&mut context, &path, stream, &pool);
|
||||
let context = context.clone();
|
||||
let path = path.clone();
|
||||
pool.execute(move || handle_connection(context, path, stream));
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,7 +135,7 @@ struct Context {
|
||||
infos: HashMap<Box<str>, Arc<Mutex<SiteInfo>>>,
|
||||
}
|
||||
|
||||
fn handle_connection(context: &mut Context, path: &Path, mut stream: TcpStream, pool: &ThreadPool) {
|
||||
fn handle_connection(context: Arc<Mutex<Context>>, path: PathBuf, mut stream: TcpStream) {
|
||||
let Some(request) = Request::from(&stream) else {
|
||||
eprintln!("Invalid request!");
|
||||
return;
|
||||
@ -193,7 +195,7 @@ fn handle_connection(context: &mut Context, path: &Path, mut stream: TcpStream,
|
||||
|
||||
if relative_path.split('/').any(|name| name == "Images") {
|
||||
let path = path.to_path_buf();
|
||||
pool.execute(move || reply_image(stream, &relative_path, path));
|
||||
reply_image(stream, &relative_path, path);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -209,34 +211,37 @@ fn handle_connection(context: &mut Context, path: &Path, mut stream: TcpStream,
|
||||
}
|
||||
}
|
||||
|
||||
use Entry::*;
|
||||
let info = match context.infos.entry(relative_path.clone().into_boxed_str()) {
|
||||
Occupied(o) => o.get().clone(),
|
||||
Vacant(v) => v
|
||||
.insert(Arc::new(Mutex::new(
|
||||
File::open(&data_path)
|
||||
.map(|mut file| {
|
||||
from_stream::<PortableSettings, _, _>(&mut file).unwrap_or_default()
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
)))
|
||||
.clone(),
|
||||
let info = if let Ok(mut context) = context.lock() {
|
||||
use Entry::*;
|
||||
match context.infos.entry(relative_path.clone().into_boxed_str()) {
|
||||
Occupied(o) => o.get().clone(),
|
||||
Vacant(v) => v
|
||||
.insert(Arc::new(Mutex::new(
|
||||
File::open(&data_path)
|
||||
.map(|mut file| {
|
||||
from_stream::<PortableSettings, _, _>(&mut file).unwrap_or_default()
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
)))
|
||||
.clone(),
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let path = path.to_path_buf();
|
||||
pool.execute(move || {
|
||||
handle_relative_connection(
|
||||
info,
|
||||
stream,
|
||||
&request.body,
|
||||
&relative_path,
|
||||
&path,
|
||||
&pk_path,
|
||||
pki_path.as_ref().map(|path| path.as_ref()),
|
||||
&data_path,
|
||||
start_level,
|
||||
)
|
||||
});
|
||||
|
||||
handle_relative_connection(
|
||||
info,
|
||||
stream,
|
||||
&request.body,
|
||||
&relative_path,
|
||||
&path,
|
||||
&pk_path,
|
||||
pki_path.as_ref().map(|path| path.as_ref()),
|
||||
&data_path,
|
||||
start_level,
|
||||
)
|
||||
}
|
||||
|
||||
fn reply_image(mut stream: TcpStream, relative_path: &str, mut path: PathBuf) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user