Remove http server from globals, create one each time (cheap)

This commit is contained in:
Mike Dilger 2024-06-22 12:31:14 +12:00
parent 9759fe6e28
commit fde63df92b
3 changed files with 30 additions and 39 deletions

View File

@ -124,18 +124,8 @@ async fn main() -> Result<(), Error> {
}, },
None => Box::new(counting_stream) None => Box::new(counting_stream)
}; };
if let Err(e) = chorus::serve(stream, hashed_peer).await { chorus::serve(stream, hashed_peer).await;
log::error!(
target: "Client",
"{}: {}", hashed_peer, e
);
}
}); });
//Err(e) => log::error!(
//target: "Client",
//"{}: {}", hashed_peer, e
//),
} }
}; };
} }

View File

@ -1,7 +1,6 @@
use crate::config::Config; use crate::config::Config;
use crate::ip::HashedIp; use crate::ip::HashedIp;
use dashmap::DashMap; use dashmap::DashMap;
use hyper::server::conn::Http;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use parking_lot::RwLock; use parking_lot::RwLock;
use pocket_db::Store; use pocket_db::Store;
@ -17,7 +16,6 @@ pub struct Globals {
pub bytes_outbound: AtomicU64, pub bytes_outbound: AtomicU64,
pub config: RwLock<Config>, pub config: RwLock<Config>,
pub store: OnceLock<Store>, pub store: OnceLock<Store>,
pub http_server: Http,
pub rid: OnceLock<String>, pub rid: OnceLock<String>,
/// This is a broadcast channel where new incoming events are advertised by their offset. /// This is a broadcast channel where new incoming events are advertised by their offset.
@ -33,10 +31,6 @@ pub struct Globals {
lazy_static! { lazy_static! {
pub static ref GLOBALS: Globals = { pub static ref GLOBALS: Globals = {
let mut http_server = hyper::server::conn::Http::new();
http_server.http1_only(true);
http_server.http1_keep_alive(true);
let (new_events, _) = tokio::sync::broadcast::channel(512); let (new_events, _) = tokio::sync::broadcast::channel(512);
let (shutting_down, _) = tokio::sync::watch::channel(false); let (shutting_down, _) = tokio::sync::watch::channel(false);
@ -46,7 +40,6 @@ lazy_static! {
bytes_outbound: AtomicU64::new(0), bytes_outbound: AtomicU64::new(0),
config: RwLock::new(Default::default()), config: RwLock::new(Default::default()),
store: OnceLock::new(), store: OnceLock::new(),
http_server,
rid: OnceLock::new(), rid: OnceLock::new(),
new_events, new_events,
num_connections: AtomicUsize::new(0), num_connections: AtomicUsize::new(0),

View File

@ -48,16 +48,27 @@ impl FullStream for CountingStream<TcpStream> {}
impl FullStream for TlsStream<CountingStream<TcpStream>> {} impl FullStream for TlsStream<CountingStream<TcpStream>> {}
/// Serve a single network connection /// Serve a single network connection
pub async fn serve(stream: Box<dyn FullStream>, peer: HashedPeer) -> Result<(), Error> { pub async fn serve(stream: Box<dyn FullStream>, peer: HashedPeer) {
// Serve the network stream with our http server and our HttpService // Serve the network stream with our http server and our HttpService
let service = HttpService { peer }; let service = HttpService { peer };
let connection = GLOBALS let mut http_server = hyper::server::conn::Http::new();
.http_server http_server.http1_only(true);
http_server.http1_keep_alive(true);
let connection = http_server
.serve_connection(stream, service) .serve_connection(stream, service)
.with_upgrades(); .with_upgrades();
tokio::spawn(async move { /* hyper 1
let mut http1builder = http1::Builder::new();
http1builder.half_close(true);
http1builder.keep_alive(true);
http1builder.header_read_timeout(Duration::from_secs(5));
let connection = http1builder
.serve_connection(stream, service)
.with_upgrades();
*/
// If our service exits with an error, log the error // If our service exits with an error, log the error
if let Err(he) = connection.await { if let Err(he) = connection.await {
if let Some(src) = he.source() { if let Some(src) = he.source() {
@ -73,9 +84,6 @@ pub async fn serve(stream: Box<dyn FullStream>, peer: HashedPeer) -> Result<(),
log::error!(target: "Client", "{}: {}", peer, e); log::error!(target: "Client", "{}: {}", peer, e);
} }
} }
});
Ok(())
} }
// This is our per-connection HTTP service // This is our per-connection HTTP service