You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::thread::{self, sleep};externcrate ws;fnmain(){// A client that sends tons of messages to the server
thread::spawn(move || {let _ = ws::connect("ws://127.0.0.1:3012", |sender| {letmut num_send = 0_usize;// Generate a thread that constantly sends messages for testing
thread::spawn(move || loop{
num_send += 1;// The content is just for example, the actual situation has more varietylet _ = sender.send(format!("overwhelming message #{num_send}!"));});// Handle nothingmove |_| Ok(())});});// A server that echoes messages back to the client
ws::listen("127.0.0.1:3012", |sender| {// Handle received messagemove |msg| {println!("Got message: {}", msg);// ! It will block on ↓this line when the `SyncSender` is fulllet _ = sender.send(msg);// * ↑If uncomment this line of code, the server will not be blockedOk(())}}).unwrap();}
The output seems to show that the server always blocks after resending 500 messages:
Once the terminal stops the output, the whole receiving process will be completely blocked, and there is almost no possibility of recovery.
It's confusing why do the library use the blocking SyncSender instead of Sender when interfacing with mio inside it?
Or, is any solution of the limited ws::Sender such as WebSocket::set_non_blocking for sending messages?
The text was updated successfully, but these errors were encountered:
After reading ws::Setting, I have improved the MWE to this one:
use std::thread::{self, sleep};externcrate ws;fnmain(){// A client that sends tons of messages to the server
thread::spawn(move || {let _ = ws::connect("ws://127.0.0.1:3012", |sender| {letmut num_send = 0_usize;// Generate a thread that constantly sends messages for testing
thread::spawn(move || loop{
num_send += 1;// The content is just for example, the actual situation has more varietylet _ = sender.send(format!("overwhelming message #{num_send}!"));});// Handle nothingmove |_| Ok(())});});// A server that echoes messages back to the client
ws::Builder::new().with_settings(ws::Settings{max_connections:0x40,// * ↓Changing this setting to `usize::MAX` actually can't be allowed: It might run out of memoryqueue_size:0x300,// ! ↓Even if it's enabled, it still can't stop the blockingpanic_on_queue:true,
..Default::default()}).build(|sender: ws::Sender| {// handle received messagemove |msg| {println!("Got message: {}", msg);println!("from {sender:?}");// ! It will block on ↓this line when the `SyncSender` is fulllet _ = sender.send(msg);// * ↑If uncomment this line of code, the server will not be blockedOk(())}}).unwrap().listen("127.0.0.1:3012").unwrap();}
But the confusing problem remains: Why not choose mio::channel::Sender as the inner sender of ws::Sender, or provide some options to run a server that will never blocks on sending messages?
A MWE of it:
The output seems to show that the server always blocks after resending 500 messages:
Once the terminal stops the output, the whole receiving process will be completely blocked, and there is almost no possibility of recovery.
It's confusing why do the library use the blocking
SyncSender
instead ofSender
when interfacing withmio
inside it?Or, is any solution of the limited
ws::Sender
such asWebSocket::set_non_blocking
for sending messages?The text was updated successfully, but these errors were encountered: