Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exponential backoff to socket accept #46

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Line wrap the file at 100 chars. Th


## [Unreleased]
### Changed
- Use cool down period if TCP accept fails. This avoids excessive CPU usage e.g. when there are no
free file descriptors available to be allocated.


## [0.3.0] - 2023-02-28
Expand Down
63 changes: 63 additions & 0 deletions src/exponential_backoff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::cmp;
use std::time::Duration;

/// Simple exponential backoff
pub struct ExponentialBackoff {
start_delay: Duration,
max_delay: Duration,
current_delay: Duration,
}

impl ExponentialBackoff {
/// Creates a new exponential backoff instance starting with delay
/// `start_delay` and maxing out at `max_delay`.
pub fn new(start_delay: Duration, max_delay: Duration) -> Self {
Self {
start_delay,
max_delay,
current_delay: start_delay,
}
}

/// Resets the exponential backoff so that the next delay is the start delay again.
pub fn reset(&mut self) {
self.current_delay = self.start_delay;
}

/// Returns the next delay. This is twice as long as the last returned delay,
/// up until `max_delay` is reached.
pub fn next_delay(&mut self) -> Duration {
let delay = self.current_delay;

let next_delay = self.current_delay * 2;
self.current_delay = cmp::min(next_delay, self.max_delay);

delay
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn correct_delays() {
let mut backoff =
ExponentialBackoff::new(Duration::from_millis(60), Duration::from_millis(300));
assert_eq!(backoff.next_delay(), Duration::from_millis(60));
assert_eq!(backoff.next_delay(), Duration::from_millis(120));
assert_eq!(backoff.next_delay(), Duration::from_millis(240));
assert_eq!(backoff.next_delay(), Duration::from_millis(300));
assert_eq!(backoff.next_delay(), Duration::from_millis(300));
}

#[test]
fn reset() {
let mut backoff =
ExponentialBackoff::new(Duration::from_millis(60), Duration::from_millis(300));
assert_eq!(backoff.next_delay(), Duration::from_millis(60));
backoff.reset();
assert_eq!(backoff.next_delay(), Duration::from_millis(60));
assert_eq!(backoff.next_delay(), Duration::from_millis(120));
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub mod udp2tcp;

pub use udp2tcp::Udp2Tcp;

mod exponential_backoff;
mod forward_traffic;
mod logging;
mod tcp_options;
Expand Down
15 changes: 14 additions & 1 deletion src/tcp2udp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Primitives for listening on TCP and forwarding the data in incoming connections
//! to UDP.

use crate::exponential_backoff::ExponentialBackoff;
use crate::logging::Redact;
use err_context::{BoxedErrorExt as _, ErrorExt as _, ResultExt as _};
use std::convert::Infallible;
Expand All @@ -9,6 +10,7 @@ use std::io;
use std::net::{IpAddr, SocketAddr};
use std::time::Duration;
use tokio::net::{TcpListener, TcpSocket, TcpStream, UdpSocket};
use tokio::time::sleep;

#[derive(Debug)]
#[cfg_attr(feature = "clap", derive(clap::Parser))]
Expand Down Expand Up @@ -149,6 +151,8 @@ async fn process_tcp_listener(
tcp_recv_timeout: Option<Duration>,
tcp_nodelay: bool,
) -> ! {
let mut cooldown =
ExponentialBackoff::new(Duration::from_millis(50), Duration::from_millis(5000));
loop {
match tcp_listener.accept().await {
Ok((tcp_stream, tcp_peer_addr)) => {
Expand All @@ -169,8 +173,17 @@ async fn process_tcp_listener(
log::error!("Error: {}", error.display("\nCaused by: "));
}
});
cooldown.reset();
}
Err(error) => {
log::error!("Error when accepting incoming TCP connection: {}", error);

// If the process runs out of file descriptors, it will fail to accept a socket.
// But that socket will also remain in the queue, so it will fail again immediately.
// This will busy loop consuming the CPU and filling any logs. To prevent this,
// delay between failed socket accept operations.
sleep(cooldown.next_delay()).await;
}
Err(error) => log::error!("Error when accepting incoming TCP connection: {}", error),
}
}
}
Expand Down
Loading