diff --git a/common/base/src/constants.rs b/common/base/src/constants.rs index b1faf270..070ac727 100644 --- a/common/base/src/constants.rs +++ b/common/base/src/constants.rs @@ -17,6 +17,8 @@ pub const REGISTRY_PROTOCOL: &str = "registry_protocol"; pub const PROTOCOL: &str = "protocol"; pub const REGISTRY: &str = "registry"; +pub const INVOKE: &str = "$invoke"; +pub const INVOKE_ASYNC: &str = "$invokeAsync"; // URL key pub const DUBBO_KEY: &str = "dubbo"; diff --git a/dubbo/src/cluster/loadbalance/impls/random.rs b/dubbo/src/cluster/loadbalance/impls/random.rs index 3e1cf651..d1b2b18e 100644 --- a/dubbo/src/cluster/loadbalance/impls/random.rs +++ b/dubbo/src/cluster/loadbalance/impls/random.rs @@ -21,7 +21,7 @@ use std::{ }; use crate::{ - cluster::loadbalance::types::{LoadBalance, Metadata}, + cluster::loadbalance::types::{AbstractLoadBalance, Metadata}, codegen::RpcInvocation, }; @@ -43,16 +43,13 @@ impl Debug for RandomLoadBalance { } } -impl LoadBalance for RandomLoadBalance { - fn select( +impl AbstractLoadBalance for RandomLoadBalance { + fn do_select( &self, invokers: Arc>, _url: Option, _invocation: Arc, ) -> Option { - if invokers.is_empty() { - return None; - } let index = rand::random::() % invokers.len(); Some(invokers[index].clone()) } diff --git a/dubbo/src/cluster/loadbalance/impls/roundrobin.rs b/dubbo/src/cluster/loadbalance/impls/roundrobin.rs index 5fd0ed49..f07c7707 100644 --- a/dubbo/src/cluster/loadbalance/impls/roundrobin.rs +++ b/dubbo/src/cluster/loadbalance/impls/roundrobin.rs @@ -25,7 +25,7 @@ use std::{ }; use crate::{ - cluster::loadbalance::types::{LoadBalance, Metadata}, + cluster::loadbalance::types::{AbstractLoadBalance, Metadata}, codegen::RpcInvocation, }; @@ -61,16 +61,13 @@ impl RoundRobinLoadBalance { } } -impl LoadBalance for RoundRobinLoadBalance { - fn select( +impl AbstractLoadBalance for RoundRobinLoadBalance { + fn do_select( &self, invokers: Arc>, _url: Option, invocation: Arc, ) -> Option { - if invokers.is_empty() { - return None; - } let fingerprint = invocation.unique_fingerprint(); self.guarantee_counter_key(fingerprint.as_str()); let index = self diff --git a/dubbo/src/cluster/loadbalance/types.rs b/dubbo/src/cluster/loadbalance/types.rs index 9273d07e..d7a403dd 100644 --- a/dubbo/src/cluster/loadbalance/types.rs +++ b/dubbo/src/cluster/loadbalance/types.rs @@ -31,6 +31,32 @@ pub trait LoadBalance: Debug { ) -> Option; } +pub trait AbstractLoadBalance: LoadBalance { + fn do_select( + &self, + invokers: Arc>, + url: Option, + invocation: Arc, + ) -> Option; +} + +impl LoadBalance for AbstractLoadBalanceImpl { + fn select( + &self, + invokers: Arc>, + url: Option, + invocation: Arc, + ) -> Option { + if invokers.is_empty() { + return None; + } + if invokers.len() == 1 { + return Some(invokers[0].clone()); + } + self.do_select(invokers, url, invocation) + } +} + pub struct Metadata { pub name: &'static str, }