Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions crates/cloud/src/app_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -381,19 +384,28 @@ impl AppData {
) -> (Vec<ProjectMetadata>, 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<ProjectMetadata> {
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<ProjectMetadata, UserError> {
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions crates/cloud/src/projects/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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)
Expand Down Expand Up @@ -634,8 +636,11 @@ impl<'a> ProjectActions<'a> {
.into_iter()
.collect::<Result<Vec<_>, _>>()?;

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()));
Expand Down
30 changes: 17 additions & 13 deletions crates/cloud/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,25 @@ pub(crate) fn update_project_cache(
cache: &Arc<RwLock<LruCache<api::ProjectId, ProjectMetadata>>>,
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.
Expand Down