perf: eliminate tokio::sync::Mutex across async boundaries in stub and nvim-core
This commit is contained in:
1 parent
573c9586fd
commit
5715625220
5 files changed
+64
-25
No files matched your search
+8
-18
@@ -1,6 +1,5 @@
|
||||
use clap::Parser;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
#[derive(Parser)]
|
||||
@@ -37,7 +36,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let cli = Cli::parse();
|
||||
let rt = tokio::runtime::Runtime::new()?;
|
||||
rt.block_on(async {
|
||||
let (msg_tx, msg_rx) = mpsc::channel::<String>(100);
|
||||
let (msg_tx, msg_rx) = async_channel::bounded::<String>(100);
|
||||
let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1);
|
||||
|
||||
tokio::spawn(async move {
|
||||
@@ -56,7 +55,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
};
|
||||
let ws_url = target_url.replace("http://", "ws://").replace("https://", "wss://");
|
||||
let ws_url = format!("{}/ws?client=proxy", ws_url);
|
||||
let msg_rx = Arc::new(tokio::sync::Mutex::new(msg_rx));
|
||||
|
||||
loop {
|
||||
if shutdown_rx.try_recv().is_ok() {
|
||||
@@ -81,22 +79,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
tracing::info!("Successfully connected to target server");
|
||||
let (mut write, mut read) = ws_stream.split();
|
||||
|
||||
let msg_rx_clone = Arc::clone(&msg_rx);
|
||||
let rx = msg_rx.clone();
|
||||
let mut send_task = tokio::spawn(async move {
|
||||
loop {
|
||||
let mut rx = msg_rx_clone.lock().await;
|
||||
match rx.recv().await {
|
||||
Some(msg) => {
|
||||
drop(rx);
|
||||
let log_prefix = logger::extract_log_prefix(&msg, false);
|
||||
tracing::info!(">>> [Stub] Forwarding {} to server (length: {}): {}", log_prefix, msg.len(), if msg.len() > 1000 { format!("{}...", &msg[..1000]) } else { msg.clone() });
|
||||
if write.send(tokio_tungstenite::tungstenite::Message::Text(msg)).await.is_err() {
|
||||
|
||||
tracing::error!("Failed to write to websocket");
|
||||
break;
|
||||
}
|
||||
},
|
||||
None => break,
|
||||
while let Ok(msg) = rx.recv().await {
|
||||
let log_prefix = logger::extract_log_prefix(&msg, false);
|
||||
tracing::info!(">>> [Stub] Forwarding {} to server (length: {}): {}", log_prefix, msg.len(), if msg.len() > 1000 { format!("{}...", &msg[..1000]) } else { msg.clone() });
|
||||
if write.send(tokio_tungstenite::tungstenite::Message::Text(msg)).await.is_err() {
|
||||
tracing::error!("Failed to write to websocket");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user