Skip to content

Commit f2d67e0

Browse files
Use std::thread over CpuPool
Only one thread should ever be running at a time, and it's somewhat rare that one is started, so the overheads involved are fine - and CpuPool is as such not necessary.
1 parent ce59f44 commit f2d67e0

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

site/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ edition = '2018'
88
env_logger = "0.6"
99
failure = "0.1"
1010
futures = "0.1.28"
11-
futures-cpupool = "0.1.5"
1211
log = "0.4"
1312
serde = "1"
1413
serde_derive = "1"

site/src/server.rs

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ use std::borrow::Cow;
1111
use std::cmp::Ordering;
1212
use std::collections::HashMap;
1313
use std::fmt;
14-
use std::fs::File;
15-
use std::io::Read;
14+
use std::fs;
1615
use std::net::SocketAddr;
1716
use std::path::Path;
1817
use std::str;
1918
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
2019
use std::sync::Arc;
2120

22-
use failure::Error;
2321
use futures::{self, Future, Stream};
24-
use futures_cpupool::CpuPool;
2522
use headers::CacheControl;
2623
use headers::Header;
2724
use headers::{Authorization, ContentLength, ContentType};
@@ -686,8 +683,32 @@ pub fn handle_collected(
686683

687684
struct Server {
688685
data: Arc<RwLock<InputData>>,
689-
pool: CpuPool,
690-
updating: Arc<AtomicBool>,
686+
updating: UpdatingStatus,
687+
}
688+
689+
struct UpdatingStatus(Arc<AtomicBool>);
690+
691+
struct IsUpdating(Arc<AtomicBool>);
692+
693+
impl Drop for IsUpdating {
694+
fn drop(&mut self) {
695+
self.0.store(false, AtomicOrdering::Release);
696+
}
697+
}
698+
699+
impl UpdatingStatus {
700+
fn new() -> Self {
701+
UpdatingStatus(Arc::new(AtomicBool::new(false)))
702+
}
703+
704+
// Returns previous state
705+
fn set_updating(&self) -> bool {
706+
self.0.compare_and_swap(false, true, AtomicOrdering::AcqRel)
707+
}
708+
709+
fn release_on_drop(&self) -> IsUpdating {
710+
IsUpdating(self.0.clone())
711+
}
691712
}
692713

693714
macro_rules! check_http_method {
@@ -808,7 +829,7 @@ impl Server {
808829
let data = self.data.clone();
809830
let gh_header = req.headers().get("X-Hub-Signature").cloned();
810831
let gh_header = gh_header.and_then(|g| g.to_str().ok().map(|s| s.to_owned()));
811-
Box::new(self.pool.spawn_fn(move || {
832+
Box::new(
812833
req.into_body()
813834
.concat2()
814835
.map_err(|e| ServerError(format!("{:?}", e)))
@@ -868,15 +889,13 @@ impl Server {
868889
.body(hyper::Body::from(err))
869890
.unwrap(),
870891
}
871-
})
872-
}))
892+
}),
893+
)
873894
}
874895

875896
fn handle_push(&self, _req: Request) -> ServerFut {
876897
// set to updating
877-
let was_updating = self
878-
.updating
879-
.compare_and_swap(false, true, AtomicOrdering::AcqRel);
898+
let was_updating = self.updating.set_updating();
880899

881900
if was_updating {
882901
return Box::new(futures::future::ok(
@@ -895,14 +914,14 @@ impl Server {
895914
debug!("received onpush hook");
896915

897916
let rwlock = self.data.clone();
898-
let updating = self.updating.clone();
899-
let response = self.pool.spawn_fn(move || -> Result<(), Error> {
900-
let repo_path = get_repo_path()?;
917+
let updating = self.updating.release_on_drop();
918+
let _ = std::thread::spawn(move || {
919+
let repo_path = get_repo_path().unwrap();
901920

902-
git::update_repo(&repo_path)?;
921+
git::update_repo(&repo_path).unwrap();
903922

904923
info!("updating from filesystem...");
905-
let new_data = InputData::from_fs(&repo_path)?;
924+
let new_data = InputData::from_fs(&repo_path).unwrap();
906925
debug!("last date = {:?}", new_data.last_date);
907926

908927
// Retrieve the stored InputData from the request.
@@ -911,29 +930,12 @@ impl Server {
911930
// Write the new data back into the request
912931
*data = new_data;
913932

914-
updating.store(false, AtomicOrdering::Release);
915-
916-
Ok(())
933+
std::mem::drop(updating);
917934
});
918935

919-
let updating = self.updating.clone();
920-
Box::new(
921-
response
922-
.map(|_| Response::new(hyper::Body::from("Successfully updated!")))
923-
.or_else(move |err| {
924-
updating.store(false, AtomicOrdering::Release);
925-
futures::future::ok(
926-
http::Response::builder()
927-
.status(StatusCode::INTERNAL_SERVER_ERROR)
928-
.header_typed(ContentType::text_utf8())
929-
.body(hyper::Body::from(format!(
930-
"Internal Server Error: {:?}",
931-
err
932-
)))
933-
.unwrap(),
934-
)
935-
}),
936-
)
936+
Box::new(futures::future::ok(Response::new(hyper::Body::from(
937+
"Queued update",
938+
))))
937939
}
938940
}
939941

@@ -972,12 +974,10 @@ impl Server {
972974
}
973975

974976
if Path::new(&fs_path).is_file() {
975-
return Box::new(self.pool.spawn_fn(move || {
976-
let mut f = File::open(&fs_path).unwrap();
977-
let mut source = Vec::new();
978-
f.read_to_end(&mut source).unwrap();
979-
futures::future::ok(Response::new(hyper::Body::from(source)))
980-
}));
977+
let source = fs::read(&fs_path).unwrap();
978+
return Box::new(futures::future::ok(Response::new(hyper::Body::from(
979+
source,
980+
))));
981981
}
982982

983983
match req.uri().path() {
@@ -1018,8 +1018,7 @@ fn verify_gh_sig(cfg: &Config, header: &str, body: &[u8]) -> Option<bool> {
10181018
pub fn start(data: InputData, port: u16) {
10191019
let server = Arc::new(Server {
10201020
data: Arc::new(RwLock::new(data)),
1021-
pool: CpuPool::new_num_cpus(),
1022-
updating: Arc::new(AtomicBool::new(false)),
1021+
updating: UpdatingStatus::new(),
10231022
});
10241023
let mut server_address: SocketAddr = "0.0.0.0:2346".parse().unwrap();
10251024
server_address.set_port(port);

0 commit comments

Comments
 (0)