use super::*;
#[derive(Clone, Copy, Data, Debug, Eq, PartialEq)]
pub struct RateLimitConfig {
#[get(type(copy))]
pub(crate) requests_per_second: u64,
#[get(type(copy))]
pub(crate) burst: u64,
}
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct UpstreamTarget {
pub(crate) url: String,
#[get(type(copy))]
pub(crate) weight: u32,
}
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct GatewayRoute {
pub(crate) prefix: String,
#[get(type(copy))]
pub(crate) strip_prefix: bool,
#[get(type(clone))]
pub(crate) host: Option<String>,
pub(crate) upstreams: Vec<UpstreamTarget>,
#[get(type(copy))]
pub(crate) strategy: LoadBalanceStrategy,
#[get(type(copy))]
pub(crate) timeout_ms: u64,
#[get(type(copy))]
pub(crate) max_retries: usize,
#[get(type(copy))]
pub(crate) rate_limit: Option<RateLimitConfig>,
#[get(type(copy))]
pub(crate) use_http2: bool,
}
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct GatewayConfig {
pub(crate) routes: Vec<GatewayRoute>,
#[get(type(copy))]
pub(crate) max_body_size: usize,
#[get(type(copy))]
pub(crate) circuit_failure_threshold: u32,
#[get(type(copy))]
pub(crate) circuit_reset_ms: u64,
}
pub(crate) struct UpstreamRuntime {
pub(crate) base_url: String,
pub(crate) weight: u32,
pub(crate) active_conns: AtomicUsize,
pub(crate) consecutive_failures: AtomicU32,
pub(crate) open_until_ms: AtomicU64,
}
pub(crate) struct TokenBucketState {
pub(crate) tokens: f64,
pub(crate) last_refill: Instant,
}
pub(crate) struct TokenBucket {
pub(crate) rate_per_sec: f64,
pub(crate) capacity: f64,
pub(crate) state: Mutex<TokenBucketState>,
}
pub(crate) struct CompiledRoute {
pub(crate) route: GatewayRoute,
pub(crate) normalized_prefix: String,
pub(crate) upstreams: Vec<Arc<UpstreamRuntime>>,
pub(crate) cursor: AtomicUsize,
pub(crate) limiter: Option<TokenBucket>,
}
#[derive(Clone)]
pub struct GatewayRuntime {
pub(crate) config: GatewayConfig,
pub(crate) routes: Arc<Vec<CompiledRoute>>,
}
#[derive(Clone)]
pub struct GatewayPlugin {
pub(crate) runtime: GatewayRuntime,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct GatewayHook;