diff --git a/crates/cloud/src/app_data/mod.rs b/crates/cloud/src/app_data/mod.rs index a9d0087c..f9f15e70 100644 --- a/crates/cloud/src/app_data/mod.rs +++ b/crates/cloud/src/app_data/mod.rs @@ -365,10 +365,13 @@ impl AppData { .await .map_err(InternalError::DatabaseConnectionError)?; - let mut cache = self.project_cache.write().unwrap(); - projects.iter().for_each(|project| { - cache.put(project.id.clone(), project.clone()); - }); + if let Ok(mut cache) = self.project_cache.write() { + projects.iter().for_each(|project| { + cache.put(project.id.clone(), project.clone()); + }); + } else { + log::warn!("Unable to acquire lock to cache project metadata."); + } results.extend(projects); } @@ -381,19 +384,28 @@ impl AppData { ) -> (Vec, Vec<&'a api::ProjectId>) { let mut results = Vec::new(); let mut missing_projects = Vec::new(); - let mut cache = self.project_cache.write().unwrap(); - for id in ids { - match cache.get(id) { - Some(project_metadata) => results.push(project_metadata.clone()), - None => missing_projects.push(id), + if let Ok(mut cache) = self.project_cache.write() { + for id in ids { + match cache.get(id) { + Some(project_metadata) => results.push(project_metadata.clone()), + None => missing_projects.push(id), + } } + (results, missing_projects) + } else { + (results, ids.collect()) } - (results, missing_projects) } fn get_cached_project(&self, id: &ProjectId) -> Option { - let mut cache = self.project_cache.write().unwrap(); - cache.get(id).map(|md| md.to_owned()) + self.project_cache + .write() + .map_err(|err| { + log::warn!("Unable to acquire project cache: {}", &err); + err + }) + .ok() + .and_then(|mut cache| cache.get(id).map(|md| md.to_owned())) } async fn get_project_and_cache(&self, id: &ProjectId) -> Result { @@ -404,9 +416,13 @@ impl AppData { .map_err(InternalError::DatabaseConnectionError)? .ok_or(UserError::ProjectNotFoundError)?; - let mut cache = self.project_cache.write().unwrap(); - cache.put(id.clone(), metadata); - Ok(cache.get(id).unwrap().clone()) + if let Ok(mut cache) = self.project_cache.write() { + log::warn!("Unable to acquire project cache to cache project"); + cache.put(id.clone(), metadata); + Ok(cache.get(id).unwrap().clone()) + } else { + Ok(metadata) + } } // Membership queries (cached) diff --git a/crates/cloud/src/projects/actions.rs b/crates/cloud/src/projects/actions.rs index 4a7e6624..6f05f022 100644 --- a/crates/cloud/src/projects/actions.rs +++ b/crates/cloud/src/projects/actions.rs @@ -226,6 +226,7 @@ impl<'a> ProjectActions<'a> { .write_image(image.as_bytes(), resized_width, resized_height, color) .map_err(InternalError::ThumbnailEncodeError)?; actix_web::web::Bytes::copy_from_slice(&png_bytes.into_inner().unwrap()) + // FIXME: remove unwrap? } else { let (width, height) = thumbnail.dimensions(); let mut png_bytes = BufWriter::new(Vec::new()); @@ -235,6 +236,7 @@ impl<'a> ProjectActions<'a> { .write_image(thumbnail.as_bytes(), width, height, color) .map_err(InternalError::ThumbnailEncodeError)?; actix_web::web::Bytes::copy_from_slice(&png_bytes.into_inner().unwrap()) + // FIXME: remove unwrap? }; Ok(image_content) @@ -634,8 +636,11 @@ impl<'a> ProjectActions<'a> { .into_iter() .collect::, _>>()?; - let mut cache = self.project_cache.write().unwrap(); - cache.pop(&metadata.id); + if let Ok(mut cache) = self.project_cache.write() { + cache.pop(&metadata.id); + } else { + log::error!("Unable to acquire project cache lock to clear project."); + } self.network .do_send(topology::ProjectDeleted::new(metadata.clone())); diff --git a/crates/cloud/src/utils.rs b/crates/cloud/src/utils.rs index df3371bb..a980079e 100644 --- a/crates/cloud/src/utils.rs +++ b/crates/cloud/src/utils.rs @@ -41,21 +41,25 @@ pub(crate) fn update_project_cache( cache: &Arc>>, metadata: ProjectMetadata, ) -> ProjectMetadata { - let mut cache = cache.write().unwrap(); - let latest = cache - .get(&metadata.id) - .and_then(|existing| { - if existing.updated > metadata.updated { - Some(existing.to_owned()) - } else { - None - } - }) - .unwrap_or(metadata); + if let Ok(mut cache) = cache.write() { + let latest = cache + .get(&metadata.id) + .and_then(|existing| { + if existing.updated > metadata.updated { + Some(existing.to_owned()) + } else { + None + } + }) + .unwrap_or(metadata); - cache.put(latest.id.clone(), latest.clone()); + cache.put(latest.id.clone(), latest.clone()); - latest + latest + } else { + log::warn!("Unable to acquire project cache to update project"); + metadata + } } /// Get a unique project name for the given user and preferred name.