Skip to content

Commit 05fe232

Browse files
committed
refactor: use &'static Config instead of Arc
1 parent 5b2e207 commit 05fe232

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

src/logic_ask.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{sync::Arc, str::FromStr};
1+
use std::str::FromStr;
22
use http_body_util::combinators::BoxBody;
33
use http_body_util::{BodyExt, Full};
44
use hyper::body::{Bytes, Incoming};
@@ -18,7 +18,7 @@ use crate::{config::Config, structs::MyStatusCode, msg::{HttpRequest, HttpRespon
1818
/// This function knows from its map which app to direct the message to
1919
pub(crate) async fn handler_http(
2020
mut req: Request<Incoming>,
21-
config: Arc<Config>,
21+
config: &Config,
2222
https_authority: Option<Authority>,
2323
) -> Result<Response, MyStatusCode> {
2424

@@ -96,12 +96,12 @@ pub(crate) async fn handler_http(
9696
info!("{method} {} via {target}", req.uri());
9797
let span = info_span!("request", %method, via = %target, url = %req.uri());
9898
#[cfg(feature = "sockets")]
99-
return crate::sockets::handle_via_sockets(req, &config, target, auth).instrument(span).await;
99+
return crate::sockets::handle_via_sockets(req, config, target, auth).instrument(span).await;
100100
#[cfg(not(feature = "sockets"))]
101101
return handle_via_tasks(req, &config, target, auth).instrument(span).await;
102102
}
103103

104-
async fn handle_via_tasks(req: Request<Incoming>, config: &Arc<Config>, target: &AppId, auth: HeaderValue) -> Result<Response, MyStatusCode> {
104+
async fn handle_via_tasks(req: Request<Incoming>, config: &Config, target: &AppId, auth: HeaderValue) -> Result<Response, MyStatusCode> {
105105
let msg = http_req_to_struct(req, &config.my_app_id, &target, config.expire).await?;
106106

107107
// Send to Proxy

src/main.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{convert::Infallible, sync::Arc, time::Duration};
1+
use std::{convert::Infallible, time::Duration};
22

33
use config::Config;
44
use http_body_util::combinators::BoxBody;
@@ -28,7 +28,7 @@ async fn main() -> anyhow::Result<()> {
2828
tracing::subscriber::set_global_default(tracing_subscriber::fmt().with_env_filter(EnvFilter::builder().with_default_directive(LevelFilter::INFO.into()).from_env_lossy()).finish())?;
2929
banner::print_banner();
3030
let config = Config::load().await?;
31-
let config2 = config.clone();
31+
let config: &'static _ = Box::leak(Box::new(config));
3232
let client = config.client.clone();
3333
let client2 = client.clone();
3434
banner::print_startup_app_config(&config);
@@ -42,7 +42,7 @@ async fn main() -> anyhow::Result<()> {
4242
let mut timer= std::pin::pin!(tokio::time::sleep(Duration::from_secs(60)));
4343
loop {
4444
debug!("Waiting for next request ...");
45-
if let Err(e) = logic_reply::process_requests(config2.clone(), client2.clone()).await {
45+
if let Err(e) = logic_reply::process_requests(config, client2.clone()).await {
4646
match e {
4747
BeamConnectError::ProxyTimeoutError => {
4848
debug!("{e}");
@@ -71,11 +71,9 @@ async fn main() -> anyhow::Result<()> {
7171
#[allow(unused_mut)]
7272
let mut executers = vec![http_executor];
7373
#[cfg(feature = "sockets")]
74-
executers.push(sockets::spawn_socket_task_poller(config.clone()));
74+
executers.push(sockets::spawn_socket_task_poller(config));
7575

76-
let config = Arc::new(config.clone());
77-
78-
if let Err(e) = server(&config).await {
76+
if let Err(e) = server(config).await {
7977
error!("Server error: {}", e);
8078
}
8179
info!("Shutting down...");
@@ -84,7 +82,7 @@ async fn main() -> anyhow::Result<()> {
8482
}
8583

8684
// See https://github.com/hyperium/hyper-util/blob/master/examples/server_graceful.rs
87-
async fn server(config: &Arc<Config>) -> anyhow::Result<()> {
85+
async fn server(config: &'static Config) -> anyhow::Result<()> {
8886
let listener = TcpListener::bind(config.bind_addr.clone()).await?;
8987

9088
let server = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new());
@@ -106,9 +104,7 @@ async fn server(config: &Arc<Config>) -> anyhow::Result<()> {
106104

107105
let stream = hyper_util::rt::TokioIo::new(stream);
108106

109-
let config = config.clone();
110107
let conn = server.serve_connection_with_upgrades(stream, service_fn(move |req| {
111-
let config = config.clone();
112108
handler_http_wrapper(req, config)
113109
}));
114110

@@ -146,7 +142,7 @@ pub type Response<T = BoxBody<Bytes, anyhow::Error>> = hyper::Response<T>;
146142

147143
pub(crate) async fn handler_http_wrapper(
148144
req: Request<Incoming>,
149-
config: Arc<Config>,
145+
config: &'static Config,
150146
) -> Result<Response, Infallible> {
151147
// On https connections we want to emulate that we successfully connected to get the actual http request
152148
if req.method() == Method::CONNECT {
@@ -162,7 +158,6 @@ pub(crate) async fn handler_http_wrapper(
162158
Ok(s) => s,
163159
};
164160
server::conn::auto::Builder::new(TokioExecutor::new()).serve_connection_with_upgrades(TokioIo::new(tls_connection), service_fn(|req| {
165-
let config = config.clone();
166161
let authority = authority.clone();
167162
async move {
168163
match handler_http(req, config, authority).await {

src/sockets.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{time::Duration, collections::HashSet, sync::Arc, convert::Infallible};
1+
use std::{time::Duration, collections::HashSet, convert::Infallible};
22

33
use futures_util::TryStreamExt;
44
use http_body_util::{combinators::BoxBody, BodyExt, BodyStream, StreamBody};
@@ -12,7 +12,7 @@ use reqwest::Response;
1212
use crate::{config::Config, errors::BeamConnectError, structs::MyStatusCode};
1313

1414

15-
pub(crate) fn spawn_socket_task_poller(config: Config) -> JoinHandle<()> {
15+
pub(crate) fn spawn_socket_task_poller(config: &'static Config) -> JoinHandle<()> {
1616
tokio::spawn(async move {
1717
use BeamConnectError::*;
1818
let mut seen: HashSet<MsgId> = HashSet::new();
@@ -41,10 +41,9 @@ pub(crate) fn spawn_socket_task_poller(config: Config) -> JoinHandle<()> {
4141
warn!("Invalid app id skipping");
4242
continue;
4343
};
44-
let config_clone = config.clone();
4544
tokio::spawn(async move {
46-
match connect_proxy(&task.id, &config_clone).await {
47-
Ok(resp) => tunnel(resp, client, &config_clone).await,
45+
match connect_proxy(&task.id, config).await {
46+
Ok(resp) => tunnel(resp, client, config).await,
4847
Err(e) => {
4948
warn!("{e}");
5049
},
@@ -200,7 +199,7 @@ fn tunnel_upgrade(client: Option<OnUpgrade>, server: Option<OnUpgrade>) {
200199
}
201200
}
202201

203-
pub(crate) async fn handle_via_sockets(mut req: Request<Incoming>, config: &Arc<Config>, target: &AppId, auth: HeaderValue) -> Result<crate::Response, MyStatusCode> {
202+
pub(crate) async fn handle_via_sockets(mut req: Request<Incoming>, config: &Config, target: &AppId, auth: HeaderValue) -> Result<crate::Response, MyStatusCode> {
204203
let resp = config.client
205204
.post(format!("{}v1/sockets/{target}", config.proxy_url))
206205
.header(header::AUTHORIZATION, auth)

0 commit comments

Comments
 (0)