Skip to content

Commit

Permalink
[Rft] refactor loadbalance module
Browse files Browse the repository at this point in the history
  • Loading branch information
loongs-zhang committed Jan 9, 2024
1 parent 15b2ac2 commit adc212b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 2 additions & 0 deletions common/base/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
9 changes: 3 additions & 6 deletions dubbo/src/cluster/loadbalance/impls/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::{
};

use crate::{
cluster::loadbalance::types::{LoadBalance, Metadata},
cluster::loadbalance::types::{AbstractLoadBalance, Metadata},
codegen::RpcInvocation,
};

Expand All @@ -43,16 +43,13 @@ impl Debug for RandomLoadBalance {
}
}

impl LoadBalance for RandomLoadBalance {
fn select(
impl AbstractLoadBalance for RandomLoadBalance {
fn do_select(
&self,
invokers: Arc<Vec<Url>>,
_url: Option<Url>,
_invocation: Arc<RpcInvocation>,
) -> Option<Url> {
if invokers.is_empty() {
return None;
}
let index = rand::random::<usize>() % invokers.len();
Some(invokers[index].clone())
}
Expand Down
9 changes: 3 additions & 6 deletions dubbo/src/cluster/loadbalance/impls/roundrobin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::{
};

use crate::{
cluster::loadbalance::types::{LoadBalance, Metadata},
cluster::loadbalance::types::{AbstractLoadBalance, Metadata},
codegen::RpcInvocation,
};

Expand Down Expand Up @@ -61,16 +61,13 @@ impl RoundRobinLoadBalance {
}
}

impl LoadBalance for RoundRobinLoadBalance {
fn select(
impl AbstractLoadBalance for RoundRobinLoadBalance {
fn do_select(
&self,
invokers: Arc<Vec<Url>>,
_url: Option<Url>,
invocation: Arc<RpcInvocation>,
) -> Option<Url> {
if invokers.is_empty() {
return None;
}
let fingerprint = invocation.unique_fingerprint();
self.guarantee_counter_key(fingerprint.as_str());
let index = self
Expand Down
26 changes: 26 additions & 0 deletions dubbo/src/cluster/loadbalance/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ pub trait LoadBalance: Debug {
) -> Option<Url>;
}

pub trait AbstractLoadBalance: LoadBalance {
fn do_select(
&self,
invokers: Arc<Vec<Url>>,
url: Option<Url>,
invocation: Arc<RpcInvocation>,
) -> Option<Url>;
}

impl<AbstractLoadBalanceImpl: AbstractLoadBalance> LoadBalance for AbstractLoadBalanceImpl {
fn select(
&self,
invokers: Arc<Vec<Url>>,
url: Option<Url>,
invocation: Arc<RpcInvocation>,
) -> Option<Url> {
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,
}
Expand Down

0 comments on commit adc212b

Please sign in to comment.