A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::{
byte_budget::BudgetedBytes,
coalescer::CoalescerError,
media_process::MediaBytes,
output_format::OutputFormat,
server::{response::error::text_with_reason, transform::TransformRuntime},
};
use axum::{
http::{HeaderValue, StatusCode, header},
response::Response,
};
use std::{future::Future, time::Instant};
pub(in crate::server) struct CachedTransformRequest<'a, F> {
pub(in crate::server) runtime: &'a TransformRuntime,
pub(in crate::server) cache_key: String,
pub(in crate::server) format: OutputFormat,
pub(in crate::server) deadline: Option<Instant>,
pub(in crate::server) work: F,
}
pub(in crate::server) fn cached_transform_hit(
runtime: &TransformRuntime,
cache_key: &str,
) -> Option<crate::transform_cache::CachedTransform> {
runtime.cache().get(cache_key)
}
pub(in crate::server) fn cached_transform_failed(
runtime: &TransformRuntime,
cache_key: &str,
) -> bool {
runtime.failed_transforms().contains_key(cache_key)
}
pub(in crate::server) fn record_cached_transform_failure(
runtime: &TransformRuntime,
cache_key: String,
) {
runtime.failed_transforms().insert(cache_key, ());
}
pub(in crate::server) async fn cached_transform<F, Fut>(
request: CachedTransformRequest<'_, F>,
) -> Result<BudgetedBytes, CoalescerError>
where
F: FnOnce() -> Fut,
Fut: Future<Output = anyhow::Result<MediaBytes>>,
{
let CachedTransformRequest {
runtime,
cache_key,
format,
deadline,
work,
} = request;
let result = runtime
.cache()
.get_or_run(cache_key, format, deadline, work)
.await;
if matches!(
result,
Err(CoalescerError::WorkFailed | CoalescerError::TooLargeToProcess)
) {
runtime.metrics().transform_failures.inc();
}
result
}
pub(in crate::server) fn coalescer_error_is_transient(error: CoalescerError) -> bool {
!matches!(
error,
CoalescerError::WorkFailed | CoalescerError::TooLargeToProcess
)
}
pub(in crate::server) fn forbid_storing(response: &mut Response) {
let headers = response.headers_mut();
headers.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
headers.insert("CDN-Cache-Control", HeaderValue::from_static("no-store"));
}
pub(in crate::server) fn coalescer_failure_response(
error: CoalescerError,
timeout_code: &'static str,
) -> Option<Response> {
let reason = match error {
CoalescerError::WorkFailed | CoalescerError::TooLargeToProcess => return None,
CoalescerError::RequestTimeout => {
return Some(text_with_reason(
StatusCode::GATEWAY_TIMEOUT,
"Gateway Timeout",
timeout_code,
));
}
CoalescerError::BufferBudgetExhausted => "transform_output_budget_exhausted",
CoalescerError::AllocationFailed => "native_transform_allocation_failed",
CoalescerError::Overloaded => "native_transform_queue_full",
CoalescerError::Unavailable => "native_transform_unavailable",
CoalescerError::WorkCancelled => "transform_work_cancelled",
};
Some(text_with_reason(
StatusCode::SERVICE_UNAVAILABLE,
"Service Unavailable",
reason,
))
}