use crate::error::Result;
use async_trait::async_trait;
use std::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Primitive {
Kv,
Queue,
Blob,
Auth,
Config,
RateLimit,
Schedule,
Pubsub,
}
impl Primitive {
pub fn as_str(self) -> &'static str {
match self {
Primitive::Kv => "kv",
Primitive::Queue => "queue",
Primitive::Blob => "blob",
Primitive::Auth => "auth",
Primitive::Config => "config",
Primitive::RateLimit => "ratelimit",
Primitive::Schedule => "schedule",
Primitive::Pubsub => "pubsub",
}
}
pub fn parse(value: &str) -> Result<Self> {
match value {
"kv" => Ok(Self::Kv),
"queue" => Ok(Self::Queue),
"blob" => Ok(Self::Blob),
"auth" => Ok(Self::Auth),
"config" => Ok(Self::Config),
"ratelimit" => Ok(Self::RateLimit),
"schedule" => Ok(Self::Schedule),
"pubsub" => Ok(Self::Pubsub),
_ => Err(crate::ForgeError::invalid(
"backend must be kv, queue, blob, auth, config, ratelimit, schedule, or pubsub",
)),
}
}
}
#[async_trait]
pub trait BackendLifecycle: Send + Sync {
fn name(&self) -> &'static str;
fn primitive(&self) -> Primitive;
fn durable(&self) -> bool {
true
}
fn caveats(&self) -> &'static str {
"none"
}
async fn maintain(&self) -> Result<()> {
Ok(())
}
async fn close(&self) -> Result<()> {
Ok(())
}
}
pub trait KvBackend: crate::kv::Kv + BackendLifecycle {}
impl<T: crate::kv::Kv + BackendLifecycle> KvBackend for T {}
pub trait QueueBackend: crate::queue::Queue + BackendLifecycle {}
impl<T: crate::queue::Queue + BackendLifecycle> QueueBackend for T {}
pub trait ConfigStoreBackend: crate::config_store::ConfigStore + BackendLifecycle {}
impl<T: crate::config_store::ConfigStore + BackendLifecycle> ConfigStoreBackend for T {}
pub trait RateLimitBackend: crate::ratelimit::RateLimit + BackendLifecycle {}
impl<T: crate::ratelimit::RateLimit + BackendLifecycle> RateLimitBackend for T {}
pub trait BlobBackend: crate::blob::Blob + BackendLifecycle {}
impl<T: crate::blob::Blob + BackendLifecycle> BlobBackend for T {}
pub trait AuthBackend: crate::auth::Auth + BackendLifecycle {}
impl<T: crate::auth::Auth + BackendLifecycle> AuthBackend for T {}
pub trait ScheduleBackend: crate::schedule::Schedule + BackendLifecycle {}
impl<T: crate::schedule::Schedule + BackendLifecycle> ScheduleBackend for T {}
pub trait PubsubBackend: crate::pubsub::Pubsub + BackendLifecycle {}
impl<T: crate::pubsub::Pubsub + BackendLifecycle> PubsubBackend for T {}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct BackendInfo {
pub primitive: Primitive,
pub provider: &'static str,
pub durable: bool,
pub caveats: &'static str,
}
impl BackendInfo {
pub fn new(
primitive: Primitive,
provider: &'static str,
durable: bool,
caveats: &'static str,
) -> Self {
Self {
primitive,
provider,
durable,
caveats,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct BackendCapabilities {
pub backends: Vec<BackendInfo>,
}
impl BackendCapabilities {
pub fn new(backends: Vec<BackendInfo>) -> Self {
Self { backends }
}
}
impl fmt::Display for BackendCapabilities {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "forge backend capabilities:")?;
for b in &self.backends {
writeln!(
f,
" {:<10} {:<12} durable={:<3} caveats={}",
b.primitive.as_str(),
b.provider,
if b.durable { "yes" } else { "no" },
b.caveats,
)?;
}
Ok(())
}
}
#[async_trait]
impl BackendLifecycle for crate::kv::PgKv {
fn name(&self) -> &'static str {
"postgres"
}
fn primitive(&self) -> Primitive {
Primitive::Kv
}
async fn maintain(&self) -> Result<()> {
self.sweep().await.map(|_| ())
}
}
#[async_trait]
impl BackendLifecycle for crate::kv::MemKv {
fn name(&self) -> &'static str {
"memory"
}
fn primitive(&self) -> Primitive {
Primitive::Kv
}
fn durable(&self) -> bool {
false
}
fn caveats(&self) -> &'static str {
"in-process, not shared across replicas"
}
async fn maintain(&self) -> Result<()> {
self.purge_expired();
Ok(())
}
}
#[async_trait]
impl BackendLifecycle for crate::queue::PgQueue {
fn name(&self) -> &'static str {
"postgres"
}
fn primitive(&self) -> Primitive {
Primitive::Queue
}
async fn maintain(&self) -> Result<()> {
self.maintenance().await
}
}
#[async_trait]
impl BackendLifecycle for crate::ratelimit::PgRateLimit {
fn name(&self) -> &'static str {
"postgres"
}
fn primitive(&self) -> Primitive {
Primitive::RateLimit
}
async fn maintain(&self) -> Result<()> {
self.sweep().await.map(|_| ())
}
}
#[async_trait]
impl BackendLifecycle for crate::auth::PgAuth {
fn name(&self) -> &'static str {
"postgres"
}
fn primitive(&self) -> Primitive {
Primitive::Auth
}
async fn maintain(&self) -> Result<()> {
self.sweep().await.map(|_| ())
}
}
#[async_trait]
impl BackendLifecycle for crate::config_store::PgConfig {
fn name(&self) -> &'static str {
"postgres"
}
fn primitive(&self) -> Primitive {
Primitive::Config
}
}
#[async_trait]
impl BackendLifecycle for crate::schedule::PgSchedule {
fn name(&self) -> &'static str {
"postgres"
}
fn primitive(&self) -> Primitive {
Primitive::Schedule
}
}
#[async_trait]
impl BackendLifecycle for crate::pubsub::PgPubsub {
fn name(&self) -> &'static str {
"postgres"
}
fn primitive(&self) -> Primitive {
Primitive::Pubsub
}
fn durable(&self) -> bool {
false
}
fn caveats(&self) -> &'static str {
"at-most-once, non-durable"
}
async fn close(&self) -> Result<()> {
self.shutdown().await
}
}
#[async_trait]
impl BackendLifecycle for crate::blob::PgBlob {
fn name(&self) -> &'static str {
"postgres"
}
fn primitive(&self) -> Primitive {
Primitive::Blob
}
}
#[async_trait]
impl BackendLifecycle for crate::blob::FsBlob {
fn name(&self) -> &'static str {
"filesystem"
}
fn primitive(&self) -> Primitive {
Primitive::Blob
}
fn caveats(&self) -> &'static str {
"local-dir, shared-mount-for-multi-replica, put-not-atomic-with-app-sql"
}
async fn maintain(&self) -> Result<()> {
self.sweep_orphans().await.map(|_| ())
}
}