Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rft] refactor loadbalance module #172

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading