feat: improve udp max throughput

This commit is contained in:
Σrebe - Romain GERARD 2023-10-26 09:01:46 +02:00
parent e2f193cabc
commit 6e72929999
No known key found for this signature in database
GPG key ID: 7A42B4B97E0332F4

View file

@ -16,7 +16,7 @@ use tokio::net::UdpSocket;
use tokio::time::Sleep; use tokio::time::Sleep;
use tracing::{debug, error, info}; use tracing::{debug, error, info};
const DEFAULT_UDP_BUFFER_SIZE: usize = 8 * 1024; const DEFAULT_UDP_BUFFER_SIZE: usize = 64 * 1024; // 64kb
struct UdpServer { struct UdpServer {
listener: Arc<UdpSocket>, listener: Arc<UdpSocket>,
@ -154,7 +154,7 @@ pub async fn run_server(
match server.peers.entry(peer_addr) { match server.peers.entry(peer_addr) {
Entry::Occupied(mut peer) => { Entry::Occupied(mut peer) => {
let ret = peer.get_mut().write_all(&server.buffer[0..nb_bytes]).await; let ret = peer.get_mut().write_all(&server.buffer[..nb_bytes]).await;
if let Err(err) = ret { if let Err(err) = ret {
info!("Peer {:?} disconnected {:?}", peer_addr, err); info!("Peer {:?} disconnected {:?}", peer_addr, err);
peer.remove(); peer.remove();
@ -162,7 +162,7 @@ pub async fn run_server(
} }
Entry::Vacant(peer) => { Entry::Vacant(peer) => {
let (mut rx, tx) = tokio::io::duplex(DEFAULT_UDP_BUFFER_SIZE); let (mut rx, tx) = tokio::io::duplex(DEFAULT_UDP_BUFFER_SIZE);
rx.write_all(&server.buffer[0..nb_bytes]) rx.write_all(&server.buffer[..nb_bytes])
.await .await
.unwrap_or_default(); // should never fail .unwrap_or_default(); // should never fail
peer.insert(rx); peer.insert(rx);