♻️ refactor: move Mutex inside RelayState

main
_ 2021-07-17 21:46:17 +00:00
parent 4f3424f482
commit 24d04f2b1f
3 changed files with 18 additions and 10 deletions

7
Cargo.lock generated
View File

@ -319,6 +319,12 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "drop_guard"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78bb5d73478dbcb7c404cd86336e90c67425497ca94b6f7352c8ea7deb9098e2"
[[package]] [[package]]
name = "either" name = "either"
version = "1.6.1" version = "1.6.1"
@ -1351,6 +1357,7 @@ name = "quic_demo"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"drop_guard",
"futures-util", "futures-util",
"quinn", "quinn",
"rcgen", "rcgen",

View File

@ -8,6 +8,7 @@ edition = "2018"
[dependencies] [dependencies]
anyhow = "1.0.38" anyhow = "1.0.38"
drop_guard = "0.2.1"
futures-util = "0.3.9" futures-util = "0.3.9"
quinn = "0.7.2" quinn = "0.7.2"
rcgen = "0.8.11" rcgen = "0.8.11"

View File

@ -9,7 +9,7 @@ async fn main () -> anyhow::Result <()> {
tokio::fs::write ("quic_server.crt", &server_cert).await?; tokio::fs::write ("quic_server.crt", &server_cert).await?;
let relay_state = RelayState::default (); let relay_state = RelayState::default ();
let relay_state = Arc::new (Mutex::new (relay_state)); let relay_state = Arc::new (relay_state);
while let Some (conn) = incoming.next ().await { while let Some (conn) = incoming.next ().await {
let relay_state = Arc::clone (&relay_state); let relay_state = Arc::clone (&relay_state);
@ -25,7 +25,7 @@ async fn main () -> anyhow::Result <()> {
#[derive (Default)] #[derive (Default)]
struct RelayState { struct RelayState {
p4_server_proxies: HashMap <u8, P4State>, p4_server_proxies: Mutex <HashMap <u8, P4State>>,
} }
struct P4State { struct P4State {
@ -104,7 +104,7 @@ impl PtthNewConnection {
} }
async fn handle_quic_connection ( async fn handle_quic_connection (
relay_state: Arc <Mutex <RelayState>>, relay_state: Arc <RelayState>,
conn: quinn::Connecting, conn: quinn::Connecting,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
@ -146,7 +146,7 @@ async fn handle_quic_connection (
} }
async fn handle_p2_connection ( async fn handle_p2_connection (
relay_state: Arc <Mutex <RelayState>>, relay_state: Arc <RelayState>,
client_id: u8, client_id: u8,
conn: quinn::NewConnection, conn: quinn::NewConnection,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
@ -186,7 +186,7 @@ async fn handle_p2_connection (
} }
async fn handle_request_p2_to_p4 ( async fn handle_request_p2_to_p4 (
relay_state: Arc <Mutex <RelayState>>, relay_state: Arc <RelayState>,
client_id: u8, client_id: u8,
server_id: u8, server_id: u8,
mut client_send: quinn::SendStream, mut client_send: quinn::SendStream,
@ -201,8 +201,8 @@ async fn handle_request_p2_to_p4 (
client_send.write_all (&resp_buf).await?; client_send.write_all (&resp_buf).await?;
{ {
let relay_state = relay_state.lock ().await; let p4_server_proxies = relay_state.p4_server_proxies.lock ().await;
match relay_state.p4_server_proxies.get (&server_id) { match p4_server_proxies.get (&server_id) {
Some (p4_state) => { Some (p4_state) => {
p4_state.req_channel.send (RequestP2ToP4 { p4_state.req_channel.send (RequestP2ToP4 {
client_send, client_send,
@ -218,7 +218,7 @@ async fn handle_request_p2_to_p4 (
} }
async fn handle_p4_connection ( async fn handle_p4_connection (
relay_state: Arc <Mutex <RelayState>>, relay_state: Arc <RelayState>,
server_id: u8, server_id: u8,
conn: quinn::NewConnection, conn: quinn::NewConnection,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
@ -234,8 +234,8 @@ async fn handle_p4_connection (
}; };
{ {
let mut relay_state = relay_state.lock ().await; let mut p4_server_proxies = relay_state.p4_server_proxies.lock ().await;
relay_state.p4_server_proxies.insert (server_id, p4_state); p4_server_proxies.insert (server_id, p4_state);
} }
while let Some (req) = rx.recv ().await { while let Some (req) = rx.recv ().await {