use super::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RouterConfig {
#[serde(default)]
pub strategy: RoutingStrategyConfig,
#[serde(default)]
pub circuit_breaker: CircuitBreakerConfig,
#[serde(default)]
pub load_balancer: LoadBalancerConfig,
}
#[allow(dead_code)]
impl RouterConfig {
pub fn merge(mut self, other: Self) -> Self {
self.strategy = other.strategy;
self.circuit_breaker = self.circuit_breaker.merge(other.circuit_breaker);
self.load_balancer = self.load_balancer.merge(other.load_balancer);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RoutingStrategyConfig {
RoundRobin,
LeastLatency,
LeastCost,
Random,
Weighted {
weights: std::collections::HashMap<String, f64>,
},
Priority {
priorities: std::collections::HashMap<String, u32>,
},
ABTest {
split_ratio: f64,
},
Custom {
logic: String,
},
}
impl Default for RoutingStrategyConfig {
fn default() -> Self {
Self::RoundRobin
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CircuitBreakerConfig {
#[serde(default = "default_failure_threshold")]
pub failure_threshold: u32,
#[serde(default = "default_recovery_timeout")]
pub recovery_timeout: u64,
#[serde(default = "default_min_requests")]
pub min_requests: u32,
#[serde(default = "default_success_threshold")]
pub success_threshold: u32,
}
impl Default for CircuitBreakerConfig {
fn default() -> Self {
Self {
failure_threshold: default_failure_threshold(),
recovery_timeout: default_recovery_timeout(),
min_requests: default_min_requests(),
success_threshold: 3,
}
}
}
#[allow(dead_code)]
impl CircuitBreakerConfig {
pub fn merge(mut self, other: Self) -> Self {
if other.failure_threshold != default_failure_threshold() {
self.failure_threshold = other.failure_threshold;
}
if other.recovery_timeout != default_recovery_timeout() {
self.recovery_timeout = other.recovery_timeout;
}
if other.min_requests != default_min_requests() {
self.min_requests = other.min_requests;
}
if other.success_threshold != 3 {
self.success_threshold = other.success_threshold;
}
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoadBalancerConfig {
#[serde(default = "default_true")]
pub health_check_enabled: bool,
#[serde(default)]
pub sticky_sessions: bool,
#[serde(default = "default_session_timeout")]
pub session_timeout: u64,
}
impl Default for LoadBalancerConfig {
fn default() -> Self {
Self {
health_check_enabled: true,
sticky_sessions: false,
session_timeout: 3600,
}
}
}
#[allow(dead_code)]
impl LoadBalancerConfig {
pub fn merge(mut self, other: Self) -> Self {
if !other.health_check_enabled {
self.health_check_enabled = other.health_check_enabled;
}
if other.sticky_sessions {
self.sticky_sessions = other.sticky_sessions;
}
if other.session_timeout != 3600 {
self.session_timeout = other.session_timeout;
}
self
}
}
fn default_success_threshold() -> u32 {
3
}
fn default_session_timeout() -> u64 {
3600
}
fn default_true() -> bool {
true
}