Skip to content

Commit bd79479

Browse files
committed
Debugging
1 parent 2245b23 commit bd79479

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

src/udp/impls/response_batch_manager.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ impl ResponseBatchManager {
1616
let (sender, mut receiver) = mpsc::unbounded_channel::<QueuedResponse>();
1717

1818
tokio::spawn(async move {
19-
let mut buffer = VecDeque::with_capacity(1000); // Larger buffer for higher throughput
20-
let mut timer = interval(Duration::from_millis(5)); // 5ms flush interval for better responsiveness
21-
let mut stats_timer = interval(Duration::from_secs(10)); // Stats every 10 seconds
19+
let mut buffer = VecDeque::with_capacity(1000);
20+
let mut timer = interval(Duration::from_millis(1)); // 1ms flush - much more aggressive
21+
let mut stats_timer = interval(Duration::from_secs(10));
2222
let mut total_queued = 0u64;
2323

2424
loop {
@@ -28,17 +28,15 @@ impl ResponseBatchManager {
2828
total_queued += 1;
2929
buffer.push_back(response);
3030

31-
// If buffer reaches 500 responses, send immediately
32-
if buffer.len() >= 500 {
33-
debug!("Buffer full ({} items) - flushing immediately", buffer.len());
31+
// Much smaller batch size for lower latency
32+
if buffer.len() >= 50 {
3433
Self::flush_buffer(&socket, &mut buffer).await;
3534
}
3635
}
3736

38-
// Timer tick - flush any remaining responses
37+
// Very frequent timer tick - prioritize low latency over efficiency
3938
_ = timer.tick() => {
4039
if !buffer.is_empty() {
41-
debug!("Timer flush - {} items in buffer", buffer.len());
4240
Self::flush_buffer(&socket, &mut buffer).await;
4341
}
4442
}

src/udp/impls/udp_server.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,6 @@ impl UdpServer {
5353
socket.set_send_buffer_size(actual_send_buffer).map_err(tokio::io::Error::other)?;
5454
socket.set_reuse_address(reuse_address).map_err(tokio::io::Error::other)?;
5555

56-
// Enable SO_REUSEPORT for better load distribution across threads
57-
#[cfg(target_os = "linux")]
58-
{
59-
let reuse_port = 1i32;
60-
unsafe {
61-
let optval = &reuse_port as *const i32 as *const libc::c_void;
62-
if libc::setsockopt(
63-
socket.as_raw_fd(),
64-
libc::SOL_SOCKET,
65-
libc::SO_REUSEPORT,
66-
optval,
67-
std::mem::size_of::<i32>() as libc::socklen_t,
68-
) != 0 {
69-
log::warn!("Failed to set SO_REUSEPORT - continuing without it");
70-
}
71-
}
72-
}
73-
7456
socket.bind(&bind_address.into()).map_err(tokio::io::Error::other)?;
7557
socket.set_nonblocking(true).map_err(tokio::io::Error::other)?;
7658

@@ -174,9 +156,18 @@ impl UdpServer {
174156
let position = cursor.position() as usize;
175157
debug!("Response bytes: {:?}", &buffer[..position]);
176158

177-
// Get batch manager for this socket and queue the response
178-
let batch_manager = ResponseBatchManager::get_for_socket(socket).await;
179-
batch_manager.queue_response(remote_addr, buffer[..position].to_vec());
159+
// For immediate response (bypassing batching for better latency)
160+
match socket.send_to(&buffer[..position], &remote_addr).await {
161+
Ok(bytes_sent) => {
162+
debug!("Direct send successful: {} bytes to {}", bytes_sent, remote_addr);
163+
}
164+
Err(e) => {
165+
// If direct send fails, fall back to batched approach
166+
log::warn!("Direct send failed ({}), using batch queue", e);
167+
let batch_manager = ResponseBatchManager::get_for_socket(socket).await;
168+
batch_manager.queue_response(remote_addr, buffer[..position].to_vec());
169+
}
170+
}
180171
}
181172
Err(error) => {
182173
sentry::capture_error(&error);

0 commit comments

Comments
 (0)