Skip to content

Commit 363e746

Browse files
Do not attempt to acquire lock if updating
1 parent 8d19fcf commit 363e746

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

site/src/server.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ struct IsUpdating(Arc<AtomicBool>);
727727

728728
impl Drop for IsUpdating {
729729
fn drop(&mut self) {
730-
self.0.store(false, AtomicOrdering::Release);
730+
self.0.store(false, AtomicOrdering::SeqCst);
731731
}
732732
}
733733

@@ -738,7 +738,11 @@ impl UpdatingStatus {
738738

739739
// Returns previous state
740740
fn set_updating(&self) -> bool {
741-
self.0.compare_and_swap(false, true, AtomicOrdering::AcqRel)
741+
self.0.compare_and_swap(false, true, AtomicOrdering::SeqCst)
742+
}
743+
744+
fn is_updating(&self) -> bool {
745+
self.0.load(AtomicOrdering::SeqCst)
742746
}
743747

744748
fn release_on_drop(&self) -> IsUpdating {
@@ -830,21 +834,20 @@ impl Server {
830834
let updating = self.updating.release_on_drop();
831835
let _ = std::thread::spawn(move || {
832836
let repo_path = get_repo_path().unwrap();
833-
834837
git::update_repo(&repo_path).unwrap();
835838

839+
// Acquire the lock. We're not returning data via HTTP already anyway, as we've cleared
840+
let mut handle = rwlock.write();
841+
836842
// Erase old data
837-
*rwlock.write() = None;
843+
*handle = None;
838844

839845
info!("updating from filesystem...");
840846
let new_data = Arc::new(InputData::from_fs(&repo_path).unwrap());
841847
debug!("last date = {:?}", new_data.last_date);
842848

843-
// Retrieve the stored InputData from the request.
844-
let mut data = rwlock.write();
845-
846849
// Write the new data back into the request
847-
*data = Some(new_data);
850+
*handle = Some(new_data);
848851

849852
std::mem::drop(updating);
850853
});
@@ -865,7 +868,8 @@ impl fmt::Display for ServerError {
865868
impl std::error::Error for ServerError {}
866869

867870
async fn serve_req(ctx: Arc<Server>, req: Request) -> Result<Response, ServerError> {
868-
if ctx.data.read().is_none() {
871+
// Don't attempt to get lock if we're updating
872+
if ctx.updating.is_updating() || ctx.data.read().is_none() {
869873
return Ok(Response::new(hyper::Body::from("no data yet, please wait")));
870874
}
871875

0 commit comments

Comments
 (0)