Skip to content

Commit

Permalink
Move blocking code to blocking task.
Browse files Browse the repository at this point in the history
  • Loading branch information
vldr committed Aug 7, 2023
1 parent cc7f672 commit e84a180
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ impl PacketSender for Sender<Message> {
fn send_packet(&self, packet: ResponsePacket) -> Result<(), SendError<Message>> {
let serialized_packet = serde_json::to_string(&packet).unwrap();

self.send(Message::Text(serialized_packet))
let cloned_sender = self.clone();
tokio::task::spawn_blocking(move || cloned_sender.send(Message::Text(serialized_packet)));

return Ok(());
}

fn send_error_packet(&self, message: String) -> Result<(), SendError<Message>> {
Expand Down Expand Up @@ -149,7 +152,7 @@ impl Server {
if let Ok(websocket_stream) =
tokio_tungstenite::accept_hdr_async(tcp_stream, callback).await
{
let (sender, receiver) = flume::unbounded();
let (sender, receiver) = flume::bounded(0);
let (outgoing, incoming) = websocket_stream.split();

let mut client = Client::new(sender.clone());
Expand Down Expand Up @@ -331,7 +334,9 @@ impl Client {
data[0] = source;

if destination < room.senders.len() {
return room.senders[destination].send(Message::Binary(data));
let cloned_sender = room.senders[destination].clone();

tokio::task::spawn_blocking(move || cloned_sender.send(Message::Binary(data)));
} else if destination == usize::from(u8::MAX) {
for sender in &room.senders {
if sender.same_channel(&self.sender) {
Expand Down

0 comments on commit e84a180

Please sign in to comment.