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

Feat/tonic example #23

Merged
merged 7 commits into from
Dec 22, 2024
Merged
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
86 changes: 39 additions & 47 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use std::time::Duration;

use bytes::Bytes;
use futures::stream::{FuturesUnordered, StreamExt};
use futures::stream::StreamExt;
use http::{Request, Response};
use http_body::Body;
use hyper::body::Incoming;
Expand Down Expand Up @@ -366,7 +366,7 @@ where
S::Error: Into<crate::Error>,
ResBody: Body<Data = Bytes> + Send + 'static,
ResBody::Error: Into<crate::Error> + Send,
E: HttpServerConnExec<S::Future, ResBody> + Send + 'static,
E: HttpServerConnExec<S::Future, ResBody> + Send + Sync + 'static,
{
// Initialize shutdown signaling
let (signal_tx, signal_rx) = tokio::sync::watch::channel(());
Expand All @@ -386,7 +386,6 @@ where

// Pin the incoming stream to the stack
let mut incoming = pin!(incoming);
let mut active_connections = FuturesUnordered::new();

// Enter the main server loop
loop {
Expand All @@ -400,51 +399,50 @@ where
break;
},
Some(io_result) = incoming.next() => {
let io = match io_result {
Ok(io) => io,
Err(e) => {
trace!("error accepting connection: {:#}", e);
continue;
}
};

trace!("TCP streaming connection accepted");

let connection_service = service.clone();
let connection_builder = builder.clone();
let connection_signal_rx = graceful.then_some(signal_rx.clone());
let connection_tls_acceptor = tls_acceptor.clone();

let transport = if let Some(tls_acceptor) = &tls_acceptor {
match accept_tls_connection(io, Arc::clone(tls_acceptor)).await {
Ok(tls_stream) => Transport::new_tls(tls_stream),
Err(e) => {
// This connection failed to handshake
debug!("TLS handshake failed: {:#}", e);
continue;
}
tokio::spawn(async move {
let io = match io_result {
Ok(io) => io,
Err(e) => {
trace!("error accepting connection: {:#}", e);
return;
}
};

trace!("TCP streaming connection accepted");

let transport = if let Some(connection_tls_acceptor) = &connection_tls_acceptor {
match accept_tls_connection(io, Arc::clone(connection_tls_acceptor)).await {
Ok(tls_stream) => Transport::new_tls(tls_stream),
Err(e) => {
// This connection failed to handshake
debug!("TLS handshake failed: {:#}", e);
return;
}
}
} else {
Transport::new_plain(io)
};

// Convert our abstracted tokio transport into a hyper transport
let hyper_io = TokioIo::new(transport);


// Create future for serving the connection
serve_http_connection(
hyper_io,
connection_service,
connection_builder,
connection_signal_rx,
None
).await;
}
} else {
Transport::new_plain(io)
};

// Convert our abstracted tokio transport into a hyper transport
let hyper_io = TokioIo::new(transport);


// Create future for serving the connection
let conn_future = serve_http_connection(
hyper_io,
connection_service,
connection_builder,
connection_signal_rx,
None
);

active_connections.push(conn_future);
},
Some(_) = active_connections.next(), if !active_connections.is_empty() => {
trace!("Connection completed, {} active", active_connections.len());
}
}
}

Expand All @@ -458,12 +456,6 @@ where
"waiting for {} connections to close",
signal_tx.receiver_count()
);

while !active_connections.is_empty() {
if active_connections.next().await.is_some() {
trace!("Connection closed during shutdown");
}
}
signal_tx.closed().await;
}

Expand Down
Loading