use std::{
collections::BTreeMap,
fmt,
io::{self, Read, Write},
net::{Shutdown, SocketAddr, TcpListener, TcpStream},
sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError},
thread::{self, JoinHandle},
time::{Duration, Instant},
};
use crate::{
config::Config,
endpoint::{End, Endpoint, Next},
error::Error,
framing::Framing,
limits::Limits,
queue::{QueueLimits, Queued},
router::Router,
trace::event,
};
const WAKE_TIMEOUT: Duration = Duration::from_secs(1);
#[cfg(windows)]
const TICK: Duration = Duration::from_millis(250);
const IDLE_POLL: Duration = Duration::from_secs(1);
const BACKOFF: (Duration, Duration) = (Duration::from_millis(10), Duration::from_secs(1));
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Connection {
pub id: u64,
pub addr: String,
}
pub struct Server {
framing: Framing,
config: Config,
queue: QueueLimits,
max_connections: usize,
deadlines: Deadlines,
shared: Arc<Shared>,
}
#[derive(Clone, Copy, Debug)]
struct Deadlines {
message: Option<Duration>,
idle: Option<Duration>,
}
#[derive(Default)]
struct Shared {
state: Mutex<State>,
changed: Condvar,
}
#[derive(Default)]
struct State {
stopped: bool,
active: usize,
wake: Option<Box<dyn Fn() + Send>>,
open: BTreeMap<u64, Box<dyn Fn() + Send>>,
}
impl Shared {
fn lock(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
fn stop(&self) {
let (wake, open) = {
let mut state = self.lock();
state.stopped = true;
(state.wake.take(), std::mem::take(&mut state.open))
};
self.changed.notify_all();
for shut in open.values() {
shut();
}
if let Some(wake) = wake {
wake();
}
}
fn ended(&self, id: u64) {
{
let mut state = self.lock();
state.active = state.active.saturating_sub(1);
let _shut = state.open.remove(&id);
}
self.changed.notify_all();
}
}
#[derive(Clone)]
pub struct Stopper(Arc<Shared>);
impl Stopper {
pub fn stop(&self) {
self.0.stop();
}
#[must_use]
pub fn is_stopped(&self) -> bool {
self.0.lock().stopped
}
}
impl fmt::Debug for Stopper {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Stopper")
.field("stopped", &self.is_stopped())
.finish()
}
}
trait Stream: Read + Write + Send + Sync + Sized + 'static {
fn try_clone(&self) -> io::Result<Self>;
fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()>;
fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()>;
fn shut(&self);
fn wait_readable(
&self,
deadline: Option<Instant>,
_stopped: &dyn Fn() -> bool,
) -> io::Result<bool> {
let left = match deadline {
None => None,
Some(at) => match at.checked_duration_since(Instant::now()) {
Some(left) if !left.is_zero() => Some(left),
_ => return Ok(false),
},
};
self.set_read_timeout(left)?;
Ok(true)
}
}
impl Stream for TcpStream {
fn try_clone(&self) -> io::Result<Self> {
TcpStream::try_clone(self)
}
fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
TcpStream::set_read_timeout(self, timeout)
}
fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
TcpStream::set_write_timeout(self, timeout.filter(|t| !t.is_zero()))
}
fn shut(&self) {
let _closed = self.shutdown(Shutdown::Both);
}
#[cfg(windows)]
fn wait_readable(
&self,
deadline: Option<Instant>,
stopped: &dyn Fn() -> bool,
) -> io::Result<bool> {
let mut byte = [0u8; 1];
loop {
if stopped() {
return Ok(false);
}
let tick = match deadline {
None => TICK,
Some(at) => match at.checked_duration_since(Instant::now()) {
Some(left) if !left.is_zero() => left.min(TICK),
_ => return Ok(false),
},
};
self.set_read_timeout(Some(tick))?;
match self.peek(&mut byte) {
Ok(_) => return Ok(true),
Err(error) if is_timeout(&error) || error.kind() == io::ErrorKind::Interrupted => {}
Err(error) => return Err(error),
}
}
}
}
#[cfg(unix)]
impl Stream for std::os::unix::net::UnixStream {
fn try_clone(&self) -> io::Result<Self> {
std::os::unix::net::UnixStream::try_clone(self)
}
fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
std::os::unix::net::UnixStream::set_read_timeout(self, timeout)
}
fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
std::os::unix::net::UnixStream::set_write_timeout(self, timeout.filter(|t| !t.is_zero()))
}
fn shut(&self) {
let _closed = self.shutdown(Shutdown::Both);
}
}
fn is_timeout(error: &io::Error) -> bool {
matches!(
error.kind(),
io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut
)
}
struct Writer<T> {
stream: T,
closer: Arc<T>,
}
impl<T: Stream> Write for Writer<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.stream.write(buf).inspect_err(|error| {
if error.kind() != io::ErrorKind::Interrupted {
self.closer.shut();
}
})
}
fn flush(&mut self) -> io::Result<()> {
self.stream.flush().inspect_err(|_| self.closer.shut())
}
}
#[allow(dead_code)]
#[derive(Debug)]
enum Ending {
Done(End),
Late,
Stopped,
Failed(Error),
}
fn serve_connection<T: Stream, S>(
stream: &mut T,
endpoint: &mut Endpoint<S, Queued>,
framing: Framing,
deadlines: Deadlines,
shared: &Shared,
) -> Ending {
let stopped = || shared.lock().stopped;
let handled = |endpoint: &Endpoint<S, Queued>| {
let stats = endpoint.stats();
stats.bodies_in.wrapping_add(stats.invalid_in)
};
let mut busy_since = Some(Instant::now());
let mut last_read = Instant::now();
let mut last_active = last_read;
let mut sent = 0;
let mut done = 0;
loop {
match endpoint.next_read(framing) {
Ok(Next::Read(_)) => {}
Ok(Next::Stopped) => return Ending::Done(End::Stopped),
Err(error) => return Ending::Failed(error),
}
let finished = handled(endpoint);
if finished != done {
done = finished;
busy_since = endpoint.mid_message().then_some(last_read);
} else if endpoint.mid_message() {
busy_since = busy_since.or(Some(last_read));
}
let stats = endpoint.stats();
let open_ended = endpoint.open_ended();
let working = stats.pending > open_ended || (stats.in_flight > 0 && open_ended == 0);
let now = Instant::now();
if working || stats.bodies_out != sent {
sent = stats.bodies_out;
last_active = now;
}
last_active = last_active.max(last_read);
let deadline = match busy_since {
Some(since) => deadlines.message.and_then(|t| since.checked_add(t)),
None if working => deadlines
.idle
.and_then(|t| now.checked_add(t.min(IDLE_POLL))),
None => deadlines.idle.and_then(|t| last_active.checked_add(t)),
};
if stopped() {
return Ending::Stopped;
}
if deadline.is_some_and(|at| at <= now) {
return Ending::Late;
}
match stream.wait_readable(deadline, &stopped) {
Ok(true) => {}
Ok(false) if stopped() => return Ending::Stopped,
Ok(false) => continue,
Err(error) => return Ending::Failed(Error::Io(error)),
}
let Ok(Next::Read(buf)) = endpoint.next_read(framing) else {
continue;
};
match stream.read(buf) {
Ok(0) => {
return match endpoint.finish() {
Ok(end) => Ending::Done(end),
Err(error) => Ending::Failed(error),
};
}
Ok(n) => {
endpoint.commit(n);
last_read = Instant::now();
}
Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
Err(error) if is_timeout(&error) => {
if stopped() {
return Ending::Stopped;
}
if cfg!(windows) {
return Ending::Late;
}
}
Err(error) => return Ending::Failed(Error::Io(error)),
}
}
}
impl Server {
#[must_use]
pub fn new(framing: Framing) -> Self {
Self {
framing,
config: Config::new().with_limits(Limits::network()),
queue: QueueLimits::new()
.with_max_bytes(8 * 1024 * 1024)
.with_max_messages(4096),
max_connections: 256,
deadlines: Deadlines {
message: Some(Duration::from_secs(30)),
idle: Some(Duration::from_secs(300)),
},
shared: Arc::default(),
}
}
#[must_use]
pub fn with_message_timeout(mut self, timeout: Option<Duration>) -> Self {
self.deadlines.message = timeout;
self
}
#[must_use]
pub fn with_idle_timeout(mut self, timeout: Option<Duration>) -> Self {
self.deadlines.idle = timeout;
self
}
#[must_use]
pub fn message_timeout(&self) -> Option<Duration> {
self.deadlines.message
}
#[must_use]
pub fn idle_timeout(&self) -> Option<Duration> {
self.deadlines.idle
}
#[must_use]
pub fn with_config(mut self, config: Config) -> Self {
self.config = config;
self
}
#[must_use]
pub fn with_queue(mut self, queue: QueueLimits) -> Self {
self.queue = queue;
self
}
#[must_use]
pub fn with_max_connections(mut self, count: usize) -> Self {
self.max_connections = count.max(1);
self
}
#[must_use]
pub fn config(&self) -> &Config {
&self.config
}
#[must_use]
pub fn queue(&self) -> QueueLimits {
self.queue
}
#[must_use]
pub fn max_connections(&self) -> usize {
self.max_connections
}
#[must_use]
pub fn connections(&self) -> usize {
self.shared.lock().active
}
#[must_use]
pub fn stopper(&self) -> Stopper {
Stopper(Arc::clone(&self.shared))
}
pub fn serve_tcp<S, F>(&self, listener: TcpListener, connect: F) -> io::Result<()>
where
S: Send + 'static,
F: FnMut(&Connection) -> (Router<S>, S),
{
let local = listener.local_addr()?;
let wake = move || {
let _woken = TcpStream::connect_timeout(&reachable(local), WAKE_TIMEOUT);
};
self.serve(
|| {
listener
.accept()
.map(|(stream, addr)| (stream, addr.to_string()))
},
Box::new(wake),
connect,
)
}
#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
pub fn serve_unix<S, F>(
&self,
listener: std::os::unix::net::UnixListener,
connect: F,
) -> io::Result<()>
where
S: Send + 'static,
F: FnMut(&Connection) -> (Router<S>, S),
{
use std::os::unix::net::UnixStream;
let path = listener
.local_addr()?
.as_pathname()
.map(std::path::Path::to_path_buf)
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"the socket has no path to wake it with",
)
})?;
let wake = move || {
let _woken = UnixStream::connect(&path);
};
self.serve(
|| {
listener.accept().map(|(stream, addr)| {
let addr = addr
.as_pathname()
.map(|p| p.display().to_string())
.unwrap_or_default();
(stream, addr)
})
},
Box::new(wake),
connect,
)
}
fn serve<T, S, A, F>(
&self,
mut accept: A,
wake: Box<dyn Fn() + Send>,
mut connect: F,
) -> io::Result<()>
where
T: Stream,
S: Send + 'static,
A: FnMut() -> io::Result<(T, String)>,
F: FnMut(&Connection) -> (Router<S>, S),
{
if !self.arm(wake) {
return Ok(());
}
let mut threads: Vec<JoinHandle<()>> = Vec::new();
let mut next = 0u64;
let mut backoff = BACKOFF.0;
let result = loop {
if !self.wait_for_room() {
break Ok(());
}
let (stream, addr) = match accept() {
Ok(accepted) => {
backoff = BACKOFF.0;
accepted
}
Err(error) if transient(&error) => continue,
Err(_error) if exhausted(&_error) => {
event!(warn, error = %_error, pause = ?backoff, "accept is out of resources; pausing");
if !self.pause(backoff) {
break Ok(());
}
backoff = (backoff * 2).min(BACKOFF.1);
continue;
}
Err(error) => break Err(error),
};
if self.shared.lock().stopped {
break Ok(());
}
let connection = Connection { id: next, addr };
next = next.wrapping_add(1);
let (router, state) = connect(&connection);
match self.spawn(stream, &connection, router, state) {
Ok(Some(thread)) => threads.push(thread),
Ok(None) => break Ok(()),
Err(_error) => {
event!(warn, error = %_error, "a connection could not be served");
}
}
threads.retain(|thread| !thread.is_finished());
};
self.shared.stop();
for thread in threads {
let _joined = thread.join();
}
result
}
fn arm(&self, wake: Box<dyn Fn() + Send>) -> bool {
let mut state = self.shared.lock();
if state.stopped {
return false;
}
state.wake = Some(wake);
true
}
fn pause(&self, pause: Duration) -> bool {
let deadline = Instant::now() + pause;
let mut state = self.shared.lock();
while !state.stopped {
let Some(left) = deadline.checked_duration_since(Instant::now()) else {
break;
};
state = self
.shared
.changed
.wait_timeout(state, left)
.unwrap_or_else(PoisonError::into_inner)
.0;
}
!state.stopped
}
fn wait_for_room(&self) -> bool {
let mut state = self.shared.lock();
while !state.stopped && state.active >= self.max_connections {
state = self
.shared
.changed
.wait(state)
.unwrap_or_else(PoisonError::into_inner);
}
!state.stopped
}
fn spawn<T: Stream, S: Send + 'static>(
&self,
stream: T,
connection: &Connection,
router: Router<S>,
state: S,
) -> io::Result<Option<JoinHandle<()>>> {
let closer = Arc::new(stream.try_clone()?);
let writer = Writer {
stream: stream.try_clone()?,
closer: Arc::clone(&closer),
};
stream.set_write_timeout(self.queue.write_timeout)?;
let sink = Queued::with_limits(writer, self.framing, self.queue)?;
let (framing, config, shared, id, deadlines) = (
self.framing,
self.config.clone(),
Arc::clone(&self.shared),
connection.id,
self.deadlines,
);
{
let mut open = self.shared.lock();
if open.stopped {
closer.shut();
return Ok(None);
}
open.active += 1;
let registered = Arc::clone(&closer);
let _fresh = open.open.insert(id, Box::new(move || registered.shut()));
}
let ending = Arc::clone(&closer);
let started = thread::Builder::new()
.name(format!("rpc-io-connection-{id}"))
.spawn(move || {
let mut endpoint = Endpoint::new(sink, router, state).with_config(config);
let mut stream = stream;
let ended =
serve_connection(&mut stream, &mut endpoint, framing, deadlines, &shared);
event!(debug, connection = id, end = ?ended, "a connection ended");
drop((endpoint, ended));
ending.shut();
shared.ended(id);
});
match started {
Ok(thread) => Ok(Some(thread)),
Err(error) => {
closer.shut();
self.shared.ended(id);
Err(error)
}
}
}
}
impl fmt::Debug for Server {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Server")
.field("framing", &self.framing)
.field("config", &self.config)
.field("queue", &self.queue)
.field("max_connections", &self.max_connections)
.field("message_timeout", &self.deadlines.message)
.field("idle_timeout", &self.deadlines.idle)
.field("connections", &self.connections())
.finish_non_exhaustive()
}
}
fn reachable(addr: SocketAddr) -> SocketAddr {
let mut addr = addr;
if addr.ip().is_unspecified() {
addr.set_ip(match addr {
SocketAddr::V4(_) => std::net::Ipv4Addr::LOCALHOST.into(),
SocketAddr::V6(_) => std::net::Ipv6Addr::LOCALHOST.into(),
});
}
addr
}
fn exhausted(error: &io::Error) -> bool {
if error.kind() == io::ErrorKind::OutOfMemory {
return true;
}
let Some(code) = error.raw_os_error() else {
return false;
};
if cfg!(windows) {
matches!(code, 10024 | 10055)
} else if cfg!(target_os = "linux") || cfg!(target_os = "android") {
matches!(code, 12 | 23 | 24 | 105)
} else {
matches!(code, 12 | 23 | 24 | 55)
}
}
fn transient(error: &io::Error) -> bool {
matches!(
error.kind(),
io::ErrorKind::ConnectionAborted
| io::ErrorKind::ConnectionReset
| io::ErrorKind::Interrupted
)
}
#[cfg(test)]
#[allow(clippy::panic)]
mod tests {
use super::*;
#[test]
fn test_resource_exhaustion_is_waited_out_and_other_errors_are_not() {
let out_of_files = if cfg!(windows) { 10024 } else { 24 };
assert!(exhausted(&io::Error::from_raw_os_error(out_of_files)));
assert!(exhausted(&io::Error::from(io::ErrorKind::OutOfMemory)));
assert!(!exhausted(&io::Error::from(
io::ErrorKind::PermissionDenied
)));
assert!(!exhausted(&io::Error::other("no code")));
assert!(transient(&io::Error::from(
io::ErrorKind::ConnectionAborted
)));
assert!(!transient(&io::Error::from(io::ErrorKind::OutOfMemory)));
}
#[test]
fn test_pause_ends_early_on_a_stop() {
let server = Server::new(Framing::Newline);
let started = Instant::now();
assert!(server.pause(Duration::from_millis(20)));
assert!(started.elapsed() >= Duration::from_millis(15));
let stopper = server.stopper();
let waker = thread::spawn(move || {
thread::sleep(Duration::from_millis(50));
stopper.stop();
});
let started = Instant::now();
assert!(!server.pause(Duration::from_secs(30)));
assert!(started.elapsed() < Duration::from_secs(10));
assert!(waker.join().is_ok());
}
#[test]
fn test_reachable_replaces_unspecified_addresses() {
let v4: SocketAddr = ([0, 0, 0, 0], 9).into();
assert_eq!(reachable(v4).ip(), std::net::Ipv4Addr::LOCALHOST);
let v6: SocketAddr = (std::net::Ipv6Addr::UNSPECIFIED, 9).into();
assert_eq!(reachable(v6).ip(), std::net::Ipv6Addr::LOCALHOST);
let set: SocketAddr = ([10, 0, 0, 1], 9).into();
assert_eq!(reachable(set), set);
}
}