mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-12 11:09:38 +00:00
severe isses with concurrency - why is the ws read task starving while the write thread can still loop?
This commit is contained in:
@@ -156,33 +156,35 @@ async fn serve_websocket(websocket_session: WebSocketSession, websocket: HyperWe
|
||||
}
|
||||
println!("!!!! Exit websocket receiver !!!!")
|
||||
});
|
||||
// let websocket_session_write_copy = websocket_session.clone();
|
||||
// tokio::spawn(async move {
|
||||
// println!("Ready to read messages from kafka: {:?}", websocket_session_write_copy);
|
||||
// loop {
|
||||
// println!("Reading messages from kafka.");
|
||||
// let messages = event_reader.read_from_session();
|
||||
// if let Err(_) = messages {
|
||||
// eprintln!("Failed to read messages from kafka for session: {:?}", websocket_session_write_copy);
|
||||
// continue;
|
||||
// }
|
||||
// // println!("Read messages for websocket_session {:?} from consumer: {:?}", websocket_session_write_copy, messages);
|
||||
// let messages = messages.unwrap();
|
||||
// if messages.len() == 0 {
|
||||
// println!("No new messages from kafka.");
|
||||
// continue;
|
||||
// }
|
||||
// println!("{} new messages from kafka.", messages.len());
|
||||
// let json = serde_json::to_string(&messages).unwrap();
|
||||
// let message = Message::from(json);
|
||||
// println!("Sending kafka messages through websocket.");
|
||||
// let send_res = websocket_writer.send(message).await;
|
||||
// if let Err(e) = send_res {
|
||||
// eprintln!("Failed to send message to websocket for session {:?}: {:?}", websocket_session_write_copy, e)
|
||||
// }
|
||||
// sleep(Duration::from_millis(2000));
|
||||
// }
|
||||
// });
|
||||
let websocket_session_write_copy = websocket_session.clone();
|
||||
tokio::spawn(async move {
|
||||
println!("Ready to read messages from kafka: {:?}", websocket_session_write_copy);
|
||||
loop {
|
||||
println!("Reading messages from kafka.");
|
||||
let messages = event_reader.read_from_session();
|
||||
if let Err(_) = messages {
|
||||
eprintln!("Failed to read messages from kafka for session: {:?}", websocket_session_write_copy);
|
||||
continue;
|
||||
}
|
||||
// println!("Read messages for websocket_session {:?} from consumer: {:?}", websocket_session_write_copy, messages);
|
||||
let messages = messages.unwrap();
|
||||
if messages.len() == 0 {
|
||||
println!("No new messages from kafka.");
|
||||
continue;
|
||||
}
|
||||
println!("{} new messages from kafka.", messages.len());
|
||||
let json = serde_json::to_string(&messages).unwrap();
|
||||
let message = Message::from(json);
|
||||
println!("Sending kafka messages through websocket.");
|
||||
let send_res = websocket_writer.send(message).await;
|
||||
if let Err(e) = send_res {
|
||||
eprintln!("Failed to send message to websocket for session {:?}: {:?}", websocket_session_write_copy, e)
|
||||
}
|
||||
// Avoid starvation of read thread (?)
|
||||
// TODO: How to avoid this? This is very bad for performance.
|
||||
sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ const tick = () => {
|
||||
const latestMoveEvents = Object.entries(moveEventsByObj)
|
||||
.map(([_, moveEvents]) => moveEvents[moveEvents.length - 1]);
|
||||
objects = latestMoveEvents.map(({msg}) => msg);
|
||||
// sendEvents(getInputEvents())
|
||||
sendEvents(getInputEvents())
|
||||
}
|
||||
render(objects);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user