pict-rs/src/concurrent_processor.rs

129 lines
3.6 KiB
Rust
Raw Normal View History

2021-10-21 00:28:40 +00:00
use crate::{
2022-03-24 22:09:15 +00:00
details::Details,
2021-10-21 00:28:40 +00:00
error::{Error, UploadError},
};
use actix_web::web;
use dashmap::{mapref::entry::Entry, DashMap};
use once_cell::sync::Lazy;
use std::{
future::Future,
path::PathBuf,
pin::Pin,
task::{Context, Poll},
};
use tokio::sync::oneshot::{Receiver, Sender};
use tracing::Span;
type OutcomeSender = Sender<(Details, web::Bytes)>;
type ProcessMapKey = (Vec<u8>, PathBuf);
type ProcessMap = DashMap<ProcessMapKey, Vec<OutcomeSender>>;
2021-10-21 00:28:40 +00:00
static PROCESS_MAP: Lazy<ProcessMap> = Lazy::new(DashMap::new);
struct CancelToken {
span: Span,
key: ProcessMapKey,
2021-10-21 00:28:40 +00:00
receiver: Option<Receiver<(Details, web::Bytes)>>,
}
pin_project_lite::pin_project! {
pub(super) struct CancelSafeProcessor<F> {
cancel_token: CancelToken,
#[pin]
fut: F,
}
}
impl<F> CancelSafeProcessor<F>
where
F: Future<Output = Result<(Details, web::Bytes), Error>>,
{
pub(super) fn new(hash: &[u8], path: PathBuf, fut: F) -> Self {
let key = (hash.to_vec(), path.clone());
let entry = PROCESS_MAP.entry(key.clone());
2021-10-21 00:28:40 +00:00
let (receiver, span) = match entry {
Entry::Vacant(vacant) => {
vacant.insert(Vec::new());
let span = tracing::info_span!(
"Processing image",
2022-11-26 17:17:53 +00:00
hash = &tracing::field::debug(&hex::encode(hash)),
2021-10-21 00:28:40 +00:00
path = &tracing::field::debug(&path),
completed = &tracing::field::Empty,
);
(None, span)
}
Entry::Occupied(mut occupied) => {
2022-04-07 17:56:40 +00:00
let (tx, rx) = tracing::trace_span!(parent: None, "Create channel")
.in_scope(tokio::sync::oneshot::channel);
2021-10-21 00:28:40 +00:00
occupied.get_mut().push(tx);
let span = tracing::info_span!(
"Waiting for processed image",
2022-11-26 17:17:53 +00:00
hash = &tracing::field::debug(&hex::encode(hash)),
2021-10-21 00:28:40 +00:00
path = &tracing::field::debug(&path),
);
(Some(rx), span)
}
};
CancelSafeProcessor {
2021-10-21 00:28:40 +00:00
cancel_token: CancelToken {
span,
key,
2021-10-21 00:28:40 +00:00
receiver,
},
fut,
}
2021-10-21 00:28:40 +00:00
}
}
impl<F> Future for CancelSafeProcessor<F>
where
F: Future<Output = Result<(Details, web::Bytes), Error>>,
{
type Output = Result<(Details, web::Bytes), Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.as_mut().project();
let span = &this.cancel_token.span;
let receiver = &mut this.cancel_token.receiver;
let key = &this.cancel_token.key;
2021-10-21 00:28:40 +00:00
let fut = this.fut;
span.in_scope(|| {
if let Some(ref mut rx) = receiver {
Pin::new(rx)
.poll(cx)
.map(|res| res.map_err(|_| UploadError::Canceled.into()))
} else {
fut.poll(cx).map(|res| {
let opt = PROCESS_MAP.remove(key);
2021-10-21 00:28:40 +00:00
res.map(|tup| {
if let Some((_, vec)) = opt {
for sender in vec {
let _ = sender.send(tup.clone());
}
}
tup
})
})
}
})
}
}
impl Drop for CancelToken {
fn drop(&mut self) {
if self.receiver.is_none() {
let completed = PROCESS_MAP.remove(&self.key).is_none();
2022-11-26 17:17:53 +00:00
self.span.record("completed", completed);
2021-10-21 00:28:40 +00:00
}
}
}