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

server: add connection span for each connection #922

Merged
merged 1 commit into from
Nov 3, 2022
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
41 changes: 25 additions & 16 deletions server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use tokio::sync::{watch, OwnedSemaphorePermit};
use tokio_util::compat::TokioAsyncReadCompatExt;
use tower::layer::util::Identity;
use tower::{Layer, Service};
use tracing::{instrument, Instrument};

/// Default maximum connections allowed.
const MAX_CONNECTIONS: u32 = 100;
Expand Down Expand Up @@ -183,6 +184,8 @@ where
stop_handle.clone(),
curr_conns,
max_conns,
remote_addr,
id,
)));

id = id.wrapping_add(1);
Expand Down Expand Up @@ -625,22 +628,25 @@ impl<L: Logger> hyper::service::Service<hyper::Request<hyper::Body>> for TowerSe
self.inner.logger.on_connect(self.inner.remote_addr, &request, TransportProtocol::WebSocket);
let data = self.inner.clone();

tokio::spawn(async move {
let upgraded = match hyper::upgrade::on(request).await {
Ok(u) => u,
Err(e) => {
tracing::warn!("Could not upgrade connection: {}", e);
return;
}
};

let stream = BufReader::new(BufWriter::new(upgraded.compat()));
let mut ws_builder = server.into_builder(stream);
ws_builder.set_max_message_size(data.max_request_body_size as usize);
let (sender, receiver) = ws_builder.finish();

let _ = ws::background_task::<L>(sender, receiver, data).await;
});
tokio::spawn(
async move {
let upgraded = match hyper::upgrade::on(request).await {
Ok(u) => u,
Err(e) => {
tracing::warn!("Could not upgrade connection: {}", e);
return;
}
};

let stream = BufReader::new(BufWriter::new(upgraded.compat()));
let mut ws_builder = server.into_builder(stream);
ws_builder.set_max_message_size(data.max_request_body_size as usize);
let (sender, receiver) = ws_builder.finish();

let _ = ws::background_task::<L>(sender, receiver, data).await;
}
.in_current_span(),
);

response.map(|()| hyper::Body::empty())
}
Expand Down Expand Up @@ -723,12 +729,15 @@ where
}

// Attempts to accept a new connection
#[instrument(name = "connection", skip(socket, service, stop_handle, curr_conns, max_conns), level = "INFO")]
async fn try_accept_connection<S, Bd>(
socket: TcpStream,
service: S,
mut stop_handle: StopHandle,
curr_conns: usize,
max_conns: usize,
remote_addr: SocketAddr,
conn_id: u32,
) where
S: Service<hyper::Request<hyper::Body>, Response = hyper::Response<Bd>> + Send + 'static,
S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
Expand Down