Skip to content

Commit

Permalink
minor improvements to ProxyContext support
Browse files Browse the repository at this point in the history
follow-up of 36f758d
  • Loading branch information
GlenDC committed Jan 12, 2025
1 parent 36f758d commit 967acfd
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 41 deletions.
19 changes: 1 addition & 18 deletions rama-net/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,7 @@ pub struct TransportContext {
pub authority: Authority,
}

/// The context as relevant to the proxy layer.
///
/// This is used to pass proxy related information to the transport layer.
#[derive(Debug, Clone)]
pub struct ProxyContext {
/// The transport protocol used by the proxy.
pub protocol: TransportProtocol,
}

impl From<TransportContext> for ProxyContext {
fn from(ctx: TransportContext) -> Self {
Self {
protocol: ctx.protocol,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// The protocol used for the transport layer.
pub enum TransportProtocol {
/// The `tcp` protocol.
Expand Down
4 changes: 3 additions & 1 deletion rama-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ pub use username::ProxyFilterUsernameParser;
mod proxydb;

#[doc(inline)]
pub use proxydb::{Proxy, ProxyDB, ProxyFilter, ProxyID, ProxyQueryPredicate, StringFilter};
pub use proxydb::{
Proxy, ProxyContext, ProxyDB, ProxyFilter, ProxyID, ProxyQueryPredicate, StringFilter,
};

#[doc(inline)]
pub use proxydb::layer::{ProxyDBLayer, ProxyDBService, ProxyFilterMode, UsernameFormatter};
Expand Down
24 changes: 24 additions & 0 deletions rama-proxy/src/proxydb/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use rama_net::transport::{TransportContext, TransportProtocol};

/// The context as relevant to the proxy layer.
#[derive(Debug, Clone)]
pub struct ProxyContext {
/// The transport protocol used by the proxy.
pub protocol: TransportProtocol,
}

impl From<TransportContext> for ProxyContext {
fn from(ctx: TransportContext) -> Self {
Self {
protocol: ctx.protocol,
}
}
}

impl From<&TransportContext> for ProxyContext {
fn from(ctx: &TransportContext) -> Self {
Self {
protocol: ctx.protocol,
}
}
}
4 changes: 2 additions & 2 deletions rama-proxy/src/proxydb/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl From<std::io::Error> for ProxyCsvRowReaderError {
#[cfg(test)]
mod tests {
use super::*;
use crate::ProxyFilter;
use rama_net::transport::{ProxyContext, TransportProtocol};
use crate::{proxydb::ProxyContext, ProxyFilter};
use rama_net::transport::TransportProtocol;
use rama_utils::str::NonEmptyString;
use std::str::FromStr;

Expand Down
8 changes: 2 additions & 6 deletions rama-proxy/src/proxydb/internal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use super::{ProxyFilter, StringFilter};
use rama_net::{
address::ProxyAddress,
asn::Asn,
transport::{ProxyContext, TransportProtocol},
};
use super::{ProxyContext, ProxyFilter, StringFilter};
use rama_net::{address::ProxyAddress, asn::Asn, transport::TransportProtocol};
use rama_utils::str::NonEmptyString;
use serde::{Deserialize, Serialize};

Expand Down
11 changes: 5 additions & 6 deletions rama-proxy/src/proxydb/layer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::{Proxy, ProxyDB, ProxyFilter, ProxyQueryPredicate};
use super::{Proxy, ProxyContext, ProxyDB, ProxyFilter, ProxyQueryPredicate};
use rama_core::{
error::{BoxError, ErrorContext, ErrorExt, OpaqueError},
Context, Layer, Service,
};
use rama_net::{
address::ProxyAddress,
transport::{ProxyContext, TransportProtocol, TryRefIntoTransportContext},
transport::{TransportProtocol, TryRefIntoTransportContext},
user::{Basic, ProxyCredential},
Protocol,
};
Expand Down Expand Up @@ -212,15 +212,14 @@ where
};

if let Some(filter) = maybe_filter {
let proxy_ctx: ProxyContext = ctx
let proxy_ctx: ProxyContext = (&*ctx
.get_or_try_insert_with_ctx(|ctx| req.try_ref_into_transport_ctx(ctx))
.map_err(|err| {
OpaqueError::from_boxed(err.into())
.context("proxydb: select proxy: get transport context")
})?
.clone()
})?)
.into();
let transport_protocol = proxy_ctx.protocol.clone();
let transport_protocol = proxy_ctx.protocol;

let proxy = self
.db
Expand Down
5 changes: 4 additions & 1 deletion rama-proxy/src/proxydb/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rama_core::error::{BoxError, ErrorContext, OpaqueError};
use rama_net::{asn::Asn, transport::ProxyContext};
use rama_net::asn::Asn;
use rama_utils::str::NonEmptyString;
use serde::{Deserialize, Serialize};
use std::{fmt, future::Future};
Expand All @@ -10,6 +10,9 @@ mod update;
#[doc(inline)]
pub use update::{proxy_db_updater, LiveUpdateProxyDB, LiveUpdateProxyDBSetter};

mod context;
pub use context::ProxyContext;

mod internal;
#[doc(inline)]
pub use internal::Proxy;
Expand Down
11 changes: 4 additions & 7 deletions rama-proxy/src/proxydb/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where

async fn get_proxy_if(
&self,
ctx: rama_net::transport::ProxyContext,
ctx: super::ProxyContext,
filter: super::ProxyFilter,
predicate: impl super::ProxyQueryPredicate,
) -> Result<super::Proxy, Self::Error> {
Expand All @@ -79,7 +79,7 @@ where

async fn get_proxy(
&self,
ctx: rama_net::transport::ProxyContext,
ctx: super::ProxyContext,
filter: super::ProxyFilter,
) -> Result<super::Proxy, Self::Error> {
match self.0.load().deref().deref() {
Expand Down Expand Up @@ -118,11 +118,8 @@ impl<T: fmt::Debug> fmt::Debug for LiveUpdateProxyDBSetter<T> {

#[cfg(test)]
mod tests {
use crate::{Proxy, ProxyFilter};
use rama_net::{
asn::Asn,
transport::{ProxyContext, TransportProtocol},
};
use crate::{proxydb::ProxyContext, Proxy, ProxyFilter};
use rama_net::{asn::Asn, transport::TransportProtocol};
use rama_utils::str::NonEmptyString;

use super::*;
Expand Down

0 comments on commit 967acfd

Please sign in to comment.