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

chore(volo-http): refactor config of context #387

Merged
merged 2 commits into from
Mar 13, 2024
Merged
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: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion volo-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "volo-http"
version = "0.2.0-rc.2"
version = "0.2.0-rc.3"
edition.workspace = true
homepage.workspace = true
repository.workspace = true
Expand Down
7 changes: 3 additions & 4 deletions volo-http/src/client/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::error::Error;
use http::header;
use http_body::Body;
use motore::service::Service;
use volo::context::Context;

use crate::{
context::ClientContext, error::client::ClientError, request::ClientRequest,
Expand Down Expand Up @@ -47,9 +46,9 @@ where
}
}

let stat_enable = cx.rpc_info().config().stat_enable;
let stat_enabled = cx.stat_enabled();

if stat_enable {
if stat_enabled {
if let Some(req_size) = exact_len {
cx.common_stats.set_req_size(req_size);
}
Expand All @@ -60,7 +59,7 @@ where

let res = self.inner.call(cx, req).await;

if stat_enable {
if stat_enabled {
if let Ok(response) = res.as_ref() {
cx.stats.set_status_code(response.status());
if let Some(resp_size) = response.size_hint().exact() {
Expand Down
14 changes: 6 additions & 8 deletions volo-http/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,17 +522,15 @@ impl<S> Client<S> {
}
}

let mut cx = ClientContext::new(target, true);
let mut cx = ClientContext::new(
target,
#[cfg(feature = "__tls")]
uri.scheme()
.is_some_and(|scheme| scheme == &http::uri::Scheme::HTTPS),
);
cx.rpc_info_mut().caller_mut().set_service_name(caller_name);
cx.rpc_info_mut().callee_mut().set_service_name(callee_name);
cx.rpc_info_mut().set_config(self.inner.config.clone());
#[cfg(feature = "__tls")]
{
cx.rpc_info_mut().config_mut().is_tls = match uri.scheme() {
Some(scheme) => scheme == &http::uri::Scheme::HTTPS,
None => false,
};
}

self.call(&mut cx, request).await
}
Expand Down
9 changes: 4 additions & 5 deletions volo-http/src/client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ impl ClientTransport {
#[cfg(feature = "__tls")]
async fn make_connection(&self, cx: &ClientContext) -> Result<Conn, ClientError> {
let target_addr = cx.rpc_info().callee().address().ok_or_else(no_address)?;
let is_tls = cx.rpc_info().config().is_tls;
match target_addr {
volo::net::Address::Ip(_) if is_tls => {
volo::net::Address::Ip(_) if cx.is_tls() => {
let target_name = cx.rpc_info().callee().service_name_ref();
tracing::debug!("connecting to tls target: {target_addr:?}, name: {target_name:?}");
let conn = self
Expand Down Expand Up @@ -140,15 +139,15 @@ where
cx: &mut ClientContext,
req: ClientRequest<B>,
) -> Result<Self::Response, Self::Error> {
let stat_enable = cx.rpc_info().config().stat_enable;
let stat_enabled = cx.stat_enabled();

if stat_enable {
if stat_enabled {
cx.stats.record_transport_start_at();
}

let res = self.request(cx, req).await;

if stat_enable {
if stat_enabled {
cx.stats.record_transport_end_at();
}

Expand Down
33 changes: 24 additions & 9 deletions volo-http/src/context/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@ use crate::utils::macros::impl_deref_and_deref_mut;
pub struct ClientContext(pub(crate) RpcCx<ClientCxInner, Config>);

impl ClientContext {
pub fn new(target: Address, stat_enable: bool) -> Self {
pub fn new(target: Address, #[cfg(feature = "__tls")] tls: bool) -> Self {
let mut cx = RpcCx::new(
RpcInfo::<Config>::with_role(Role::Client),
ClientCxInner {
#[cfg(feature = "__tls")]
tls,
stats: ClientStats::default(),
common_stats: CommonStats::default(),
},
);
cx.rpc_info_mut().callee_mut().set_address(target);
cx.rpc_info_mut().config_mut().stat_enable = stat_enable;
Self(cx)
}

pub fn enable_stat(&mut self, stat: bool) {
self.rpc_info_mut().config_mut().stat_enable = stat;
}

pub(crate) fn stat_enabled(&self) -> bool {
self.rpc_info().config().stat_enable
}
}

newtype_impl_context!(ClientContext, Config, 0);
Expand All @@ -35,12 +44,22 @@ impl_deref_and_deref_mut!(ClientContext, RpcCx<ClientCxInner, Config>, 0);

#[derive(Debug)]
pub struct ClientCxInner {
#[cfg(feature = "__tls")]
tls: bool,

/// This is unstable now and may be changed in the future.
pub stats: ClientStats,
/// This is unstable now and may be changed in the future.
pub common_stats: CommonStats,
}

impl ClientCxInner {
#[cfg(feature = "__tls")]
pub fn is_tls(&self) -> bool {
self.tls
}
}

/// This is unstable now and may be changed in the future.
#[derive(Debug, Default, Clone, Copy)]
pub struct ClientStats {
Expand All @@ -58,11 +77,9 @@ impl ClientStats {

#[derive(Clone, Debug)]
pub struct Config {
pub caller_name: CallerName,
pub callee_name: CalleeName,
pub stat_enable: bool,
#[cfg(feature = "__tls")]
pub is_tls: bool,
pub(crate) caller_name: CallerName,
pub(crate) callee_name: CalleeName,
pub(crate) stat_enable: bool,
}

impl Default for Config {
Expand All @@ -71,8 +88,6 @@ impl Default for Config {
caller_name: CallerName::default(),
callee_name: CalleeName::default(),
stat_enable: true,
#[cfg(feature = "__tls")]
is_tls: false,
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions volo-http/src/context/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
pub struct ServerContext(pub(crate) RpcCx<ServerCxInner, Config>);

impl ServerContext {
pub fn new(peer: Address, stat_enable: bool) -> Self {
pub fn new(peer: Address) -> Self {
let mut cx = RpcCx::new(
RpcInfo::<Config>::with_role(Role::Server),
ServerCxInner {
Expand All @@ -36,9 +36,16 @@ impl ServerContext {
},
);
cx.rpc_info_mut().caller_mut().set_address(peer);
cx.rpc_info_mut().config_mut().stat_enable = stat_enable;
Self(cx)
}

pub fn enable_stat(&mut self, enable: bool) {
self.rpc_info_mut().config_mut().stat_enable = enable;
}

pub(crate) fn stat_enabled(&self) -> bool {
self.rpc_info().config().stat_enable
}
}

impl_deref_and_deref_mut!(ServerContext, RpcCx<ServerCxInner, Config>, 0);
Expand Down Expand Up @@ -78,7 +85,7 @@ impl ServerStats {

#[derive(Debug, Clone, Copy)]
pub struct Config {
pub stat_enable: bool,
pub(crate) stat_enable: bool,
}

impl Default for Config {
Expand Down
14 changes: 10 additions & 4 deletions volo-http/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ use motore::{
use scopeguard::defer;
use tokio::{sync::Notify, task::JoinHandle};
use tracing::{info, trace};
use volo::net::{conn::Conn, incoming::Incoming, Address, MakeIncoming};
#[cfg(feature = "__tls")]
use volo::net::{conn::ConnStream, tls::Acceptor, tls::ServerTlsConfig};
use volo::{
context::Context,
net::{conn::Conn, incoming::Incoming, Address, MakeIncoming},
};

use crate::{
context::{server::Config, ServerContext},
Expand Down Expand Up @@ -469,9 +472,12 @@ where
.scope(RefCell::new(MetaInfo::default()), async {
let service = service.clone();
let peer = peer.clone();
let mut cx = ServerContext::new(peer, config.stat_enable);
let mut cx = ServerContext::new(peer);
cx.rpc_info_mut().set_config(config);

let stat_enabled = cx.stat_enabled();

if config.stat_enable {
if stat_enabled {
cx.stats.set_uri(request.uri().to_owned());
cx.stats.set_method(request.method().to_owned());
if let Some(req_size) = request.size_hint().exact() {
Expand All @@ -482,7 +488,7 @@ where

let resp = service.call(&mut cx, request).await.into_response();

if config.stat_enable {
if stat_enabled {
cx.stats.record_process_end_at();
cx.common_stats.set_status_code(resp.status());
if let Some(resp_size) = resp.size_hint().exact() {
Expand Down
Loading