Use AtomicUsize and simplify spans

This commit is contained in:
Bad Manners 2024-09-22 12:12:58 -03:00
parent fef441a00a
commit 0714f1932c

View file

@ -1,7 +1,10 @@
use std::{ use std::{
iter, iter,
path::PathBuf, path::PathBuf,
sync::{Arc, LazyLock, Mutex}, sync::{
atomic::{AtomicUsize, Ordering},
Arc, LazyLock,
},
time::Duration, time::Duration,
}; };
@ -127,25 +130,19 @@ async fn main() -> Result<()> {
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
data: Arc<Mutex<usize>>, data: Arc<AtomicUsize>,
}
/// A function that creates our Axum router.
fn router_factory(state: AppState) -> Router {
Router::new().route("/", get(hello)).with_state(state)
} }
/// A lazily-created Router, to be used by the SSH client tunnels. /// A lazily-created Router, to be used by the SSH client tunnels.
static ROUTER: LazyLock<Router> = LazyLock::new(|| { static ROUTER: LazyLock<Router> = LazyLock::new(|| {
router_factory(AppState { Router::new().route("/", get(hello)).with_state(AppState {
data: Arc::new(Mutex::new(0)), data: Arc::new(AtomicUsize::new(1)),
}) })
}); });
/// A basic example endpoint that includes shared state. /// A basic example endpoint that includes shared state.
async fn hello(State(state): State<AppState>) -> String { async fn hello(State(state): State<AppState>) -> String {
let mut request_id = state.data.lock().unwrap(); let request_id = state.data.fetch_add(1, Ordering::AcqRel);
*request_id += 1;
debug!(id = %request_id, "GET /"); debug!(id = %request_id, "GET /");
format!("Hello, request #{}!", request_id) format!("Hello, request #{}!", request_id)
} }
@ -169,8 +166,7 @@ impl TcpForwardSession {
config: Arc<Config>, config: Arc<Config>,
secret_key: Arc<KeyPair>, secret_key: Arc<KeyPair>,
) -> Result<Self> { ) -> Result<Self> {
let span = debug_span!("TcpForwardSession.connect"); let _span = debug_span!("TcpForwardSession.connect");
let _enter = span;
debug!("TcpForwardSession connecting..."); debug!("TcpForwardSession connecting...");
let client = Client {}; let client = Client {};
let mut session = client::connect(Arc::clone(&config), (host, port), client) let mut session = client::connect(Arc::clone(&config), (host, port), client)
@ -200,8 +196,7 @@ impl TcpForwardSession {
remote_port: u16, remote_port: u16,
request_pty: Option<&str>, request_pty: Option<&str>,
) -> Result<u32> { ) -> Result<u32> {
let span = debug_span!("TcpForwardSession.start"); let _span = debug_span!("TcpForwardSession.start");
let _enter = span;
self.session self.session
.tcpip_forward(remote_host, remote_port.into()) .tcpip_forward(remote_host, remote_port.into())
.await .await
@ -363,8 +358,7 @@ impl client::Handler for Client {
originator_port: u32, originator_port: u32,
session: &mut Session, session: &mut Session,
) -> Result<(), Self::Error> { ) -> Result<(), Self::Error> {
let span = debug_span!("server_channel_open_forwarded_tcpip"); let _span = debug_span!("server_channel_open_forwarded_tcpip");
let _enter = span.enter();
debug!( debug!(
sshid = %String::from_utf8_lossy(session.remote_sshid()), sshid = %String::from_utf8_lossy(session.remote_sshid()),
connected_address = connected_address, connected_address = connected_address,