net-pool 0.1.3

A set of types for network connection pool.
Documentation
use crate::backend::{Address, BackendState};

pub enum Strategy {
    /// 普通哈希
    Hash,

    /// 一致性哈希
    ConsistentHash,

    /// 轮询
    RoundRobin,
}

/// 策略trait
pub trait LbStrategy: Send + Sync {
    /// 策略类型
    fn strategy(&self) -> Strategy;

    /// 判断一个后端地址是否存在
    fn contain(&self, hash_code: u64) -> bool;

    /// 添加一个后端地址
    fn add_backend(&self, addr: Address);

    /// 移除一个后端地址
    fn remove_backend(&self, addr: &Address) -> bool;

    /// 获取一个后端地址
    fn get_backend(&self, key: &str) -> Option<BackendState>;

    /// 获取所有后端地址
    fn get_backends(&self) -> Vec<BackendState>;

    /// 获取原始的所有后端地址
    fn get_origin_backends(&self) -> Vec<BackendState>;

    /// 获取一个后端地址, 通过通用key
    fn backend<KEY: AsRef<str>>(this: Box<dyn LbStrategy>, key: KEY) -> Option<BackendState>
    where
        Self: Sized,
    {
        this.get_backend(key.as_ref())
    }

    /// 使一个后端地址无效
    fn disable_backend(&self, addr: &Address) -> bool {
        self.remove_backend(addr)
    }

    /// 使一个后端地址有效
    fn enable_backend(&self, addr: &Address) {
        self.add_backend(addr.clone())
    }
}