Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ tls-rustls = [
"dep:rustls-platform-verifier",
"dep:rustls-webpki",
]
__unstable = []

[dependencies]
base64 = "0.22"
Expand Down
6 changes: 3 additions & 3 deletions grpc/src/client/load_balancing/child_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use crate::rt::GrpcRuntime;

// An LbPolicy implementation that manages multiple children.
#[derive(Debug)]
pub(crate) struct ChildManager<T: Debug> {
pub struct ChildManager<T: Debug> {
subchannel_to_child_idx: HashMap<WeakSubchannel, usize>,
handle_to_child_idx: HashMap<ChildHandle, usize>,
children: Vec<Child<T>>,
Expand All @@ -61,7 +61,7 @@ pub(crate) struct ChildManager<T: Debug> {

#[non_exhaustive]
#[derive(Debug)]
pub(crate) struct Child<T> {
pub struct Child<T> {
pub identifier: T,
pub builder: Arc<DynLbPolicyBuilder>,
pub state: LbState,
Expand All @@ -70,7 +70,7 @@ pub(crate) struct Child<T> {
}

/// A collection of data sent to a child of the ChildManager.
pub(crate) struct ChildUpdate<'a, T> {
pub struct ChildUpdate<'a, T> {
/// The identifier the ChildManager should use for this child.
pub child_identifier: T,
/// The builder the ChildManager should use to create this child if it does
Expand Down
4 changes: 2 additions & 2 deletions grpc/src/client/load_balancing/graceful_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::client::name_resolution::ResolverUpdate;
use crate::rt::GrpcRuntime;

#[derive(Debug)]
pub(crate) struct GracefulSwitchLbConfig {
pub struct GracefulSwitchLbConfig {
child_builder: Arc<DynLbPolicyBuilder>,
child_config: Option<DynLbConfig>,
}
Expand All @@ -56,7 +56,7 @@ pub(crate) struct GracefulSwitchLbConfig {
/// active policy is not READY, graceful switch will promote the pending policy
/// to active and tear down the previously active policy.
#[derive(Debug)]
pub(crate) struct GracefulSwitchPolicy {
pub struct GracefulSwitchPolicy {
child_manager: ChildManager<()>, // Child ID empty - only the name of the child LB policy matters.
last_update: Option<LbState>, // Saves the last output LbState to determine if an update is needed.
active_child_builder: Option<Arc<DynLbPolicyBuilder>>,
Expand Down
2 changes: 1 addition & 1 deletion grpc/src/client/load_balancing/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use crate::core::RequestHeaders;
/// Note that Lazy can only properly wrap a policy whose config is Clone, as it
/// needs to store the config until the child is built.
#[derive(Debug)]
pub(crate) struct Lazy<T: LbPolicyBuilder> {
pub struct Lazy<T: LbPolicyBuilder> {
inner: Inner<T>,
}

Expand Down
52 changes: 26 additions & 26 deletions grpc/src/client/load_balancing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ use crate::core::RequestHeaders;
use crate::metadata::MetadataMap;
use crate::rt::GrpcRuntime;

pub(crate) mod child_manager;
pub(crate) mod graceful_switch;
pub(crate) mod lazy;
pub(crate) mod pick_first;
pub(crate) mod round_robin;
pub(crate) mod subchannel;
pub(crate) mod subchannel_sharing;

pub mod child_manager;
pub mod graceful_switch;
pub mod lazy;
pub mod pick_first;
pub mod registry;
pub mod round_robin;
pub mod subchannel;
pub use registry::GLOBAL_LB_REGISTRY;

#[cfg(test)]
pub(crate) mod test_utils;

pub(crate) mod registry;
pub(crate) use registry::GLOBAL_LB_REGISTRY;

/// An LB policy factory that produces LbPolicy instances used by the channel
/// to manage connections and pick connections for RPCs.
pub(crate) trait LbPolicyBuilder: Send + Sync + Debug + 'static {
pub trait LbPolicyBuilder: Send + Sync + Debug + 'static {
type LbPolicy: LbPolicy;

/// Builds and returns a new LB policy instance.
Expand Down Expand Up @@ -88,7 +88,7 @@ pub(crate) trait LbPolicyBuilder: Send + Sync + Debug + 'static {
/// LB policies are responsible for creating connections (modeled as
/// Subchannels) and producing Picker instances for picking connections for
/// RPCs.
pub(crate) trait LbPolicy: Send + Sync + Debug + 'static {
pub trait LbPolicy: Send + Sync + Debug + 'static {
type LbConfig: Any + Send + Sync + Debug + 'static;

/// Called by the channel when the name resolver produces a new set of
Expand Down Expand Up @@ -121,7 +121,7 @@ pub(crate) trait LbPolicy: Send + Sync + Debug + 'static {
/// A collection of data configured on the channel that is constructing this
/// LbPolicy.
#[derive(Debug)]
pub(crate) struct LbPolicyOptions {
pub struct LbPolicyOptions {
/// A hook into the channel's work scheduler that allows the LbPolicy to
/// request the ability to perform operations on the ChannelController.
pub work_scheduler: Arc<dyn WorkScheduler>,
Expand All @@ -132,13 +132,13 @@ pub(crate) struct LbPolicyOptions {
/// to be printed more readily. Blanket implemented on all types that are Any +
/// Send + Debug. `dyn WorkDataTrait` also implements downcast methods like
/// [`Any`] for convenience.
pub(crate) trait WorkDataTrait: Any + Send + Debug {}
pub trait WorkDataTrait: Any + Send + Debug {}

impl<T: Any + Send + Debug> WorkDataTrait for T {}

impl dyn WorkDataTrait {
/// Like [`Box<dyn Any>::downcast`] but for this wrapper trait.
pub(crate) fn downcast<T: Any>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
pub fn downcast<T: Any>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
// If we directly call downcast then we can't return `Self` anymore
// (only a Box<dyn Any + Send>), so we first have to check `is` and only
// downcast when we know it will succeed.
Expand All @@ -152,26 +152,26 @@ impl dyn WorkDataTrait {
/// Like
/// [`downcast_ref`](https://doc.rust-lang.org/std/any/trait.Any.html#method.downcast_ref)
/// implemented on [`dyn Any`](Any), but for this wrapper trait.
pub(crate) fn downcast_ref<T: Any>(&self) -> Option<&T> {
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
(self as &(dyn Any + Send)).downcast_ref::<T>()
}

/// Like
/// [`downcast_mut`](https://doc.rust-lang.org/std/any/trait.Any.html#method.downcast_mut)
/// implemented on [`dyn Any`](Any), but for this wrapper trait.
pub(crate) fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
(self as &mut (dyn Any + Send)).downcast_mut::<T>()
}
}

/// A dynamic payload passed between [`WorkScheduler::schedule_work`] and its
/// associated policy's [`work`](LbPolicy::work) method.
pub(crate) type WorkData = Box<dyn WorkDataTrait>;
pub type WorkData = Box<dyn WorkDataTrait>;

/// Used to asynchronously request a call into the LbPolicy's work method if
/// the LbPolicy needs to provide an update without waiting for an update
/// from the channel first.
pub(crate) trait WorkScheduler: Send + Sync + Debug {
pub trait WorkScheduler: Send + Sync + Debug {
// Schedules a call into the LbPolicy's work method. If there is already a
// pending work call that has not yet started, this may not schedule another
// call.
Expand All @@ -182,7 +182,7 @@ pub(crate) trait WorkScheduler: Send + Sync + Debug {
/// JSON. Hides internal storage details and includes a method to deserialize
/// the JSON into a concrete policy struct.
#[derive(Debug)]
pub(crate) struct ParsedJsonLbConfig {
pub struct ParsedJsonLbConfig {
value: serde_json::Value,
}

Expand All @@ -195,7 +195,7 @@ impl ParsedJsonLbConfig {
}
}

pub(crate) fn from_value(value: serde_json::Value) -> Self {
pub fn from_value(value: serde_json::Value) -> Self {
Comment thread
gu0keno0 marked this conversation as resolved.
Self { value }
}

Expand All @@ -218,7 +218,7 @@ impl ParsedJsonLbConfig {
}

/// Controls channel behaviors.
pub(crate) trait ChannelController: Send + Sync {
pub trait ChannelController: Send + Sync {
/// Creates a new subchannel and returns its current state.
fn new_subchannel(&mut self, address: &Address) -> (Arc<dyn Subchannel>, SubchannelState);

Expand Down Expand Up @@ -252,7 +252,7 @@ pub(crate) trait ChannelController: Send + Sync {
///
/// If the ConnectivityState is TransientFailure, the Picker should return an
/// Err with an error that describes why connections are failing.
pub(crate) trait Picker: Send + Sync + Debug {
pub trait Picker: Send + Sync + Debug {
/// Picks a connection to use for the request.
///
/// This function should not block. If the Picker needs to do blocking or
Expand All @@ -263,7 +263,7 @@ pub(crate) trait Picker: Send + Sync + Debug {
}

#[derive(Debug)]
pub(crate) enum PickResult {
pub enum PickResult {
/// Indicates the Subchannel in the Pick should be used for the request.
Pick(Pick),
/// Indicates the LbPolicy is attempting to connect to a server to use for
Expand Down Expand Up @@ -326,7 +326,7 @@ impl Display for PickResult {

/// State provided by the LB policy to the channel.
#[derive(Clone, Debug)]
pub(crate) struct LbState {
pub struct LbState {
pub connectivity_state: super::ConnectivityState,
pub picker: Arc<dyn Picker>,
}
Expand Down Expand Up @@ -358,10 +358,10 @@ impl LbState {
}

/// Type alias for the completion callback function.
pub(crate) type CompletionCallback = Box<dyn Fn() + Send + Sync>;
pub type CompletionCallback = Box<dyn Fn() + Send + Sync>;

/// A collection of data used by the channel for routing a request.
pub(crate) struct Pick {
pub struct Pick {
/// The Subchannel for the request.
pub subchannel: Arc<dyn Subchannel>,
// Metadata to be added to existing outgoing metadata.
Expand Down
8 changes: 4 additions & 4 deletions grpc/src/client/load_balancing/pick_first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ use crate::metadata::MetadataMap;
use crate::rt::BoxedTaskHandle;
use crate::rt::GrpcRuntime;

pub(crate) static POLICY_NAME: &str = "pick_first";
pub static POLICY_NAME: &str = "pick_first";

type ShufflerFn = dyn Fn(&mut [Endpoint]) + Send + Sync + 'static;

#[derive(Debug, serde::Deserialize, Clone)]
pub(crate) struct PickFirstConfig {
pub struct PickFirstConfig {
#[serde(rename = "shuffleAddressList")]
pub shuffle_address_list: bool,
}

#[derive(Debug)]
struct PickFirstBuilder {}
pub struct PickFirstBuilder {}

impl LbPolicyBuilder for PickFirstBuilder {
type LbPolicy = PickFirstPolicy;
Expand Down Expand Up @@ -101,7 +101,7 @@ pub(crate) fn reg() {
super::GLOBAL_LB_REGISTRY.add_builder(PickFirstBuilder {})
}

pub(crate) struct PickFirstPolicy {
pub struct PickFirstPolicy {
work_scheduler: Arc<dyn WorkScheduler>,
runtime: GrpcRuntime,
connectivity_state: ConnectivityState,
Expand Down
11 changes: 5 additions & 6 deletions grpc/src/client/load_balancing/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::client::name_resolution::ResolverUpdate;

/// A registry to store and retrieve LB policies. LB policies are indexed by
/// their names.
pub(crate) struct LbPolicyRegistry {
pub struct LbPolicyRegistry {
m: Arc<Mutex<HashMap<String, Arc<DynLbPolicyBuilder>>>>,
}

Expand All @@ -54,23 +54,23 @@ impl LbPolicyRegistry {
}

/// Adds a LB policy into the registry.
pub(crate) fn add_builder<B: LbPolicyBuilder>(&self, builder: B) {
pub fn add_builder<B: LbPolicyBuilder>(&self, builder: B) {
self.m
.lock()
.unwrap()
.insert(builder.name().to_string(), DynAdapter::new_arc(builder));
}

/// Adds a dynamic LB policy into the registry.
pub(crate) fn add_dyn_builder(&self, builder: Arc<DynLbPolicyBuilder>) {
pub fn add_dyn_builder(&self, builder: Arc<DynLbPolicyBuilder>) {
self.m
.lock()
.unwrap()
.insert(builder.name().to_string(), builder);
}

/// Retrieves a LB policy from the registry, or None if not found.
pub(crate) fn get_policy(&self, name: &str) -> Option<Arc<DynLbPolicyBuilder>> {
pub fn get_policy(&self, name: &str) -> Option<Arc<DynLbPolicyBuilder>> {
self.m.lock().unwrap().get(name).cloned()
}
}
Expand All @@ -83,8 +83,7 @@ impl Default for LbPolicyRegistry {

/// The registry used if a local registry is not provided to a channel or if it
/// does not exist in the local registry.
pub(crate) static GLOBAL_LB_REGISTRY: LazyLock<LbPolicyRegistry> =
LazyLock::new(LbPolicyRegistry::new);
pub static GLOBAL_LB_REGISTRY: LazyLock<LbPolicyRegistry> = LazyLock::new(LbPolicyRegistry::new);

/// Implements DynLbPolicy and DynLbPolicyBuilder around the enclosed LbPolicy
/// or LbPolicyBuilder, respectively.
Expand Down
6 changes: 3 additions & 3 deletions grpc/src/client/load_balancing/round_robin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ use crate::client::name_resolution::Endpoint;
use crate::client::name_resolution::ResolverUpdate;
use crate::core::RequestHeaders;

pub(crate) static POLICY_NAME: &str = "round_robin";
pub static POLICY_NAME: &str = "round_robin";
static START: Once = Once::new();

#[derive(Debug)]
pub(crate) struct RoundRobinBuilder {}
pub struct RoundRobinBuilder {}

impl LbPolicyBuilder for RoundRobinBuilder {
type LbPolicy = RoundRobinPolicy;
Expand All @@ -78,7 +78,7 @@ impl LbPolicyBuilder for RoundRobinBuilder {
}

#[derive(Debug)]
pub(crate) struct RoundRobinPolicy {
pub struct RoundRobinPolicy {
child_manager: ChildManager<Endpoint>,
pick_first_builder: Arc<DynLbPolicyBuilder>,
}
Expand Down
Loading
Loading