Skip to content

Commit

Permalink
feat: in-place replacement for std::Mutex usage
Browse files Browse the repository at this point in the history
+ introduce parking_lot for better performence lock primitives
+ introduce DashMap for faster atomic hashmap management
+ adapt API usage to new primitives (no need to unwrap)
  • Loading branch information
kp-omer-shamash committed Jan 14, 2025
1 parent 29fd97b commit b701a31
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 32 deletions.
71 changes: 71 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lightway-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ metrics.workspace = true
more-asserts.workspace = true
num_enum = "0.7.0"
once_cell = "1.19.0"
parking_lot = "0.12"
pnet.workspace = true
rand.workspace = true
rand_core = "0.6.4"
Expand Down
5 changes: 3 additions & 2 deletions lightway-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ mod io_adapter;
mod key_update;

use bytes::{Bytes, BytesMut};
use parking_lot::Mutex;
use rand::Rng;
use std::borrow::Cow;
use std::net::AddrParseError;
use std::num::{NonZeroU16, Wrapping};
use std::{
net::SocketAddr,
sync::{Arc, Mutex},
sync::Arc,
time::{Duration, Instant},
};
use thiserror::Error;
Expand Down Expand Up @@ -936,7 +937,7 @@ impl<AppState: Send> Connection<AppState> {
ref mut pending_session_id,
..
} => {
let new_session_id = rng.lock().unwrap().gen();
let new_session_id = rng.lock().gen();

self.session.io_cb_mut().set_session_id(new_session_id);

Expand Down
2 changes: 1 addition & 1 deletion lightway-core/src/connection/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<'a, AppState: Send + 'static> ServerConnectionBuilder<'a, AppState> {
let auth = ctx.auth.clone();
let ip_pool = ctx.ip_pool.clone();

let session_id = ctx.rng.lock().unwrap().gen();
let session_id = ctx.rng.lock().gen();

let outside_mtu = MAX_OUTSIDE_MTU;

Expand Down
3 changes: 2 additions & 1 deletion lightway-core/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pub mod ip_pool;
mod server_auth;

use parking_lot::Mutex;
use rand::SeedableRng;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use thiserror::Error;

use crate::{
Expand Down
2 changes: 2 additions & 0 deletions lightway-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bytes.workspace = true
bytesize.workspace = true
clap.workspace = true
ctrlc.workspace = true
dashmap = "6.1.0"
delegate.workspace = true
educe.workspace = true
ipnet.workspace = true
Expand All @@ -33,6 +34,7 @@ lightway-app-utils.workspace = true
lightway-core = { workspace = true, features = ["postquantum"] }
metrics.workspace = true
metrics-util = "0.19.0"
parking_lot = "0.12.3"
pnet.workspace = true
ppp = "2.2.0"
pwhash = "1.0.0"
Expand Down
13 changes: 6 additions & 7 deletions lightway-server/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bytes::BytesMut;
use delegate::delegate;
use parking_lot::Mutex;
use std::{
net::{Ipv4Addr, SocketAddr},
sync::{Arc, Mutex, Weak},
sync::{Arc, Weak},
};
use tracing::{trace, warn};

Expand Down Expand Up @@ -80,11 +81,9 @@ impl Connection {

conn.lw_conn
.lock()
.unwrap()
.app_state_mut()
.conn
.set(Arc::downgrade(&conn))
.unwrap();

let mut join_set = tokio::task::JoinSet::new();
ticker_task.spawn(Arc::downgrade(&conn), &mut join_set);
Expand All @@ -93,7 +92,7 @@ impl Connection {
}

delegate! {
to self.lw_conn.lock().unwrap() {
to self.lw_conn.lock() {
pub fn tls_protocol_version(&self) -> ProtocolVersion;
pub fn connection_type(&self) -> ConnectionType;
pub fn session_id(&self) -> SessionId;
Expand Down Expand Up @@ -143,7 +142,7 @@ impl Connection {
}

pub fn begin_session_id_rotation(self: &Arc<Self>) {
let mut conn = self.lw_conn.lock().unwrap();
let mut conn = self.lw_conn.lock();

// A rotation is already in flight, nothing to be done this
// time.
Expand All @@ -169,12 +168,12 @@ impl Connection {
// Use this only during shutdown, after clearing all connections from
// connection_manager
pub fn lw_disconnect(self: Arc<Self>) -> ConnectionResult<()> {
self.lw_conn.lock().unwrap().disconnect()
self.lw_conn.lock().disconnect()
}

pub fn disconnect(&self) -> ConnectionResult<()> {
self.manager.remove_connection(self);
self.lw_conn.lock().unwrap().disconnect()
self.lw_conn.lock().disconnect()
}
}

Expand Down
Loading

0 comments on commit b701a31

Please sign in to comment.