Skip to content

Commit 8d07f36

Browse files
committed
chore(send queue): adapt to new locks around the event cache store 😎
1 parent 77ee02f commit 8d07f36

File tree

2 files changed

+78
-57
lines changed

2 files changed

+78
-57
lines changed

‎crates/matrix-sdk/src/send_queue.rs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ use matrix_sdk_base::{
146146
FinishUploadThumbnailInfo, QueueWedgeError, QueuedRequest, QueuedRequestKind,
147147
SentMediaInfo, SentRequestKey, SerializableEventContent,
148148
},
149+
store_locks::LockStoreError,
149150
RoomState, StoreError,
150151
};
151152
use matrix_sdk_common::executor::{spawn, JoinHandle};
@@ -693,10 +694,16 @@ impl RoomSendQueue {
693694
})
694695
})?;
695696

696-
let data =
697-
room.client().event_cache_store().get_media_content(&cache_key).await?.ok_or(
698-
crate::Error::SendQueueWedgeError(QueueWedgeError::MissingMediaContent),
699-
)?;
697+
let data = room
698+
.client()
699+
.event_cache_store()
700+
.lock()
701+
.await?
702+
.get_media_content(&cache_key)
703+
.await?
704+
.ok_or(crate::Error::SendQueueWedgeError(
705+
QueueWedgeError::MissingMediaContent,
706+
))?;
700707

