mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-11-20 11:21:14 +00:00
postgres: add already-claimed case, general: tracing paranoia
This commit is contained in:
parent
31caea438e
commit
a43de122f9
15 changed files with 234 additions and 189 deletions
|
@ -82,14 +82,12 @@ impl Drop for Backgrounded {
|
|||
|
||||
let cleanup_span = tracing::info_span!(parent: &cleanup_parent_span, "Backgrounded cleanup Identifier", identifier = ?identifier);
|
||||
|
||||
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
||||
actix_rt::spawn(
|
||||
async move {
|
||||
let _ = crate::queue::cleanup_identifier(&repo, &identifier).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
)
|
||||
});
|
||||
crate::sync::spawn(
|
||||
async move {
|
||||
let _ = crate::queue::cleanup_identifier(&repo, &identifier).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(upload_id) = self.upload_id {
|
||||
|
@ -97,14 +95,12 @@ impl Drop for Backgrounded {
|
|||
|
||||
let cleanup_span = tracing::info_span!(parent: &cleanup_parent_span, "Backgrounded cleanup Upload ID", upload_id = ?upload_id);
|
||||
|
||||
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
||||
actix_rt::spawn(
|
||||
async move {
|
||||
let _ = repo.claim(upload_id).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
)
|
||||
});
|
||||
crate::sync::spawn(
|
||||
async move {
|
||||
let _ = repo.claim(upload_id).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -446,15 +446,15 @@ mod io_uring {
|
|||
actix_rt::System::new().block_on(async move {
|
||||
let arbiter = actix_rt::Arbiter::new();
|
||||
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
let (tx, rx) = crate::sync::channel(1);
|
||||
|
||||
arbiter.spawn(async move {
|
||||
let handle = actix_rt::spawn($fut);
|
||||
let handle = crate::sync::spawn($fut);
|
||||
|
||||
let _ = tx.send(handle.await.unwrap());
|
||||
});
|
||||
|
||||
rx.await.unwrap()
|
||||
rx.into_recv_async().await.unwrap()
|
||||
})
|
||||
};
|
||||
}
|
||||
|
|
|
@ -217,14 +217,12 @@ impl Drop for Session {
|
|||
|
||||
let cleanup_span = tracing::info_span!(parent: &cleanup_parent_span, "Session cleanup hash", hash = ?hash);
|
||||
|
||||
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
||||
actix_rt::spawn(
|
||||
async move {
|
||||
let _ = crate::queue::cleanup_hash(&repo, hash).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
)
|
||||
});
|
||||
crate::sync::spawn(
|
||||
async move {
|
||||
let _ = crate::queue::cleanup_hash(&repo, hash).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(alias) = self.alias.take() {
|
||||
|
@ -233,14 +231,12 @@ impl Drop for Session {
|
|||
|
||||
let cleanup_span = tracing::info_span!(parent: &cleanup_parent_span, "Session cleanup alias", alias = ?alias);
|
||||
|
||||
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
||||
actix_rt::spawn(
|
||||
async move {
|
||||
let _ = crate::queue::cleanup_alias(&repo, alias, token).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
)
|
||||
});
|
||||
crate::sync::spawn(
|
||||
async move {
|
||||
let _ = crate::queue::cleanup_alias(&repo, alias, token).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(identifier) = self.identifier.take() {
|
||||
|
@ -248,14 +244,12 @@ impl Drop for Session {
|
|||
|
||||
let cleanup_span = tracing::info_span!(parent: &cleanup_parent_span, "Session cleanup identifier", identifier = ?identifier);
|
||||
|
||||
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
||||
actix_rt::spawn(
|
||||
async move {
|
||||
let _ = crate::queue::cleanup_identifier(&repo, &identifier).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
)
|
||||
});
|
||||
crate::sync::spawn(
|
||||
async move {
|
||||
let _ = crate::queue::cleanup_identifier(&repo, &identifier).await;
|
||||
}
|
||||
.instrument(cleanup_span),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,16 +82,15 @@ mod test {
|
|||
actix_rt::System::new().block_on(async move {
|
||||
let arbiter = actix_rt::Arbiter::new();
|
||||
|
||||
let (tx, rx) = tracing::trace_span!(parent: None, "Create channel")
|
||||
.in_scope(|| tokio::sync::oneshot::channel());
|
||||
let (tx, rx) = crate::sync::channel(1);
|
||||
|
||||
arbiter.spawn(async move {
|
||||
let handle = actix_rt::spawn($fut);
|
||||
let handle = crate::sync::spawn($fut);
|
||||
|
||||
let _ = tx.send(handle.await.unwrap());
|
||||
});
|
||||
|
||||
rx.await.unwrap()
|
||||
rx.into_recv_async().await.unwrap()
|
||||
})
|
||||
};
|
||||
}
|
||||
|
|
59
src/lib.rs
59
src/lib.rs
|
@ -26,6 +26,7 @@ mod repo_04;
|
|||
mod serde_str;
|
||||
mod store;
|
||||
mod stream;
|
||||
mod sync;
|
||||
mod tmp_file;
|
||||
mod validate;
|
||||
|
||||
|
@ -84,8 +85,9 @@ const DAYS: u32 = 24 * HOURS;
|
|||
const NOT_FOUND_KEY: &str = "404-alias";
|
||||
|
||||
static PROCESS_SEMAPHORE: Lazy<Semaphore> = Lazy::new(|| {
|
||||
tracing::trace_span!(parent: None, "Initialize semaphore")
|
||||
.in_scope(|| Semaphore::new(num_cpus::get().saturating_sub(1).max(1)))
|
||||
let permits = num_cpus::get().saturating_sub(1).max(1);
|
||||
|
||||
crate::sync::bare_semaphore(permits)
|
||||
});
|
||||
|
||||
async fn ensure_details<S: Store + 'static>(
|
||||
|
@ -1671,44 +1673,39 @@ fn spawn_cleanup(repo: ArcRepo, config: &Configuration) {
|
|||
return;
|
||||
}
|
||||
|
||||
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
||||
actix_rt::spawn(async move {
|
||||
let mut interval = actix_rt::time::interval(Duration::from_secs(30));
|
||||
crate::sync::spawn(async move {
|
||||
let mut interval = actix_rt::time::interval(Duration::from_secs(30));
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
loop {
|
||||
interval.tick().await;
|
||||
|
||||
if let Err(e) = queue::cleanup_outdated_variants(&repo).await {
|
||||
tracing::warn!(
|
||||
"Failed to spawn cleanup for outdated variants:{}",
|
||||
format!("\n{e}\n{e:?}")
|
||||
);
|
||||
}
|
||||
|
||||
if let Err(e) = queue::cleanup_outdated_proxies(&repo).await {
|
||||
tracing::warn!(
|
||||
"Failed to spawn cleanup for outdated proxies:{}",
|
||||
format!("\n{e}\n{e:?}")
|
||||
);
|
||||
}
|
||||
if let Err(e) = queue::cleanup_outdated_variants(&repo).await {
|
||||
tracing::warn!(
|
||||
"Failed to spawn cleanup for outdated variants:{}",
|
||||
format!("\n{e}\n{e:?}")
|
||||
);
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
if let Err(e) = queue::cleanup_outdated_proxies(&repo).await {
|
||||
tracing::warn!(
|
||||
"Failed to spawn cleanup for outdated proxies:{}",
|
||||
format!("\n{e}\n{e:?}")
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn spawn_workers<S>(repo: ArcRepo, store: S, config: Configuration, process_map: ProcessMap)
|
||||
where
|
||||
S: Store + 'static,
|
||||
{
|
||||
tracing::trace_span!(parent: None, "Spawn task").in_scope(|| {
|
||||
actix_rt::spawn(queue::process_cleanup(
|
||||
repo.clone(),
|
||||
store.clone(),
|
||||
config.clone(),
|
||||
))
|
||||
});
|
||||
tracing::trace_span!(parent: None, "Spawn task")
|
||||
.in_scope(|| actix_rt::spawn(queue::process_images(repo, store, process_map, config)));
|
||||
crate::sync::spawn(queue::process_cleanup(
|
||||
repo.clone(),
|
||||
store.clone(),
|
||||
config.clone(),
|
||||
));
|
||||
crate::sync::spawn(queue::process_images(repo, store, process_map, config));
|
||||
}
|
||||
|
||||
async fn launch_file_store<F: Fn(&mut web::ServiceConfig) + Send + Clone + 'static>(
|
||||
|
|
|
@ -61,7 +61,7 @@ where
|
|||
tracing::warn!("Retrying migration +{failure_count}");
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
||||
actix_rt::time::sleep(Duration::from_secs(3)).await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -364,7 +364,7 @@ where
|
|||
tracing::warn!("Failed moving file. Retrying +{failure_count}");
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
||||
actix_rt::time::sleep(Duration::from_secs(3)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use actix_rt::task::JoinHandle;
|
||||
use actix_web::web::Bytes;
|
||||
use flume::r#async::RecvFut;
|
||||
use std::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
|
@ -10,7 +11,6 @@ use std::{
|
|||
use tokio::{
|
||||
io::{AsyncRead, AsyncWriteExt, ReadBuf},
|
||||
process::{Child, ChildStdin, ChildStdout, Command},
|
||||
sync::oneshot::{channel, Receiver},
|
||||
};
|
||||
use tracing::{Instrument, Span};
|
||||
|
||||
|
@ -73,7 +73,7 @@ struct DropHandle {
|
|||
|
||||
pub(crate) struct ProcessRead<I> {
|
||||
inner: I,
|
||||
err_recv: Receiver<std::io::Error>,
|
||||
err_recv: RecvFut<'static, std::io::Error>,
|
||||
err_closed: bool,
|
||||
#[allow(dead_code)]
|
||||
handle: DropHandle,
|
||||
|
@ -206,39 +206,37 @@ impl Process {
|
|||
let stdin = child.stdin.take().expect("stdin exists");
|
||||
let stdout = child.stdout.take().expect("stdout exists");
|
||||
|
||||
let (tx, rx) = tracing::trace_span!(parent: None, "Create channel", %command)
|
||||
.in_scope(channel::<std::io::Error>);
|
||||
let (tx, rx) = crate::sync::channel::<std::io::Error>(1);
|
||||
let rx = rx.into_recv_async();
|
||||
|
||||
let span = tracing::info_span!(parent: None, "Background process task", %command);
|
||||
span.follows_from(Span::current());
|
||||
|
||||
let handle = tracing::trace_span!(parent: None, "Spawn task", %command).in_scope(|| {
|
||||
actix_rt::spawn(
|
||||
async move {
|
||||
let child_fut = async {
|
||||
(f)(stdin).await?;
|
||||
let handle = crate::sync::spawn(
|
||||
async move {
|
||||
let child_fut = async {
|
||||
(f)(stdin).await?;
|
||||
|
||||
child.wait().await
|
||||
};
|
||||
child.wait().await
|
||||
};
|
||||
|
||||
let error = match actix_rt::time::timeout(timeout, child_fut).await {
|
||||
Ok(Ok(status)) if status.success() => {
|
||||
guard.disarm();
|
||||
return;
|
||||
}
|
||||
Ok(Ok(status)) => {
|
||||
std::io::Error::new(std::io::ErrorKind::Other, StatusError(status))
|
||||
}
|
||||
Ok(Err(e)) => e,
|
||||
Err(_) => std::io::ErrorKind::TimedOut.into(),
|
||||
};
|
||||
let error = match actix_rt::time::timeout(timeout, child_fut).await {
|
||||
Ok(Ok(status)) if status.success() => {
|
||||
guard.disarm();
|
||||
return;
|
||||
}
|
||||
Ok(Ok(status)) => {
|
||||
std::io::Error::new(std::io::ErrorKind::Other, StatusError(status))
|
||||
}
|
||||
Ok(Err(e)) => e,
|
||||
Err(_) => std::io::ErrorKind::TimedOut.into(),
|
||||
};
|
||||
|
||||
let _ = tx.send(error);
|
||||
let _ = child.kill().await;
|
||||
}
|
||||
.instrument(span),
|
||||
)
|
||||
});
|
||||
let _ = tx.send(error);
|
||||
let _ = child.kill().await;
|
||||
}
|
||||
.instrument(span),
|
||||
);
|
||||
|
||||
let sleep = actix_rt::time::sleep(timeout);
|
||||
|
||||
|
|
|
@ -86,7 +86,7 @@ where
|
|||
let repo = repo.clone();
|
||||
|
||||
let media = media.clone();
|
||||
let error_boundary = actix_rt::spawn(async move {
|
||||
let error_boundary = crate::sync::spawn(async move {
|
||||
let stream = store2
|
||||
.to_stream(&ident, None, None)
|
||||
.await?
|
||||
|
|
|
@ -20,7 +20,8 @@ use diesel_async::{
|
|||
AsyncConnection, AsyncPgConnection, RunQueryDsl,
|
||||
};
|
||||
use tokio::sync::Notify;
|
||||
use tokio_postgres::{AsyncMessage, Notification};
|
||||
use tokio_postgres::{tls::NoTlsStream, AsyncMessage, Connection, NoTls, Notification, Socket};
|
||||
use tracing::Instrument;
|
||||
use url::Url;
|
||||
use uuid::Uuid;
|
||||
|
||||
|
@ -64,7 +65,7 @@ async fn delegate_notifications(receiver: flume::Receiver<Notification>, inner:
|
|||
inner
|
||||
.queue_notifications
|
||||
.entry(queue_name)
|
||||
.or_insert_with(|| Arc::new(Notify::new()))
|
||||
.or_insert_with(crate::sync::notify)
|
||||
.notify_waiters();
|
||||
}
|
||||
"upload_completion_channel" => {
|
||||
|
@ -134,12 +135,11 @@ impl PostgresError {
|
|||
|
||||
impl PostgresRepo {
|
||||
pub(crate) async fn connect(postgres_url: Url) -> Result<Self, ConnectPostgresError> {
|
||||
let (mut client, conn) =
|
||||
tokio_postgres::connect(postgres_url.as_str(), tokio_postgres::tls::NoTls)
|
||||
.await
|
||||
.map_err(ConnectPostgresError::ConnectForMigration)?;
|
||||
let (mut client, conn) = tokio_postgres::connect(postgres_url.as_str(), NoTls)
|
||||
.await
|
||||
.map_err(ConnectPostgresError::ConnectForMigration)?;
|
||||
|
||||
let handle = actix_rt::spawn(conn);
|
||||
let handle = crate::sync::spawn(conn);
|
||||
|
||||
embedded::migrations::runner()
|
||||
.run_async(&mut client)
|
||||
|
@ -166,10 +166,12 @@ impl PostgresRepo {
|
|||
health_count: AtomicU64::new(0),
|
||||
pool,
|
||||
queue_notifications: DashMap::new(),
|
||||
upload_notifier: Notify::new(),
|
||||
upload_notifier: crate::sync::bare_notify(),
|
||||
});
|
||||
|
||||
let notifications = Arc::new(actix_rt::spawn(delegate_notifications(rx, inner.clone())));
|
||||
let handle = crate::sync::spawn(delegate_notifications(rx, inner.clone()));
|
||||
|
||||
let notifications = Arc::new(handle);
|
||||
|
||||
Ok(PostgresRepo {
|
||||
inner,
|
||||
|
@ -198,41 +200,53 @@ fn build_handler(sender: flume::Sender<Notification>) -> ConfigFn {
|
|||
move |config: &str| -> BoxFuture<'_, ConnectionResult<AsyncPgConnection>> {
|
||||
let sender = sender.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
let (client, mut conn) =
|
||||
tokio_postgres::connect(config, tokio_postgres::tls::NoTls)
|
||||
.await
|
||||
.map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
|
||||
let connect_span = tracing::trace_span!(parent: None, "connect future");
|
||||
|
||||
// not very cash money (structured concurrency) of me
|
||||
actix_rt::spawn(async move {
|
||||
while let Some(res) = std::future::poll_fn(|cx| conn.poll_message(cx)).await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
tracing::error!("Database Connection {e:?}");
|
||||
return;
|
||||
}
|
||||
Ok(AsyncMessage::Notice(e)) => {
|
||||
tracing::warn!("Database Notice {e:?}");
|
||||
}
|
||||
Ok(AsyncMessage::Notification(notification)) => {
|
||||
if sender.send_async(notification).await.is_err() {
|
||||
tracing::warn!("Missed notification. Are we shutting down?");
|
||||
}
|
||||
}
|
||||
Ok(_) => {
|
||||
tracing::warn!("Unhandled AsyncMessage!!! Please contact the developer of this application");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Box::pin(
|
||||
async move {
|
||||
let (client, conn) =
|
||||
tokio_postgres::connect(config, tokio_postgres::tls::NoTls)
|
||||
.await
|
||||
.map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
|
||||
|
||||
AsyncPgConnection::try_from(client).await
|
||||
})
|
||||
// not very cash money (structured concurrency) of me
|
||||
spawn_db_notification_task(sender, conn);
|
||||
|
||||
AsyncPgConnection::try_from(client).await
|
||||
}
|
||||
.instrument(connect_span),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn spawn_db_notification_task(
|
||||
sender: flume::Sender<Notification>,
|
||||
mut conn: Connection<Socket, NoTlsStream>,
|
||||
) {
|
||||
crate::sync::spawn(async move {
|
||||
while let Some(res) = std::future::poll_fn(|cx| conn.poll_message(cx)).await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
tracing::error!("Database Connection {e:?}");
|
||||
return;
|
||||
}
|
||||
Ok(AsyncMessage::Notice(e)) => {
|
||||
tracing::warn!("Database Notice {e:?}");
|
||||
}
|
||||
Ok(AsyncMessage::Notification(notification)) => {
|
||||
if sender.send_async(notification).await.is_err() {
|
||||
tracing::warn!("Missed notification. Are we shutting down?");
|
||||
}
|
||||
}
|
||||
Ok(_) => {
|
||||
tracing::warn!("Unhandled AsyncMessage!!! Please contact the developer of this application");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn to_primitive(timestamp: time::OffsetDateTime) -> time::PrimitiveDateTime {
|
||||
let timestamp = timestamp.to_offset(time::UtcOffset::UTC);
|
||||
time::PrimitiveDateTime::new(timestamp.date(), timestamp.time())
|
||||
|
@ -849,7 +863,7 @@ impl QueueRepo for PostgresRepo {
|
|||
.inner
|
||||
.queue_notifications
|
||||
.entry(String::from(queue_name))
|
||||
.or_insert_with(|| Arc::new(Notify::new()))
|
||||
.or_insert_with(crate::sync::notify)
|
||||
.clone();
|
||||
|
||||
diesel::sql_query("LISTEN queue_status_channel;")
|
||||
|
@ -1330,20 +1344,27 @@ impl UploadRepo for PostgresRepo {
|
|||
.await
|
||||
.map_err(PostgresError::Diesel)?;
|
||||
|
||||
let opt = uploads
|
||||
let nested_opt = uploads
|
||||
.select(result)
|
||||
.filter(id.eq(upload_id.id))
|
||||
.get_result(&mut conn)
|
||||
.await
|
||||
.optional()
|
||||
.map_err(PostgresError::Diesel)?
|
||||
.flatten();
|
||||
.map_err(PostgresError::Diesel)?;
|
||||
|
||||
if let Some(upload_result) = opt {
|
||||
let upload_result: InnerUploadResult = serde_json::from_value(upload_result)
|
||||
.map_err(PostgresError::DeserializeUploadResult)?;
|
||||
match nested_opt {
|
||||
Some(opt) => {
|
||||
if let Some(upload_result) = opt {
|
||||
let upload_result: InnerUploadResult =
|
||||
serde_json::from_value(upload_result)
|
||||
.map_err(PostgresError::DeserializeUploadResult)?;
|
||||
|
||||
return Ok(upload_result.into());
|
||||
return Ok(upload_result.into());
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return Err(RepoError::AlreadyClaimed);
|
||||
}
|
||||
}
|
||||
|
||||
drop(conn);
|
||||
|
|
|
@ -29,9 +29,7 @@ macro_rules! b {
|
|||
($self:ident.$ident:ident, $expr:expr) => {{
|
||||
let $ident = $self.$ident.clone();
|
||||
|
||||
let span = tracing::Span::current();
|
||||
|
||||
actix_rt::task::spawn_blocking(move || span.in_scope(|| $expr))
|
||||
crate::sync::spawn_blocking(move || $expr)
|
||||
.await
|
||||
.map_err(SledError::from)
|
||||
.map_err(RepoError::from)?
|
||||
|
@ -175,7 +173,7 @@ impl SledRepo {
|
|||
|
||||
let this = self.db.clone();
|
||||
|
||||
actix_rt::task::spawn_blocking(move || {
|
||||
crate::sync::spawn_blocking(move || {
|
||||
let export = this.export();
|
||||
export_db.import(export);
|
||||
})
|
||||
|
@ -258,7 +256,7 @@ impl AliasAccessRepo for SledRepo {
|
|||
let alias_access = self.alias_access.clone();
|
||||
let inverse_alias_access = self.inverse_alias_access.clone();
|
||||
|
||||
let res = actix_rt::task::spawn_blocking(move || {
|
||||
let res = crate::sync::spawn_blocking(move || {
|
||||
(&alias_access, &inverse_alias_access).transaction(
|
||||
|(alias_access, inverse_alias_access)| {
|
||||
if let Some(old) = alias_access.insert(alias.to_bytes(), &value_bytes)? {
|
||||
|
@ -324,7 +322,7 @@ impl AliasAccessRepo for SledRepo {
|
|||
let alias_access = self.alias_access.clone();
|
||||
let inverse_alias_access = self.inverse_alias_access.clone();
|
||||
|
||||
let res = actix_rt::task::spawn_blocking(move || {
|
||||
let res = crate::sync::spawn_blocking(move || {
|
||||
(&alias_access, &inverse_alias_access).transaction(
|
||||
|(alias_access, inverse_alias_access)| {
|
||||
if let Some(old) = alias_access.remove(alias.to_bytes())? {
|
||||
|
@ -364,7 +362,7 @@ impl VariantAccessRepo for SledRepo {
|
|||
let variant_access = self.variant_access.clone();
|
||||
let inverse_variant_access = self.inverse_variant_access.clone();
|
||||
|
||||
let res = actix_rt::task::spawn_blocking(move || {
|
||||
let res = crate::sync::spawn_blocking(move || {
|
||||
(&variant_access, &inverse_variant_access).transaction(
|
||||
|(variant_access, inverse_variant_access)| {
|
||||
if let Some(old) = variant_access.insert(&key, &value_bytes)? {
|
||||
|
@ -434,7 +432,7 @@ impl VariantAccessRepo for SledRepo {
|
|||
let variant_access = self.variant_access.clone();
|
||||
let inverse_variant_access = self.inverse_variant_access.clone();
|
||||
|
||||
let res = actix_rt::task::spawn_blocking(move || {
|
||||
let res = crate::sync::spawn_blocking(move || {
|
||||
(&variant_access, &inverse_variant_access).transaction(
|
||||
|(variant_access, inverse_variant_access)| {
|
||||
if let Some(old) = variant_access.remove(&key)? {
|
||||
|
@ -678,7 +676,7 @@ impl QueueRepo for SledRepo {
|
|||
let queue = self.queue.clone();
|
||||
let job_state = self.job_state.clone();
|
||||
|
||||
let res = actix_rt::task::spawn_blocking(move || {
|
||||
let res = crate::sync::spawn_blocking(move || {
|
||||
(&queue, &job_state).transaction(|(queue, job_state)| {
|
||||
let state = JobState::pending();
|
||||
|
||||
|
@ -705,7 +703,7 @@ impl QueueRepo for SledRepo {
|
|||
.write()
|
||||
.unwrap()
|
||||
.entry(queue_name)
|
||||
.or_insert_with(|| Arc::new(Notify::new()))
|
||||
.or_insert_with(crate::sync::notify)
|
||||
.notify_one();
|
||||
|
||||
metrics_guard.disarm();
|
||||
|
@ -728,7 +726,7 @@ impl QueueRepo for SledRepo {
|
|||
let job_state = self.job_state.clone();
|
||||
|
||||
let span = tracing::Span::current();
|
||||
let opt = actix_rt::task::spawn_blocking(move || {
|
||||
let opt = crate::sync::spawn_blocking(move || {
|
||||
let _guard = span.enter();
|
||||
// Job IDs are generated with Uuid version 7 - defining their first bits as a
|
||||
// timestamp. Scanning a prefix should give us jobs in the order they were queued.
|
||||
|
@ -802,9 +800,7 @@ impl QueueRepo for SledRepo {
|
|||
notify
|
||||
} else {
|
||||
let mut guard = self.queue_notifier.write().unwrap();
|
||||
let entry = guard
|
||||
.entry(queue_name)
|
||||
.or_insert_with(|| Arc::new(Notify::new()));
|
||||
let entry = guard.entry(queue_name).or_insert_with(crate::sync::notify);
|
||||
Arc::clone(entry)
|
||||
};
|
||||
|
||||
|
@ -823,7 +819,7 @@ impl QueueRepo for SledRepo {
|
|||
|
||||
let job_state = self.job_state.clone();
|
||||
|
||||
actix_rt::task::spawn_blocking(move || {
|
||||
crate::sync::spawn_blocking(move || {
|
||||
if let Some(state) = job_state.get(&key)? {
|
||||
let new_state = JobState::running(worker_id);
|
||||
|
||||
|
@ -853,7 +849,7 @@ impl QueueRepo for SledRepo {
|
|||
let queue = self.queue.clone();
|
||||
let job_state = self.job_state.clone();
|
||||
|
||||
let res = actix_rt::task::spawn_blocking(move || {
|
||||
let res = crate::sync::spawn_blocking(move || {
|
||||
(&queue, &job_state).transaction(|(queue, job_state)| {
|
||||
queue.remove(&key[..])?;
|
||||
job_state.remove(&key[..])?;
|
||||
|
@ -1112,7 +1108,7 @@ impl HashRepo for SledRepo {
|
|||
None => (self.hashes_inverse.iter(), None),
|
||||
};
|
||||
|
||||
actix_rt::task::spawn_blocking(move || {
|
||||
crate::sync::spawn_blocking(move || {
|
||||
let page_iter = page_iter
|
||||
.keys()
|
||||
.rev()
|
||||
|
@ -1164,7 +1160,7 @@ impl HashRepo for SledRepo {
|
|||
let page_iter = self.hashes_inverse.range(..=date_nanos);
|
||||
let prev_iter = Some(self.hashes_inverse.range(date_nanos..));
|
||||
|
||||
actix_rt::task::spawn_blocking(move || {
|
||||
crate::sync::spawn_blocking(move || {
|
||||
let page_iter = page_iter
|
||||
.keys()
|
||||
.rev()
|
||||
|
@ -1292,7 +1288,7 @@ impl HashRepo for SledRepo {
|
|||
|
||||
let hash_variant_identifiers = self.hash_variant_identifiers.clone();
|
||||
|
||||
actix_rt::task::spawn_blocking(move || {
|
||||
crate::sync::spawn_blocking(move || {
|
||||
hash_variant_identifiers
|
||||
.compare_and_swap(key, Option::<&[u8]>::None, Some(value.as_bytes()))
|
||||
.map(|res| res.map_err(|_| VariantAlreadyExists))
|
||||
|
|
|
@ -34,9 +34,7 @@ macro_rules! b {
|
|||
($self:ident.$ident:ident, $expr:expr) => {{
|
||||
let $ident = $self.$ident.clone();
|
||||
|
||||
let span = tracing::Span::current();
|
||||
|
||||
actix_rt::task::spawn_blocking(move || span.in_scope(|| $expr))
|
||||
crate::sync::spawn_blocking(move || $expr)
|
||||
.await
|
||||
.map_err(SledError::from)
|
||||
.map_err(RepoError::from)?
|
||||
|
|
|
@ -277,7 +277,7 @@ impl Store for ObjectStore {
|
|||
|
||||
let object_id2 = object_id.clone();
|
||||
let upload_id2 = upload_id.clone();
|
||||
let handle = actix_rt::spawn(
|
||||
let handle = crate::sync::spawn(
|
||||
async move {
|
||||
let response = this
|
||||
.create_upload_part_request(
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use actix_rt::{task::JoinHandle, time::Sleep};
|
||||
use actix_web::web::Bytes;
|
||||
use flume::r#async::RecvStream;
|
||||
use futures_core::Stream;
|
||||
use std::{
|
||||
future::Future,
|
||||
|
@ -174,19 +175,25 @@ pin_project_lite::pin_project! {
|
|||
}
|
||||
}
|
||||
|
||||
enum IterStreamState<I, T> {
|
||||
enum IterStreamState<I, T>
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
New {
|
||||
iterator: I,
|
||||
buffer: usize,
|
||||
},
|
||||
Running {
|
||||
handle: JoinHandle<()>,
|
||||
receiver: tokio::sync::mpsc::Receiver<T>,
|
||||
receiver: RecvStream<'static, T>,
|
||||
},
|
||||
Pending,
|
||||
}
|
||||
|
||||
pub(crate) struct IterStream<I, T> {
|
||||
pub(crate) struct IterStream<I, T>
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
state: IterStreamState<I, T>,
|
||||
}
|
||||
|
||||
|
@ -287,14 +294,13 @@ where
|
|||
|
||||
match std::mem::replace(&mut this.state, IterStreamState::Pending) {
|
||||
IterStreamState::New { iterator, buffer } => {
|
||||
let (sender, receiver) = tracing::trace_span!(parent: None, "Create channel")
|
||||
.in_scope(|| tokio::sync::mpsc::channel(buffer));
|
||||
let (sender, receiver) = crate::sync::channel(buffer);
|
||||
|
||||
let mut handle = actix_rt::task::spawn_blocking(move || {
|
||||
let mut handle = crate::sync::spawn_blocking(move || {
|
||||
let iterator = iterator.into_iter();
|
||||
|
||||
for item in iterator {
|
||||
if sender.blocking_send(item).is_err() {
|
||||
if sender.send(item).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -304,14 +310,17 @@ where
|
|||
return Poll::Ready(None);
|
||||
}
|
||||
|
||||
this.state = IterStreamState::Running { handle, receiver };
|
||||
this.state = IterStreamState::Running {
|
||||
handle,
|
||||
receiver: receiver.into_stream(),
|
||||
};
|
||||
|
||||
self.poll_next(cx)
|
||||
}
|
||||
IterStreamState::Running {
|
||||
mut handle,
|
||||
mut receiver,
|
||||
} => match Pin::new(&mut receiver).poll_recv(cx) {
|
||||
} => match Pin::new(&mut receiver).poll_next(cx) {
|
||||
Poll::Ready(Some(item)) => {
|
||||
this.state = IterStreamState::Running { handle, receiver };
|
||||
|
||||
|
|
38
src/sync.rs
Normal file
38
src/sync.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use tokio::sync::{Notify, Semaphore};
|
||||
|
||||
pub(crate) fn channel<T>(bound: usize) -> (flume::Sender<T>, flume::Receiver<T>) {
|
||||
tracing::trace_span!(parent: None, "make channel").in_scope(|| flume::bounded(bound))
|
||||
}
|
||||
|
||||
pub(crate) fn notify() -> Arc<Notify> {
|
||||
Arc::new(bare_notify())
|
||||
}
|
||||
|
||||
pub(crate) fn bare_notify() -> Notify {
|
||||
tracing::trace_span!(parent: None, "make notifier").in_scope(Notify::new)
|
||||
}
|
||||
|
||||
pub(crate) fn bare_semaphore(permits: usize) -> Semaphore {
|
||||
tracing::trace_span!(parent: None, "make semaphore").in_scope(|| Semaphore::new(permits))
|
||||
}
|
||||
|
||||
pub(crate) fn spawn<F>(future: F) -> actix_rt::task::JoinHandle<F::Output>
|
||||
where
|
||||
F: std::future::Future + 'static,
|
||||
F::Output: 'static,
|
||||
{
|
||||
tracing::trace_span!(parent: None, "spawn task").in_scope(|| actix_rt::spawn(future))
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_blocking<F, Out>(function: F) -> actix_rt::task::JoinHandle<Out>
|
||||
where
|
||||
F: FnOnce() -> Out + Send + 'static,
|
||||
Out: Send + 'static,
|
||||
{
|
||||
let outer_span = tracing::Span::current();
|
||||
|
||||
tracing::trace_span!(parent: None, "spawn blocking task")
|
||||
.in_scope(|| actix_rt::task::spawn_blocking(move || outer_span.in_scope(function)))
|
||||
}
|
|
@ -13,8 +13,7 @@ struct TmpFile(PathBuf);
|
|||
|
||||
impl Drop for TmpFile {
|
||||
fn drop(&mut self) {
|
||||
tracing::trace_span!(parent: None, "Spawn task")
|
||||
.in_scope(|| actix_rt::spawn(tokio::fs::remove_file(self.0.clone())));
|
||||
crate::sync::spawn(tokio::fs::remove_file(self.0.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue