add error tracking to UDP flood counter and implement signal-based run method

This commit is contained in:
2025-09-06 19:07:28 +09:00
parent 264a1552de
commit 8e4b6be991
2 changed files with 22 additions and 1 deletions

View File

@@ -4,12 +4,14 @@ use std::sync::atomic::AtomicU64;
pub struct UdpFloodCounter {
pub sent_bytes: u64,
pub sent_packets: u64,
pub error: u64,
}
#[derive(Default)]
pub struct AtomicUdpFloodCounter {
pub sent_bytes: AtomicU64,
pub sent_packets: AtomicU64,
pub error: AtomicU64,
}
impl AtomicUdpFloodCounter {
@@ -17,6 +19,7 @@ impl AtomicUdpFloodCounter {
UdpFloodCounter {
sent_bytes: self.sent_bytes.load(std::sync::atomic::Ordering::Relaxed),
sent_packets: self.sent_packets.load(std::sync::atomic::Ordering::Relaxed),
error: self.error.load(std::sync::atomic::Ordering::Relaxed),
}
}
}

View File

@@ -4,7 +4,7 @@ use crate::udp::random_flood::UdpSward;
use crate::udp::{BoxedUdpRequest, SizedUdpRequest};
use rand::Rng;
use std::net::SocketAddr;
use std::sync::{atomic, Arc};
use std::sync::{Arc, atomic};
use tokio::net::UdpSocket;
use tower::{Service, ServiceExt};
use wyrand::WyRand;
@@ -55,6 +55,24 @@ impl IntegratedUdpSward {
.call(content)
.await
}
pub async fn run_with_signal<const SIZE: usize>(
mut self,
mut signal: tokio::sync::oneshot::Receiver<()>,
) -> Result<(), tokio::sync::oneshot::error::RecvError> {
loop {
tokio::select! {
receive_result = &mut signal => {
receive_result?;
return Ok(())
},
oneshot_result = self.oneshot_array::<SIZE>() => {
if oneshot_result.is_err() {
self.counter.error.fetch_add(1, atomic::Ordering::Relaxed);
}
}
}
}
}
pub fn get_counter(&self) -> Arc<AtomicUdpFloodCounter> {
self.counter.clone()
}