aboutsummaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-09-30 22:19:19 +0200
committermetamuffin <metamuffin@disroot.org>2023-09-30 22:19:19 +0200
commitd546caa3f5053ade763430490911fefd6257af9f (patch)
tree5834ea5aa352239ab9a3f57ee96dee20af51ca77 /server/src
parentc8fe73a7b160d4ada3136de9c87ad2eb0091ff7b (diff)
downloadjellything-d546caa3f5053ade763430490911fefd6257af9f.tar
jellything-d546caa3f5053ade763430490911fefd6257af9f.tar.bz2
jellything-d546caa3f5053ade763430490911fefd6257af9f.tar.zst
make cache async and fix parallel write bug
Diffstat (limited to 'server/src')
-rw-r--r--server/src/import.rs39
-rw-r--r--server/src/routes/ui/assets.rs7
-rw-r--r--server/src/routes/ui/layout.rs2
3 files changed, 27 insertions, 21 deletions
diff --git a/server/src/import.rs b/server/src/import.rs
index 7e62b47..8d8198a 100644
--- a/server/src/import.rs
+++ b/server/src/import.rs
@@ -7,11 +7,17 @@ use crate::{database::Database, federation::Federation, CONF};
use anyhow::{anyhow, bail, Context, Ok};
use async_recursion::async_recursion;
use futures::{stream::FuturesUnordered, StreamExt, TryFutureExt};
-use jellybase::{cache_file, AssetLocationExt};
+use jellybase::async_cache_file;
use jellyclient::Session;
use jellycommon::{AssetLocation, MediaSource, Node, NodePrivate, RemoteImportOptions};
use log::{debug, error, info};
-use std::{ffi::OsStr, fs::File, os::unix::prelude::OsStrExt, path::PathBuf, sync::LazyLock};
+use std::{
+ ffi::OsStr,
+ fs::File,
+ os::unix::prelude::OsStrExt,
+ path::PathBuf,
+ sync::{Arc, LazyLock},
+};
use tokio::sync::Semaphore;
static IMPORT_SEM: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(1));
@@ -133,7 +139,7 @@ static SEM_REMOTE_IMPORT: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(
async fn import_remote(
mut opts: RemoteImportOptions,
db: &Database,
- session: &Session,
+ session: &Arc<Session>,
identifier: String,
parent: Option<String>,
) -> anyhow::Result<Vec<String>> {
@@ -149,11 +155,11 @@ async fn import_remote(
.context("fetching remote node")?;
if node.federated.is_some() {
- return Ok(vec![]) // node is federated, lets not import it
+ return Ok(vec![]); // node is federated, lets not import it
}
- let poster = cache_federation_asset(session, &opts.id, "poster").await?;
- let backdrop = cache_federation_asset(session, &opts.id, "backdrop").await?;
+ let poster = cache_federation_asset(session.to_owned(), opts.id.clone(), "poster").await?;
+ let backdrop = cache_federation_asset(session.to_owned(), opts.id.clone(), "backdrop").await?;
drop(_permit);
@@ -215,15 +221,16 @@ async fn import_remote(
}
async fn cache_federation_asset(
- session: &Session,
- identifier: &String,
- role: &str,
+ session: Arc<Session>,
+ identifier: String,
+ role: &'static str,
) -> anyhow::Result<AssetLocation> {
- let poster = cache_file(&["federation-asset", role, identifier]);
- if !poster.path().exists() {
- session
- .node_asset(&identifier, role, File::create(&poster.path())?)
- .await?;
- }
- Ok(poster)
+ async_cache_file(
+ &["federation-asset", role, &identifier.clone()],
+ move |out| async move {
+ let session = session;
+ session.node_asset(identifier.as_str(), role, out).await
+ },
+ )
+ .await
}
diff --git a/server/src/routes/ui/assets.rs b/server/src/routes/ui/assets.rs
index 992e3da..8c0496e 100644
--- a/server/src/routes/ui/assets.rs
+++ b/server/src/routes/ui/assets.rs
@@ -8,7 +8,7 @@ use crate::{
routes::ui::{account::session::Session, error::MyError, CacheControlFile},
};
use anyhow::anyhow;
-use async_std::task::spawn_blocking;
+use jellybase::AssetLocationExt;
use jellycommon::AssetLocation;
use log::info;
use rocket::{get, http::ContentType, FromFormField, State, UriDisplayQuery};
@@ -53,11 +53,10 @@ pub async fn r_item_assets(
));
// fit the resolution into a finite set so the maximum cache is finite too.
let width = 2usize.pow(width.unwrap_or(2048).clamp(128, 8196).ilog2());
- let path =
- spawn_blocking(move || jellytranscoder::image::transcode(asset, 50., 5, width)).await?;
+ let path = jellytranscoder::image::transcode(asset, 50., 5, width).await?;
info!("loading asset from {path:?}");
Ok((
ContentType::AVIF,
- CacheControlFile::new(File::open(path).await?).await,
+ CacheControlFile::new(File::open(path.path()).await?).await,
))
}
diff --git a/server/src/routes/ui/layout.rs b/server/src/routes/ui/layout.rs
index fdda3e4..1c47247 100644
--- a/server/src/routes/ui/layout.rs
+++ b/server/src/routes/ui/layout.rs
@@ -16,7 +16,7 @@ use crate::{
},
uri,
};
-use async_std::task::block_on;
+use futures::executor::block_on;
use jellybase::CONF;
use markup::{DynRender, Render};
use rocket::{