Skip to content

Commit 8d19fcf

Browse files
Do not attempt to hold two data sets in memory
1 parent ab80d14 commit 8d19fcf

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

site/src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
use env_logger;
1111

12+
use parking_lot::RwLock;
1213
use site::{load, util};
1314
use std::env;
15+
use std::sync::Arc;
1416

1517
#[global_allocator]
1618
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
@@ -19,13 +21,17 @@ fn main() {
1921
env_logger::init();
2022
let _ = jemalloc_ctl::background_thread::write(true);
2123

22-
let data = load::InputData::from_fs(&util::get_repo_path().unwrap()).unwrap();
23-
24+
let data: Arc<RwLock<Option<Arc<load::InputData>>>> = Arc::new(RwLock::new(None));
25+
let data_ = data.clone();
26+
std::thread::spawn(move || {
27+
*data_.write() = Some(Arc::new(
28+
load::InputData::from_fs(&util::get_repo_path().unwrap()).unwrap(),
29+
));
30+
});
2431
let port = env::var("PORT")
2532
.ok()
2633
.and_then(|x| x.parse().ok())
2734
.unwrap_or(2346);
2835
println!("Starting server with port={:?}", port);
29-
3036
site::server::start(data, port);
3137
}

site/src/server.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ pub async fn handle_self_profile(
717717
}
718718

719719
struct Server {
720-
data: Arc<RwLock<Arc<InputData>>>,
720+
data: Arc<RwLock<Option<Arc<InputData>>>>,
721721
updating: UpdatingStatus,
722722
}
723723

@@ -781,6 +781,7 @@ impl Server {
781781
check_http_method!(*req.method(), http::Method::GET);
782782
let data = self.data.clone();
783783
let data = data.read();
784+
let data = data.as_ref().unwrap();
784785
let result = handler(&data);
785786
Ok(http::Response::builder()
786787
.header_typed(ContentType::json())
@@ -794,6 +795,7 @@ impl Server {
794795
.get(Authorization::<headers::authorization::Bearer>::name())
795796
{
796797
let data = self.data.read();
798+
let data = data.as_ref().unwrap();
797799
let auth = Authorization::<headers::authorization::Bearer>::decode(
798800
&mut Some(auth).into_iter(),
799801
)
@@ -831,6 +833,9 @@ impl Server {
831833

832834
git::update_repo(&repo_path).unwrap();
833835

836+
// Erase old data
837+
*rwlock.write() = None;
838+
834839
info!("updating from filesystem...");
835840
let new_data = Arc::new(InputData::from_fs(&repo_path).unwrap());
836841
debug!("last date = {:?}", new_data.last_date);
@@ -839,7 +844,7 @@ impl Server {
839844
let mut data = rwlock.write();
840845

841846
// Write the new data back into the request
842-
*data = new_data;
847+
*data = Some(new_data);
843848

844849
std::mem::drop(updating);
845850
});
@@ -860,6 +865,10 @@ impl fmt::Display for ServerError {
860865
impl std::error::Error for ServerError {}
861866

862867
async fn serve_req(ctx: Arc<Server>, req: Request) -> Result<Response, ServerError> {
868+
if ctx.data.read().is_none() {
869+
return Ok(Response::new(hyper::Body::from("no data yet, please wait")));
870+
}
871+
863872
let fs_path = format!(
864873
"site/static{}",
865874
if req.uri().path() == "" || req.uri().path() == "/" {
@@ -897,7 +906,7 @@ async fn serve_req(ctx: Arc<Server>, req: Request) -> Result<Response, ServerErr
897906
let (req, body_stream) = req.into_parts();
898907
let p = req.uri.path();
899908
check_http_method!(req.method, http::Method::POST);
900-
let data: Arc<InputData> = ctx.data.read().clone();
909+
let data: Arc<InputData> = ctx.data.read().as_ref().unwrap().clone();
901910
let mut c = body_stream.compat();
902911
let mut body = Vec::new();
903912
while let Some(chunk) = c.next().await {
@@ -1038,9 +1047,9 @@ where
10381047
}
10391048
}
10401049

1041-
async fn run_server(data: InputData, addr: SocketAddr) {
1050+
async fn run_server(data: Arc<RwLock<Option<Arc<InputData>>>>, addr: SocketAddr) {
10421051
let ctx = Arc::new(Server {
1043-
data: Arc::new(RwLock::new(Arc::new(data))),
1052+
data,
10441053
updating: UpdatingStatus::new(),
10451054
});
10461055
let server = hyper::Server::bind(&addr).serve(move || {
@@ -1063,7 +1072,7 @@ async fn run_server(data: InputData, addr: SocketAddr) {
10631072
}
10641073
}
10651074

1066-
pub fn start(data: InputData, port: u16) {
1075+
pub fn start(data: Arc<RwLock<Option<Arc<InputData>>>>, port: u16) {
10671076
let mut server_address: SocketAddr = "0.0.0.0:2346".parse().unwrap();
10681077
server_address.set_port(port);
10691078
hyper::rt::run(

0 commit comments

Comments
 (0)