Skip to content

Commit

Permalink
WIP: speed up sending ws messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pashinov committed Feb 9, 2024
1 parent e1f50ec commit c662804
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions server/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use anyhow::Result;
use axum::extract::ws::{Message, WebSocket};
use axum::extract::{Query, State, WebSocketUpgrade};
use futures_util::stream::SplitSink;
use futures_util::stream::{FuturesUnordered, SplitSink};
use futures_util::{SinkExt, StreamExt};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -80,10 +80,25 @@ impl WsProducer {
})?;

let message = bincode::serialize(&accounts)?;

let mut clients = self.clients.lock().await;
for (_, client) in clients.iter_mut() {
let message = Message::Binary(message.clone());
client.send(message).await?;
let messages_to_send = FuturesUnordered::new();
for (client_id, client) in clients.iter_mut() {
messages_to_send.push(async {
let message = Message::Binary(message.clone());
let res = client.send(message).await;
(*client_id, res)
});
}

let messages_to_send = messages_to_send
.collect::<Vec<(uuid::Uuid, Result<(), _>)>>()
.await;

for (client_id, result) in messages_to_send {
if let Err(e) = result {
tracing::error!(%client_id, "failed to send message to ws client: {e}");
}
}

Ok(())
Expand Down

0 comments on commit c662804

Please sign in to comment.