Skip to content

Commit

Permalink
Add rw lock for static file serving
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox committed Aug 10, 2021
1 parent 46bfbdf commit 3b185cb
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/server.rs
Expand Up @@ -4,7 +4,7 @@ use crate::types::Headers;
use actix_files::Files;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{Relaxed, SeqCst};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, RwLock};
use std::thread;
// pyO3 module
use actix_web::*;
Expand All @@ -27,7 +27,7 @@ struct Directory {
pub struct Server {
router: Arc<Router>,
headers: Arc<DashMap<String, String>>,
directories: Arc<Mutex<Vec<Directory>>>,
directories: Arc<RwLock<Vec<Directory>>>,
}

#[pymethods]
Expand All @@ -37,7 +37,7 @@ impl Server {
Self {
router: Arc::new(Router::new()),
headers: Arc::new(DashMap::new()),
directories: Arc::new(Mutex::new(Vec::new())),
directories: Arc::new(RwLock::new(Vec::new())),
}
}

Expand Down Expand Up @@ -69,7 +69,7 @@ impl Server {

HttpServer::new(move || {
let mut app = App::new();
let directories = directories.lock().unwrap();
let directories = directories.read().unwrap();
for directory in directories.iter() {
if let Some(index_file) = &directory.index_file {
app = app.service(
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Server {
index_file: Option<String>,
show_files_listing: bool,
) {
self.directories.lock().unwrap().push(Directory {
self.directories.write().unwrap().push(Directory {
route,
directory_path,
index_file,
Expand Down

0 comments on commit 3b185cb

Please sign in to comment.