mod enforce;
mod measure;
mod store;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::common::disk_usage::DiskUsage;
use ahash::AHashMap;
use parking_lot::Mutex;
pub use self::enforce::ExceededVerdicts;
pub use self::measure::DiskFit;
pub use self::store::QUOTA_CONFIG_FILE;
use self::store::Store;
use super::config::QuotaConfig;
use super::error::QuotaResult;
use super::meter::Meter;
use super::status::QuotaStatus;
pub struct QuotaManager {
config: Store,
storage_path: PathBuf,
memory: Meter<Option<u8>>,
disk: Mutex<AHashMap<PathBuf, Arc<Meter<Option<DiskUsage>>>>>,
exceeded: ExceededVerdicts,
}
impl std::fmt::Debug for QuotaManager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("QuotaManager")
.field("config", &self.config.read())
.field("storage_path", &self.storage_path)
.finish_non_exhaustive()
}
}
impl Default for QuotaManager {
fn default() -> Self {
QuotaManager {
config: Store::ephemeral(),
storage_path: PathBuf::new(),
memory: Meter::default(),
disk: Mutex::new(AHashMap::new()),
exceeded: ExceededVerdicts::default(),
}
}
}
impl QuotaManager {
pub fn load_or_init(storage_path: &Path, from_settings: QuotaConfig) -> QuotaResult<Self> {
Ok(QuotaManager {
config: Store::load_or_init(storage_path, from_settings)?,
storage_path: storage_path.to_path_buf(),
memory: Meter::default(),
disk: Mutex::new(AHashMap::new()),
exceeded: ExceededVerdicts::default(),
})
}
pub fn config(&self) -> QuotaConfig {
self.config.read()
}
pub fn set_config(&self, config: QuotaConfig) -> QuotaResult<()> {
self.config.write(config)?;
self.exceeded.clear();
Ok(())
}
pub fn status(&self) -> QuotaStatus {
QuotaStatus {
config: self.config(),
usage: self.usage(),
peers: None,
}
}
}