use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use crate::tenant::{Tenant, TenantId};
tokio::task_local! {
static CURRENT: TenantContext;
}
pub fn current_context() -> Option<TenantContext> {
CURRENT.try_with(Clone::clone).ok()
}
#[derive(Clone)]
pub struct TenantContext {
tenant: Tenant,
scope: Arc<RwLock<Scope>>,
slots: Arc<RwLock<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>>,
}
struct Scope {
cache_namespace: String,
session_namespace: String,
queue_namespace: String,
storage_root: PathBuf,
}
impl TenantContext {
pub fn new(tenant: Tenant) -> Self {
let namespace = format!("tenant:{}", tenant.slug);
Self {
tenant,
scope: Arc::new(RwLock::new(Scope {
cache_namespace: namespace.clone(),
session_namespace: namespace.clone(),
queue_namespace: namespace,
storage_root: PathBuf::new(),
})),
slots: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn tenant(&self) -> &Tenant {
&self.tenant
}
pub fn tenant_id(&self) -> TenantId {
self.tenant.id
}
pub fn cache_namespace(&self) -> String {
self.scope().cache_namespace.clone()
}
pub fn session_namespace(&self) -> String {
self.scope().session_namespace.clone()
}
pub fn queue_namespace(&self) -> String {
self.scope().queue_namespace.clone()
}
pub fn storage_root(&self) -> PathBuf {
self.scope().storage_root.clone()
}
pub fn set_cache_namespace(&self, namespace: impl Into<String>) {
self.scope_mut().cache_namespace = namespace.into();
}
pub fn set_session_namespace(&self, namespace: impl Into<String>) {
self.scope_mut().session_namespace = namespace.into();
}
pub fn set_queue_namespace(&self, namespace: impl Into<String>) {
self.scope_mut().queue_namespace = namespace.into();
}
pub fn set_storage_root(&self, root: impl Into<PathBuf>) {
self.scope_mut().storage_root = root.into();
}
pub fn insert<T: Any + Send + Sync>(&self, value: T) {
let mut slots = self
.slots
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner());
slots.insert(TypeId::of::<T>(), Arc::new(value));
}
pub fn get<T: Any + Send + Sync>(&self) -> Option<Arc<T>> {
let slots = self
.slots
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner());
slots
.get(&TypeId::of::<T>())
.and_then(|slot| Arc::clone(slot).downcast::<T>().ok())
}
pub fn get_cloned<T: Any + Send + Sync + Clone>(&self) -> Option<T> {
self.get::<T>().map(|slot| (*slot).clone())
}
fn scope(&self) -> std::sync::RwLockReadGuard<'_, Scope> {
self.scope
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn scope_mut(&self) -> std::sync::RwLockWriteGuard<'_, Scope> {
self.scope
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
}
#[allow(dead_code)] pub(crate) async fn scope_context<F: std::future::Future>(
context: TenantContext,
future: F,
) -> F::Output {
CURRENT.scope(context, future).await
}
impl std::fmt::Debug for TenantContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let scope = self.scope();
f.debug_struct("TenantContext")
.field("tenant", &self.tenant.slug)
.field("cache_namespace", &scope.cache_namespace)
.field("session_namespace", &scope.session_namespace)
.field("queue_namespace", &scope.queue_namespace)
.field("storage_root", &scope.storage_root)
.finish()
}
}