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

@@ -18,7 +18,6 @@ use rustical_dav::{
};
use rustical_store::{model::object::CalendarObject, CalendarStore};
use serde::Deserialize;
use tokio::sync::RwLock;
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
@@ -35,7 +34,7 @@ pub async fn get_objects_calendar_multiget<C: CalendarStore + ?Sized>(
principal_url: &str,
principal: &str,
cal_id: &str,
store: &RwLock<C>,
store: &C,
) -> Result<(Vec<CalendarObject>, Vec<String>), Error> {
let resource_def =
ResourceDef::prefix(principal_url).join(&ResourceDef::new("/{cal_id}/{object_id}"));
@@ -43,7 +42,6 @@ pub async fn get_objects_calendar_multiget<C: CalendarStore + ?Sized>(
let mut result = vec![];
let mut not_found = vec![];
let store = store.read().await;
for href in &cal_query.href {
let mut path = Path::new(href.as_str());
if !resource_def.capture_match_info(&mut path) {
@@ -69,7 +67,7 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
req: HttpRequest,
principal: &str,
cal_id: &str,
cal_store: &RwLock<C>,
cal_store: &C,
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
let principal_url = PrincipalResource::get_url(req.resource_map(), vec![principal]).unwrap();
let (objects, not_found) =

View File

@@ -7,7 +7,6 @@ use rustical_dav::{
};
use rustical_store::{model::object::CalendarObject, CalendarStore};
use serde::Deserialize;
use tokio::sync::RwLock;
use crate::{
calendar_object::resource::{CalendarObjectProp, CalendarObjectResource},
@@ -195,9 +194,9 @@ pub async fn get_objects_calendar_query<C: CalendarStore + ?Sized>(
cal_query: &CalendarQueryRequest,
principal: &str,
cal_id: &str,
store: &RwLock<C>,
store: &C,
) -> Result<Vec<CalendarObject>, Error> {
let mut objects = store.read().await.get_objects(principal, cal_id).await?;
let mut objects = store.get_objects(principal, cal_id).await?;
if let Some(filter) = &cal_query.filter {
objects.retain(|object| filter.matches(object));
}
@@ -209,7 +208,7 @@ pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
req: HttpRequest,
principal: &str,
cal_id: &str,
cal_store: &RwLock<C>,
cal_store: &C,
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
let objects = get_objects_calendar_query(&cal_query, principal, cal_id, cal_store).await?;

View File

@@ -8,7 +8,6 @@ use calendar_query::{handle_calendar_query, CalendarQueryRequest};
use rustical_store::{auth::User, CalendarStore};
use serde::{Deserialize, Serialize};
use sync_collection::{handle_sync_collection, SyncCollectionRequest};
use tokio::sync::RwLock;
use tracing::instrument;
mod calendar_multiget;
@@ -37,7 +36,7 @@ pub async fn route_report_calendar<C: CalendarStore + ?Sized>(
body: String,
user: User,
req: HttpRequest,
cal_store: Data<RwLock<C>>,
cal_store: Data<C>,
) -> Result<impl Responder, Error> {
let (principal, cal_id) = path.into_inner();
if principal != user.id {
@@ -48,13 +47,21 @@ pub async fn route_report_calendar<C: CalendarStore + ?Sized>(
Ok(match request.clone() {
ReportRequest::CalendarQuery(cal_query) => {
handle_calendar_query(cal_query, req, &principal, &cal_id, &cal_store).await?
handle_calendar_query(cal_query, req, &principal, &cal_id, cal_store.as_ref()).await?
}
ReportRequest::CalendarMultiget(cal_multiget) => {
handle_calendar_multiget(cal_multiget, req, &principal, &cal_id, &cal_store).await?
handle_calendar_multiget(cal_multiget, req, &principal, &cal_id, cal_store.as_ref())
.await?
}
ReportRequest::SyncCollection(sync_collection) => {
handle_sync_collection(sync_collection, req, &principal, &cal_id, &cal_store).await?
handle_sync_collection(
sync_collection,
req,
&principal,
&cal_id,
cal_store.as_ref(),
)
.await?
}
})
}

View File

@@ -12,7 +12,6 @@ use rustical_store::{
CalendarStore,
};
use serde::Deserialize;
use tokio::sync::RwLock;
use crate::{
calendar_object::resource::{CalendarObjectProp, CalendarObjectResource},
@@ -47,7 +46,7 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
req: HttpRequest,
principal: &str,
cal_id: &str,
cal_store: &RwLock<C>,
cal_store: &C,
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
let props = match sync_collection.prop {
PropfindType::Allprop => {
@@ -62,8 +61,6 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
let old_synctoken = parse_synctoken(&sync_collection.sync_token).unwrap_or(0);
let (new_objects, deleted_objects, new_synctoken) = cal_store
.read()
.await
.sync_changes(principal, cal_id, old_synctoken)
.await?;