Skip to content

Commit

Permalink
endpoint: 生成代码时,按需要添加R的泛型支持
Browse files Browse the repository at this point in the history
  • Loading branch information
icycrystal4 committed Dec 15, 2023
1 parent 388cd8c commit 0e8fd81
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 275 deletions.
296 changes: 39 additions & 257 deletions endpoint/src/topo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,264 +10,46 @@ pub type TopologyProtocol<E, P> = Topologies<E, P>;
// 2. trait => where表示,为Topologies实现trait,满足where的条件.
// 第一个参数必须是self,否则无法dispatcher
// 3. 如果trait是pub的,则同时会创建这个trait。非pub的trait,只会为Topologies实现
// procs::topology_dispatcher! {
// #[derive(Clone)]
// pub enum Topologies<E, P> {
// MsgQue(crate::msgque::topo::MsgQue<E, P>),
// RedisService(crate::redisservice::topo::RedisService<E, P>),
// CacheService(crate::cacheservice::topo::CacheService<E, P>),
// PhantomService(crate::phantomservice::topo::PhantomService<E, P>),
// KvService(crate::kv::topo::KvService<E, P>),
// UuidService(crate::uuid::topo::UuidService<E, P>),
// }
procs::topology_dispatcher! {
#[derive(Clone)]
pub enum Topologies<E, P> {
MsgQue(crate::msgque::topo::MsgQue<E, P>),
RedisService(crate::redisservice::topo::RedisService<E, P>),
CacheService(crate::cacheservice::topo::CacheService<E, P>),
PhantomService(crate::phantomservice::topo::PhantomService<E, P>),
KvService(crate::kv::topo::KvService<E, P>),
UuidService(crate::uuid::topo::UuidService<E, P>),
}

pub trait Endpoint: Sized + Send + Sync {
type Item;
fn send(&self, req: Self::Item);
fn shard_idx(&self, _hash: i64) -> usize {todo!("shard_idx not implemented");}
fn available(&self) -> bool {todo!("available not implemented");}
fn addr(&self) -> &str {"addr not implemented"}
fn build_o<P:Protocol>(_addr: &str, _p: P, _r: Resource, _service: &str, _to: Timeout, _o: ResOption) -> Self {todo!("build not implemented")}
fn build<P:Protocol>(addr: &str, p: P, r: Resource, service: &str, to: Timeout) -> Self {Self::build_o(addr, p, r, service, to, Default::default())}
} => where P:Protocol, E:Endpoint<Item = R> + Inited, R: Request

pub trait Topology : Endpoint + Hash{
fn exp_sec(&self) -> u32 {86400}
} => where P:Protocol, E:Endpoint<Item = R>, R:Request, Topologies<E, P>: Endpoint

trait Inited {
fn inited(&self) -> bool;
} => where E:Inited

trait TopologyWrite {
fn update(&mut self, name: &str, cfg: &str);
fn disgroup<'a>(&self, _path: &'a str, cfg: &'a str) -> Vec<(&'a str, &'a str)>;
fn need_load(&self) -> bool;
fn load(&mut self) -> bool;
} => where P:Protocol, E:Endpoint

trait Hash {
fn hash<S: HashKey>(&self, key: &S) -> i64;
} => where P:Protocol, E:Endpoint,

// pub trait Endpoint: Sized + Send + Sync {
// type Item;
// fn send(&self, req: Self::Item);
// fn shard_idx(&self, _hash: i64) -> usize {todo!("shard_idx not implemented");}
// fn available(&self) -> bool {todo!("available not implemented");}
// fn addr(&self) -> &str {"addr not implemented"}
// fn build_o<P:Protocol>(_addr: &str, _p: P, _r: Resource, _service: &str, _to: Timeout, _o: ResOption) -> Self {todo!("build not implemented")}
// fn build<P:Protocol>(addr: &str, p: P, r: Resource, service: &str, to: Timeout) -> Self {Self::build_o(addr, p, r, service, to, Default::default())}
// } => where P:Protocol, E:Endpoint<Item = R> + Inited, R: Request

// pub trait Topology : Endpoint + Hash{
// fn exp_sec(&self) -> u32 {86400}
// } => where P:Protocol, E:Endpoint<Item = R>, R:Request, Topologies<E, P>: Endpoint

// trait Inited {
// fn inited(&self) -> bool;
// } => where E:Inited

// trait TopologyWrite {
// fn update(&mut self, name: &str, cfg: &str);
// fn disgroup<'a>(&self, _path: &'a str, cfg: &'a str) -> Vec<(&'a str, &'a str)>;
// fn need_load(&self) -> bool;
// fn load(&mut self) -> bool;
// } => where P:Protocol, E:Endpoint<Item = R>

// trait Hash {
// fn hash<S: HashKey>(&self, key: &S) -> i64;
// } => where P:Protocol, E:Endpoint<Item = R>, R:Request

// }

// Recursive expansion of topology_dispatcher! macro
// ==================================================

#[derive(Clone)]
pub enum Topologies<E, P> {
MsgQue(crate::msgque::topo::MsgQue<E, P>),
RedisService(crate::redisservice::topo::RedisService<E, P>),
CacheService(crate::cacheservice::topo::CacheService<E, P>),
PhantomService(crate::phantomservice::topo::PhantomService<E, P>),
KvService(crate::kv::topo::KvService<E, P>),
UuidService(crate::uuid::topo::UuidService<E, P>),
}
impl<E, P> Topologies<E, P> {
pub fn try_from(p: P, endpoint: &str) -> std::io::Result<Self> {
match endpoint {
"msg" | "mq" => Ok(Self::MsgQue(p.into())),
"redis" | "rs" => Ok(Self::RedisService(p.into())),
"cache" | "cs" => Ok(Self::CacheService(p.into())),
"phantom" | "ps" | "pt" => Ok(Self::PhantomService(p.into())),
"kv" | "ks" => Ok(Self::KvService(p.into())),
"uuid" | "us" => Ok(Self::UuidService(p.into())),
_ => todo!(),
}
}
}
pub trait Endpoint: Sized + Send + Sync {
type Item;
fn send(&self, req: Self::Item);

fn shard_idx(&self, _hash: i64) -> usize {
{
todo!();
};
}
fn available(&self) -> bool {
{
todo!();
};
}
fn addr(&self) -> &str {
"addr not implemented"
}
fn build_o<P: Protocol>(
_addr: &str,
_p: P,
_r: Resource,
_service: &str,
_to: Timeout,
_o: ResOption,
) -> Self {
{
todo!();
}
}
fn build<P: Protocol>(addr: &str, p: P, r: Resource, service: &str, to: Timeout) -> Self {
Self::build_o(addr, p, r, service, to, Default::default())
}
}
impl<E, R, P> Endpoint for Topologies<E, P>
where
P: Protocol,
E: Endpoint<Item = R> + Inited,
R: Request,
{
type Item = R;
#[inline]
fn send(&self, req: Self::Item) {
match self {
Topologies::MsgQue(p) => Endpoint::send(p, req),
Topologies::RedisService(p) => Endpoint::send(p, req),
Topologies::CacheService(p) => Endpoint::send(p, req),
Topologies::PhantomService(p) => Endpoint::send(p, req),
Topologies::KvService(p) => Endpoint::send(p, req),
Topologies::UuidService(p) => Endpoint::send(p, req),
}
}
#[inline]
fn shard_idx(&self, _hash: i64) -> usize {
match self {
Topologies::MsgQue(p) => Endpoint::shard_idx(p, _hash),
Topologies::RedisService(p) => Endpoint::shard_idx(p, _hash),
Topologies::CacheService(p) => Endpoint::shard_idx(p, _hash),
Topologies::PhantomService(p) => Endpoint::shard_idx(p, _hash),
Topologies::KvService(p) => Endpoint::shard_idx(p, _hash),
Topologies::UuidService(p) => Endpoint::shard_idx(p, _hash),
}
}
#[inline]
fn available(&self) -> bool {
match self {
Topologies::MsgQue(p) => Endpoint::available(p),
Topologies::RedisService(p) => Endpoint::available(p),
Topologies::CacheService(p) => Endpoint::available(p),
Topologies::PhantomService(p) => Endpoint::available(p),
Topologies::KvService(p) => Endpoint::available(p),
Topologies::UuidService(p) => Endpoint::available(p),
}
}
#[inline]
fn addr(&self) -> &str {
match self {
Topologies::MsgQue(p) => Endpoint::addr(p),
Topologies::RedisService(p) => Endpoint::addr(p),
Topologies::CacheService(p) => Endpoint::addr(p),
Topologies::PhantomService(p) => Endpoint::addr(p),
Topologies::KvService(p) => Endpoint::addr(p),
Topologies::UuidService(p) => Endpoint::addr(p),
}
}
}
pub trait Topology: Endpoint + Hash {
fn exp_sec(&self) -> u32 {
86400
}
}
impl<E, R, P> Topology for Topologies<E, P>
where
P: Protocol,
E: Endpoint<Item = R>,
R: Request,
Topologies<E, P>: Endpoint,
{
#[inline]
fn exp_sec(&self) -> u32 {
match self {
Topologies::MsgQue(p) => Topology::exp_sec(p),
Topologies::RedisService(p) => Topology::exp_sec(p),
Topologies::CacheService(p) => Topology::exp_sec(p),
Topologies::PhantomService(p) => Topology::exp_sec(p),
Topologies::KvService(p) => Topology::exp_sec(p),
Topologies::UuidService(p) => Topology::exp_sec(p),
}
}
}
impl<E, P> Inited for Topologies<E, P>
where
E: Inited,
{
#[inline]
fn inited(&self) -> bool {
match self {
Topologies::MsgQue(p) => Inited::inited(p),
Topologies::RedisService(p) => Inited::inited(p),
Topologies::CacheService(p) => Inited::inited(p),
Topologies::PhantomService(p) => Inited::inited(p),
Topologies::KvService(p) => Inited::inited(p),
Topologies::UuidService(p) => Inited::inited(p),
}
}
}
impl<E, P> TopologyWrite for Topologies<E, P>
where
P: Protocol,
E: Endpoint,
{
#[inline]
fn update(&mut self, name: &str, cfg: &str) {
match self {
Topologies::MsgQue(p) => TopologyWrite::update(p, name, cfg),
Topologies::RedisService(p) => TopologyWrite::update(p, name, cfg),
Topologies::CacheService(p) => TopologyWrite::update(p, name, cfg),
Topologies::PhantomService(p) => TopologyWrite::update(p, name, cfg),
Topologies::KvService(p) => TopologyWrite::update(p, name, cfg),
Topologies::UuidService(p) => TopologyWrite::update(p, name, cfg),
}
}
#[inline]
fn disgroup<'a>(&self, _path: &'a str, cfg: &'a str) -> Vec<(&'a str, &'a str)> {
match self {
Topologies::MsgQue(p) => TopologyWrite::disgroup(p, _path, cfg),
Topologies::RedisService(p) => TopologyWrite::disgroup(p, _path, cfg),
Topologies::CacheService(p) => TopologyWrite::disgroup(p, _path, cfg),
Topologies::PhantomService(p) => TopologyWrite::disgroup(p, _path, cfg),
Topologies::KvService(p) => TopologyWrite::disgroup(p, _path, cfg),
Topologies::UuidService(p) => TopologyWrite::disgroup(p, _path, cfg),
}
}
#[inline]
fn need_load(&self) -> bool {
match self {
Topologies::MsgQue(p) => TopologyWrite::need_load(p),
Topologies::RedisService(p) => TopologyWrite::need_load(p),
Topologies::CacheService(p) => TopologyWrite::need_load(p),
Topologies::PhantomService(p) => TopologyWrite::need_load(p),
Topologies::KvService(p) => TopologyWrite::need_load(p),
Topologies::UuidService(p) => TopologyWrite::need_load(p),
}
}
#[inline]
fn load(&mut self) -> bool {
match self {
Topologies::MsgQue(p) => TopologyWrite::load(p),
Topologies::RedisService(p) => TopologyWrite::load(p),
Topologies::CacheService(p) => TopologyWrite::load(p),
Topologies::PhantomService(p) => TopologyWrite::load(p),
Topologies::KvService(p) => TopologyWrite::load(p),
Topologies::UuidService(p) => TopologyWrite::load(p),
}
}
}
impl<E, P> Hash for Topologies<E, P>
where
P: Protocol,
E: Endpoint,
{
#[inline]
fn hash<S: HashKey>(&self, key: &S) -> i64 {
match self {
Topologies::MsgQue(p) => Hash::hash(p, key),
Topologies::RedisService(p) => Hash::hash(p, key),
Topologies::CacheService(p) => Hash::hash(p, key),
Topologies::PhantomService(p) => Hash::hash(p, key),
Topologies::KvService(p) => Hash::hash(p, key),
Topologies::UuidService(p) => Hash::hash(p, key),
}
}
}

// 从环境变量获取是否开启后端资源访问的性能模式
Expand Down
Loading

0 comments on commit 0e8fd81

Please sign in to comment.