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

@@ -8,14 +8,13 @@ use actix_web::HttpResponse;
use rustical_store::auth::User;
use rustical_store::model::AddressObject;
use rustical_store::AddressbookStore;
use tokio::sync::RwLock;
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[instrument(parent = root_span.id(), skip(store, root_span))]
pub async fn get_object<AS: AddressbookStore + ?Sized>(
path: Path<AddressObjectPathComponents>,
store: Data<RwLock<AS>>,
store: Data<AS>,
user: User,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
@@ -29,20 +28,12 @@ pub async fn get_object<AS: AddressbookStore + ?Sized>(
return Ok(HttpResponse::Unauthorized().body(""));
}
let addressbook = store
.read()
.await
.get_addressbook(&principal, &cal_id)
.await?;
let addressbook = store.get_addressbook(&principal, &cal_id).await?;
if user.id != addressbook.principal {
return Ok(HttpResponse::Unauthorized().body(""));
}
let object = store
.read()
.await
.get_object(&principal, &cal_id, &object_id)
.await?;
let object = store.get_object(&principal, &cal_id, &object_id).await?;
Ok(HttpResponse::Ok()
.insert_header(("ETag", object.get_etag()))
@@ -53,7 +44,7 @@ pub async fn get_object<AS: AddressbookStore + ?Sized>(
#[instrument(parent = root_span.id(), skip(store, req, root_span))]
pub async fn put_object<AS: AddressbookStore + ?Sized>(
path: Path<AddressObjectPathComponents>,
store: Data<RwLock<AS>>,
store: Data<AS>,
body: String,
user: User,
req: HttpRequest,
@@ -69,42 +60,15 @@ pub async fn put_object<AS: AddressbookStore + ?Sized>(
return Ok(HttpResponse::Unauthorized().body(""));
}
let addressbook = store
.read()
.await
.get_addressbook(&principal, &addressbook_id)
.await?;
if user.id != addressbook.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, &addressbook_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 = AddressObject::from_vcf(object_id, body)?;
store_write
.put_object(principal, addressbook_id, object)
store
.put_object(principal, addressbook_id, object, overwrite)
.await?;
Ok(HttpResponse::Created().body(""))

View File

@@ -7,12 +7,11 @@ use rustical_store::{model::AddressObject, AddressbookStore};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use tokio::sync::RwLock;
use super::methods::{get_object, put_object};
pub struct AddressObjectResourceService<AS: AddressbookStore + ?Sized> {
pub addr_store: Arc<RwLock<AS>>,
pub addr_store: Arc<AS>,
pub path: String,
pub principal: String,
pub cal_id: String,
@@ -120,7 +119,7 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressObjectResourceSer
} = path_components;
let addr_store = req
.app_data::<Data<RwLock<AS>>>()
.app_data::<Data<AS>>()
.expect("no addressbook store in app_data!")
.clone()
.into_inner();
@@ -140,8 +139,6 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressObjectResourceSer
}
let event = self
.addr_store
.read()
.await
.get_object(&self.principal, &self.cal_id, &self.object_id)
.await?;
Ok(event.into())
@@ -153,8 +150,6 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressObjectResourceSer
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
self.addr_store
.write()
.await
.delete_object(&self.principal, &self.cal_id, &self.object_id, use_trashbin)
.await?;
Ok(())