701708
#[cfg(feature = "e2e-encryption")]
702709
let media_source = if room.is_encrypted().await? {
@@ -1676,6 +1683,10 @@ pub enum RoomSendQueueStorageError {
16761683
#[error(transparent)]
16771684
EventCacheStoreError(#[from] EventCacheStoreError),
16781685

1686+
/// Error caused when attempting to get a handle on the event cache store.
1687+
#[error(transparent)]
1688+
LockError(#[from] LockStoreError),
1689+
16791690
/// Error caused when (de)serializing into/from json.
16801691
#[error(transparent)]
16811692
JsonSerialization(#[from] serde_json::Error),

‎crates/matrix-sdk/src/send_queue/upload.rs‎

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -160,60 +160,66 @@ impl RoomSendQueue {
160160
Span::current().record("event_txn", tracing::field::display(&*send_event_txn));
161161
debug!(filename, %content_type, %upload_file_txn, "sending an attachment");
162162

163-
// Cache the file itself in the cache store.
164163
let file_media_request = make_local_file_media_request(&upload_file_txn);
165-
room.client()
166-
.event_cache_store()
167-
.add_media_content(&file_media_request, data.clone())
168-
.await
169-
.map_err(|err| RoomSendQueueError::StorageError(err.into()))?;
170164

171-
// Process the thumbnail, if it's been provided.
172-
let (upload_thumbnail_txn, event_thumbnail_info, queue_thumbnail_info) = if let Some(
173-
thumbnail,
174-
) =
175-
config.thumbnail.take()
176-
{
177-
// Normalize information to retrieve the thumbnail in the cache store.
178-
let info = thumbnail.info.as_ref();
179-
let height = info.and_then(|info| info.height).unwrap_or_else(|| {
180-
trace!("thumbnail height is unknown, using 0 for the cache entry");
181-
uint!(0)
182-
});
183-
let width = info.and_then(|info| info.width).unwrap_or_else(|| {
184-
trace!("thumbnail width is unknown, using 0 for the cache entry");
185-
uint!(0)
186-
});
187-
188-
let txn = TransactionId::new();
189-
trace!(upload_thumbnail_txn = %txn, thumbnail_size = ?(height, width), "attachment has a thumbnail");
190-
191-
// Cache thumbnail in the cache store.
192-
let thumbnail_media_request = make_local_thumbnail_media_request(&txn, height, width);
193-
room.client()
165+
let (upload_thumbnail_txn, event_thumbnail_info, queue_thumbnail_info) = {
166+
let client = room.client();
167+
let cache_store = client
194168
.event_cache_store()
195-
.add_media_content(&thumbnail_media_request, thumbnail.data.clone())
169+
.lock()
196170
.await
197-
.map_err(|err| RoomSendQueueError::StorageError(err.into()))?;
198-
199-
// Create the information required for filling the thumbnail section of the
200-
// media event.
201-
let thumbnail_info =
202-
Box::new(assign!(thumbnail.info.map(ThumbnailInfo::from).unwrap_or_default(), {
203-
mimetype: Some(thumbnail.content_type.as_ref().to_owned())
204-
}));
205-
206-
(
207-
Some(txn.clone()),
208-
Some((thumbnail_media_request.source.clone(), thumbnail_info)),
209-
Some((
210-
FinishUploadThumbnailInfo { txn, width, height },
211-
thumbnail_media_request,
212-
thumbnail.content_type,
213-
)),
214-
)
215-
} else {
216-
Default::default()
171+
.map_err(RoomSendQueueStorageError::LockError)?;
172+
173+
// Cache the file itself in the cache store.
174+
cache_store
175+
.add_media_content(&file_media_request, data.clone())
176+
.await
177+
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;
178+
179+
// Process the thumbnail, if it's been provided.
180+
if let Some(thumbnail) = config.thumbnail.take() {
181+
// Normalize information to retrieve the thumbnail in the cache store.
182+
let info = thumbnail.info.as_ref();
183+
let height = info.and_then(|info| info.height).unwrap_or_else(|| {
184+
trace!("thumbnail height is unknown, using 0 for the cache entry");
185+
uint!(0)
186+
});
187+
let width = info.and_then(|info| info.width).unwrap_or_else(|| {
188+
trace!("thumbnail width is unknown, using 0 for the cache entry");
189+
uint!(0)
190+
});
191+
192+
let txn = TransactionId::new();
193+
trace!(upload_thumbnail_txn = %txn, thumbnail_size = ?(height, width), "attachment has a thumbnail");
194+
195+
// Cache thumbnail in the cache store.
196+
let thumbnail_media_request =
197+
make_local_thumbnail_media_request(&txn, height, width);
198+
cache_store
199+
.add_media_content(&thumbnail_media_request, thumbnail.data.clone())
200+
.await
201+
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;
202+
203+
// Create the information required for filling the thumbnail section of the
204+
// media event.
205+
let thumbnail_info = Box::new(
206+
assign!(thumbnail.info.map(ThumbnailInfo::from).unwrap_or_default(), {
207+
mimetype: Some(thumbnail.content_type.as_ref().to_owned())
208+
}),
209+
);
210+
211+
(
212+
Some(txn.clone()),
213+
Some((thumbnail_media_request.source.clone(), thumbnail_info)),
214+
Some((
215+
FinishUploadThumbnailInfo { txn, width, height },
216+
thumbnail_media_request,
217+
thumbnail.content_type,
218+
)),
219+
)
220+
} else {
221+
Default::default()
222+
}
217223
};
218224

219225
// Create the content for the media event.
@@ -296,8 +302,13 @@ impl QueueStorage {
296302

297303
trace!(from = ?from_req.source, to = ?sent_media.file, "renaming media file key in cache store");
298304

299-
client
305+
let cache_store = client
300306
.event_cache_store()
307+
.lock()
308+
.await
309+
.map_err(RoomSendQueueStorageError::LockError)?;
310+
311+
cache_store
301312
.replace_media_key(
302313
&from_req,
303314
&MediaRequestParameters {
@@ -320,8 +331,7 @@ impl QueueStorage {
320331
// Reuse the same format for the cached thumbnail with the final MXC ID.
321332
let new_format = from_req.format.clone();
322333

323-
client
324-
.event_cache_store()
334+
cache_store
325335
.replace_media_key(
326336
&from_req,
327337
&MediaRequestParameters { source: new_source, format: new_format },

0 commit comments

Comments
 (0)