mirror of
https://github.com/zhboner/realm.git
synced 2026-11-21 00:05:56 +00:00
realm_core: add builtin multi-remote support, at the cost of wasting 16b mem per endpoint
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "realm_core"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
authors = ["Realm Contributors"]
|
||||
description = "Realm's core facilities."
|
||||
repository = "https://github.com/zhboner/realm/realm_core"
|
||||
@@ -30,8 +30,7 @@ proxy-protocol = { version = "0.5", optional = true }
|
||||
[features]
|
||||
default = []
|
||||
x = []
|
||||
multi-remote = []
|
||||
hook = ["realm_hook", "multi-remote"]
|
||||
hook = ["realm_hook"]
|
||||
transport = ["kaminari"]
|
||||
transport-boost = []
|
||||
proxy = ["proxy-protocol", "bytes", "tokio/io-util"]
|
||||
|
||||
@@ -52,8 +52,6 @@ pub struct Endpoint {
|
||||
pub laddr: SocketAddr,
|
||||
pub raddr: RemoteAddr,
|
||||
pub conn_opts: ConnectOpts,
|
||||
|
||||
#[cfg(feature = "multi-remote")]
|
||||
pub extra_raddrs: Vec<RemoteAddr>,
|
||||
}
|
||||
|
||||
@@ -71,19 +69,11 @@ impl Display for RemoteAddr {
|
||||
|
||||
impl Display for Endpoint {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
#[cfg(not(feature = "multi-remote"))]
|
||||
write!(f, "{} -> {}; options: {}", &self.laddr, &self.raddr, &self.conn_opts)?;
|
||||
|
||||
#[cfg(feature = "multi-remote")]
|
||||
{
|
||||
write!(f, "{} -> [{}", &self.laddr, &self.raddr)?;
|
||||
for raddr in self.extra_raddrs.iter() {
|
||||
write!(f, "|{}", raddr)?;
|
||||
}
|
||||
write!(f, "]; options: {}", &self.conn_opts)?;
|
||||
write!(f, "{} -> [{}", &self.laddr, &self.raddr)?;
|
||||
for raddr in self.extra_raddrs.iter() {
|
||||
write!(f, "|{}", raddr)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
write!(f, "]; options: {}", &self.conn_opts)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,7 @@ pub async fn connect_and_relay(
|
||||
mut local: TcpStream,
|
||||
raddr: Ref<RemoteAddr>,
|
||||
conn_opts: Ref<ConnectOpts>,
|
||||
|
||||
#[cfg(feature = "multi-remote")] extra_raddrs: Ref<Vec<RemoteAddr>>,
|
||||
extra_raddrs: Ref<Vec<RemoteAddr>>,
|
||||
) -> Result<()> {
|
||||
let ConnectOpts {
|
||||
#[cfg(feature = "proxy-protocol")]
|
||||
|
||||
@@ -26,15 +26,11 @@ pub async fn run_tcp(endpoint: Endpoint) -> Result<()> {
|
||||
laddr,
|
||||
raddr,
|
||||
conn_opts,
|
||||
|
||||
#[cfg(feature = "multi-remote")]
|
||||
extra_raddrs,
|
||||
} = endpoint;
|
||||
|
||||
let raddr = Ref::new(&raddr);
|
||||
let conn_opts = Ref::new(&conn_opts);
|
||||
|
||||
#[cfg(feature = "multi-remote")]
|
||||
let extra_raddrs = Ref::new(&extra_raddrs);
|
||||
|
||||
let lis = socket::bind(&laddr).unwrap_or_else(|e| panic!("[tcp]failed to bind {}: {}", &laddr, e));
|
||||
@@ -52,15 +48,7 @@ pub async fn run_tcp(endpoint: Endpoint) -> Result<()> {
|
||||
let _ = local.set_nodelay(true);
|
||||
|
||||
tokio::spawn(async move {
|
||||
match connect_and_relay(
|
||||
local,
|
||||
raddr,
|
||||
conn_opts,
|
||||
#[cfg(feature = "multi-remote")]
|
||||
extra_raddrs,
|
||||
)
|
||||
.await
|
||||
{
|
||||
match connect_and_relay(local, raddr, conn_opts, extra_raddrs).await {
|
||||
Ok(..) => log::debug!("[tcp]{} => {}, finish", addr, raddr.as_ref()),
|
||||
Err(e) => log::error!("[tcp]{} => {}, error: {}", addr, raddr.as_ref(), e),
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use realm_core::tcp::run_tcp;
|
||||
use realm_core::endpoint::{Endpoint, RemoteAddr, ConnectOpts, ProxyOpts};
|
||||
|
||||
#[tokio::test]
|
||||
#[cfg(feature = "proxy")]
|
||||
async fn proxy_v1() {
|
||||
env_logger::init();
|
||||
|
||||
@@ -26,6 +27,7 @@ async fn proxy_v1() {
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
extra_raddrs: Vec::new(),
|
||||
};
|
||||
|
||||
let endpoint2 = Endpoint {
|
||||
@@ -42,6 +44,7 @@ async fn proxy_v1() {
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
extra_raddrs: Vec::new(),
|
||||
};
|
||||
|
||||
tokio::spawn(run_tcp(endpoint1));
|
||||
|
||||
@@ -9,6 +9,7 @@ use realm_core::tcp::run_tcp;
|
||||
use realm_core::endpoint::{Endpoint, RemoteAddr, ConnectOpts, ProxyOpts};
|
||||
|
||||
#[tokio::test]
|
||||
#[cfg(feature = "proxy")]
|
||||
async fn proxy_v2() {
|
||||
env_logger::init();
|
||||
|
||||
@@ -26,6 +27,7 @@ async fn proxy_v2() {
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
extra_raddrs: Vec::new(),
|
||||
};
|
||||
|
||||
let endpoint2 = Endpoint {
|
||||
@@ -42,6 +44,7 @@ async fn proxy_v2() {
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
extra_raddrs: Vec::new(),
|
||||
};
|
||||
|
||||
tokio::spawn(run_tcp(endpoint1));
|
||||
|
||||
@@ -18,6 +18,7 @@ async fn tcp() {
|
||||
.map(RemoteAddr::SocketAddr)
|
||||
.unwrap(),
|
||||
conn_opts: Default::default(),
|
||||
extra_raddrs: Vec::new(),
|
||||
};
|
||||
|
||||
tokio::spawn(run_tcp(endpoint));
|
||||
|
||||
@@ -17,6 +17,7 @@ async fn udp() {
|
||||
.map(RemoteAddr::SocketAddr)
|
||||
.unwrap(),
|
||||
conn_opts: Default::default(),
|
||||
extra_raddrs: Vec::new(),
|
||||
};
|
||||
|
||||
tokio::spawn(run_udp(endpoint));
|
||||
|
||||
Reference in New Issue
Block a user