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(())

View File

@@ -4,7 +4,6 @@ use actix_web::{web::Data, HttpResponse};
use rustical_store::model::Addressbook;
use rustical_store::{auth::User, AddressbookStore};
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
@@ -38,7 +37,7 @@ pub async fn route_mkcol<AS: AddressbookStore + ?Sized>(
path: Path<(String, String)>,
body: String,
user: User,
store: Data<RwLock<AS>>,
store: Data<AS>,
) -> Result<HttpResponse, Error> {
let (principal, addressbook_id) = path.into_inner();
if principal != user.id {
@@ -57,12 +56,7 @@ pub async fn route_mkcol<AS: AddressbookStore + ?Sized>(
synctoken: 0,
};
match store
.read()
.await
.get_addressbook(&principal, &addressbook_id)
.await
{
match store.get_addressbook(&principal, &addressbook_id).await {
Err(rustical_store::Error::NotFound) => {
// No conflict, no worries
}
@@ -76,7 +70,7 @@ pub async fn route_mkcol<AS: AddressbookStore + ?Sized>(
}
}
match store.write().await.insert_addressbook(addressbook).await {
match store.insert_addressbook(addressbook).await {
// TODO: The spec says we should return a mkcol-response.
// However, it works without one but breaks on iPadOS when using an empty one :)
Ok(()) => Ok(HttpResponse::Created()

View File

@@ -18,7 +18,6 @@ use rustical_dav::{
};
use rustical_store::{model::AddressObject, AddressbookStore};
use serde::Deserialize;
use tokio::sync::RwLock;
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
@@ -34,7 +33,7 @@ pub async fn get_objects_addressbook_multiget<AS: AddressbookStore + ?Sized>(
principal_url: &str,
principal: &str,
addressbook_id: &str,
store: &RwLock<AS>,
store: &AS,
) -> Result<(Vec<AddressObject>, Vec<String>), Error> {
let resource_def =
ResourceDef::prefix(principal_url).join(&ResourceDef::new("/{addressbook_id}/{object_id}"));
@@ -42,7 +41,6 @@ pub async fn get_objects_addressbook_multiget<AS: AddressbookStore + ?Sized>(
let mut result = vec![];
let mut not_found = vec![];
let store = store.read().await;
for href in &addressbook_multiget.href {
let mut path = Path::new(href.as_str());
if !resource_def.capture_match_info(&mut path) {
@@ -68,7 +66,7 @@ pub async fn handle_addressbook_multiget<AS: AddressbookStore + ?Sized>(
req: HttpRequest,
principal: &str,
cal_id: &str,
addr_store: &RwLock<AS>,
addr_store: &AS,
) -> Result<MultistatusElement<PropstatWrapper<AddressObjectProp>, String>, Error> {
let principal_url = PrincipalResource::get_url(req.resource_map(), vec![principal]).unwrap();
let (objects, not_found) = get_objects_addressbook_multiget(

View File

@@ -7,7 +7,6 @@ use addressbook_multiget::{handle_addressbook_multiget, AddressbookMultigetReque
use rustical_store::{auth::User, AddressbookStore};
use serde::{Deserialize, Serialize};
use sync_collection::{handle_sync_collection, SyncCollectionRequest};
use tokio::sync::RwLock;
use tracing::instrument;
mod addressbook_multiget;
@@ -34,7 +33,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore + ?Sized>(
body: String,
user: User,
req: HttpRequest,
addr_store: Data<RwLock<AS>>,
addr_store: Data<AS>,
) -> Result<impl Responder, Error> {
let (principal, addressbook_id) = path.into_inner();
if principal != user.id {
@@ -50,7 +49,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore + ?Sized>(
req,
&principal,
&addressbook_id,
&addr_store,
addr_store.as_ref(),
)
.await?
}
@@ -60,7 +59,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore + ?Sized>(
req,
&principal,
&addressbook_id,
&addr_store,
addr_store.as_ref(),
)
.await?
}

View File

@@ -16,7 +16,6 @@ use rustical_store::{
AddressbookStore,
};
use serde::Deserialize;
use tokio::sync::RwLock;
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
@@ -45,7 +44,7 @@ pub async fn handle_sync_collection<AS: AddressbookStore + ?Sized>(
req: HttpRequest,
principal: &str,
addressbook_id: &str,
addr_store: &RwLock<AS>,
addr_store: &AS,
) -> Result<MultistatusElement<PropstatWrapper<AddressObjectProp>, String>, Error> {
let props = match sync_collection.prop {
PropfindType::Allprop => {
@@ -60,8 +59,6 @@ pub async fn handle_sync_collection<AS: AddressbookStore + ?Sized>(
let old_synctoken = parse_synctoken(&sync_collection.sync_token).unwrap_or(0);
let (new_objects, deleted_objects, new_synctoken) = addr_store
.read()
.await
.sync_changes(principal, addressbook_id, old_synctoken)
.await?;

View File

@@ -18,10 +18,9 @@ use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use tokio::sync::RwLock;
pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
pub addr_store: Arc<RwLock<AS>>,
pub addr_store: Arc<AS>,
pub path: String,
pub principal: String,
pub addressbook_id: String,
@@ -204,8 +203,6 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
}
let addressbook = self
.addr_store
.read()
.await
.get_addressbook(&self.principal, &self.addressbook_id)
.await
.map_err(|_e| Error::NotFound)?;
@@ -218,8 +215,6 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
Ok(self
.addr_store
.read()
.await
.get_objects(&self.principal, &self.addressbook_id)
.await?
.into_iter()
@@ -241,7 +236,7 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
path_components: Self::PathComponents,
) -> Result<Self, Self::Error> {
let addr_store = req
.app_data::<Data<RwLock<AS>>>()
.app_data::<Data<AS>>()
.expect("no addressbook store in app_data!")
.clone()
.into_inner();
@@ -256,8 +251,6 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
async fn save_resource(&self, file: Self::Resource) -> Result<(), Self::Error> {
self.addr_store
.write()
.await
.update_addressbook(
self.principal.to_owned(),
self.addressbook_id.to_owned(),
@@ -269,8 +262,6 @@ impl<AS: AddressbookStore + ?Sized> ResourceService for AddressbookResourceServi
async fn delete_resource(&self, use_trashbin: bool) -> Result<(), Self::Error> {
self.addr_store
.write()
.await
.delete_addressbook(&self.principal, &self.addressbook_id, use_trashbin)
.await?;
Ok(())

View File

@@ -29,7 +29,7 @@ impl actix_web::ResponseError for Error {
match self {
Error::StoreError(err) => match err {
rustical_store::Error::NotFound => StatusCode::NOT_FOUND,
rustical_store::Error::InvalidIcs(_) => StatusCode::BAD_REQUEST,
rustical_store::Error::InvalidData(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
Error::DavError(err) => err.status_code(),

View File

@@ -18,7 +18,6 @@ use rustical_store::{
AddressbookStore,
};
use std::sync::Arc;
use tokio::sync::RwLock;
pub mod address_object;
pub mod addressbook;
@@ -33,7 +32,7 @@ pub fn configure_well_known(cfg: &mut web::ServiceConfig, carddav_root: String)
pub fn configure_dav<AP: AuthenticationProvider, A: AddressbookStore + ?Sized>(
cfg: &mut web::ServiceConfig,
auth_provider: Arc<AP>,
store: Arc<RwLock<A>>,
store: Arc<A>,
) {
cfg.service(
web::scope("")

View File

@@ -10,11 +10,10 @@ use rustical_store::AddressbookStore;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use tokio::sync::RwLock;
pub struct PrincipalResourceService<A: AddressbookStore + ?Sized> {
principal: String,
addr_store: Arc<RwLock<A>>,
addr_store: Arc<A>,
}
#[derive(Clone)]
@@ -111,7 +110,7 @@ impl<A: AddressbookStore + ?Sized> ResourceService for PrincipalResourceService<
(principal,): Self::PathComponents,
) -> Result<Self, Self::Error> {
let addr_store = req
.app_data::<Data<RwLock<A>>>()
.app_data::<Data<A>>()
.expect("no addressbook store in app_data!")
.clone()
.into_inner();
@@ -135,12 +134,7 @@ impl<A: AddressbookStore + ?Sized> ResourceService for PrincipalResourceService<
&self,
rmap: &ResourceMap,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
let addressbooks = self
.addr_store
.read()
.await
.get_addressbooks(&self.principal)
.await?;
let addressbooks = self.addr_store.get_addressbooks(&self.principal).await?;
Ok(addressbooks
.into_iter()
.map(|addressbook| {