Remove RwLock around stores, locking shall be the responsibility of the store implementation

This commit is contained in:
Lennart
2024-10-27 15:36:49 +01:00
parent df8790f46d
commit 858f43de67
31 changed files with 119 additions and 236 deletions

View File

@@ -7,7 +7,6 @@ use actix_web::HttpResponse;
use rustical_store::auth::User;
use rustical_store::model::CalendarObject;
use rustical_store::CalendarStore;
use tokio::sync::RwLock;
use tracing::instrument;
use tracing_actix_web::RootSpan;
@@ -16,7 +15,7 @@ use super::resource::CalendarObjectPathComponents;
#[instrument(parent = root_span.id(), skip(store, root_span))]
pub async fn get_event<C: CalendarStore + ?Sized>(
path: Path<CalendarObjectPathComponents>,
store: Data<RwLock<C>>,
store: Data<C>,
user: User,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
@@ -30,16 +29,12 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
return Ok(HttpResponse::Unauthorized().body(""));
}
let calendar = store.read().await.get_calendar(&principal, &cal_id).await?;
let calendar = store.get_calendar(&principal, &cal_id).await?;
if user.id != calendar.principal {
return Ok(HttpResponse::Unauthorized().body(""));
}
let event = store
.read()
.await
.get_object(&principal, &cal_id, &object_id)
.await?;
let event = store.get_object(&principal, &cal_id, &object_id).await?;
Ok(HttpResponse::Ok()
.insert_header(("ETag", event.get_etag()))
@@ -50,7 +45,7 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
#[instrument(parent = root_span.id(), skip(store, req, root_span))]
pub async fn put_event<C: CalendarStore + ?Sized>(
path: Path<CalendarObjectPathComponents>,
store: Data<RwLock<C>>,
store: Data<C>,
body: String,
user: User,
req: HttpRequest,
@@ -66,37 +61,16 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
return Ok(HttpResponse::Unauthorized().body(""));
}
let calendar = store.read().await.get_calendar(&principal, &cal_id).await?;
if user.id != calendar.principal {
return Ok(HttpResponse::Unauthorized().body(""));
}
// TODO: implement If-Match
//
let mut store_write = store.write().await;
if Some(&HeaderValue::from_static("*")) == req.headers().get(header::IF_NONE_MATCH) {
// Only write if not existing
match store_write
.get_object(&principal, &cal_id, &object_id)
.await
{
Ok(_) => {
// Conflict
return Ok(HttpResponse::Conflict().body("Resource with this URI already exists"));
}
Err(rustical_store::Error::NotFound) => {
// Path unused, we can proceed
}
Err(err) => {
// Some unknown error :(
return Err(err.into());
}
}
}
let overwrite =
Some(&HeaderValue::from_static("*")) != req.headers().get(header::IF_NONE_MATCH);
let object = CalendarObject::from_ics(object_id, body)?;
store_write.put_object(principal, cal_id, object).await?;
store
.put_object(principal, cal_id, object, overwrite)
.await?;
Ok(HttpResponse::Created().body(""))
}

View File

@@ -1,3 +1,4 @@
use super::methods::{get_event, put_event};
use crate::Error;
use actix_web::{dev::ResourceMap, web::Data, HttpRequest};
use async_trait::async_trait;
@@ -8,12 +9,9 @@ use rustical_store::CalendarStore;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use tokio::sync::RwLock;
use super::methods::{get_event, put_event};
pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
pub cal_store: Arc<RwLock<C>>,
pub cal_store: Arc<C>,
pub path: String,
pub principal: String,
pub cal_id: String,
@@ -121,7 +119,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
} = path_components;
let cal_store = req
.app_data::<Data<RwLock<C>>>()
.app_data::<Data<C>>()
.expect("no calendar store in app_data!")
.clone()
.into_inner();
@@ -141,8 +139,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
}
let event = self
.cal_store
.read()
.await
.get_object(&self.principal, &self.cal_id, &self.object_id)
.await?;
Ok(event.into())
@@ -154,8 +150,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
self.cal_store
.write()
.await
.delete_object(&self.principal, &self.cal_id, &self.object_id, use_trashbin)
.await?;
Ok(())