use std::io;
use rama_core::extensions::Extension;
use rama_utils::reactive::{Changed, Reactive, ReactiveRepr};
#[must_use]
pub fn is_connection_error(e: &io::Error) -> bool {
matches!(
e.kind(),
io::ErrorKind::ConnectionRefused
| io::ErrorKind::ConnectionAborted
| io::ErrorKind::ConnectionReset
| io::ErrorKind::UnexpectedEof
| io::ErrorKind::NotConnected
| io::ErrorKind::BrokenPipe
| io::ErrorKind::Interrupted
)
}
#[derive(Debug, Default, Extension)]
#[extension(tags(net))]
pub struct ConnectionHealthWatcher(Reactive<ConnectionHealth>);
impl ConnectionHealthWatcher {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn mark_healthy(&self) {
self.update_health(ConnectionHealth::Healthy);
}
pub fn mark_broken(&self) {
self.update_health(ConnectionHealth::Broken);
}
pub fn update_health(&self, health: ConnectionHealth) {
self.0.set(health);
}
#[must_use]
pub fn health(&self) -> ConnectionHealth {
self.0.get()
}
#[must_use]
pub fn watch(&self) -> Changed<ConnectionHealth> {
self.0.watch()
}
}
#[derive(Debug, PartialEq, Clone, Copy, Eq, Default)]
pub enum ConnectionHealth {
Broken,
#[default]
Healthy,
}
impl ReactiveRepr for ConnectionHealth {
fn to_usize(self) -> usize {
match self {
Self::Healthy => 0,
Self::Broken => 1,
}
}
fn from_usize(value: usize) -> Self {
match value {
0 => Self::Healthy,
_ => Self::Broken,
}
}
}
#[derive(Debug, Extension)]
#[extension(tags(net))]
pub struct MaxConcurrency(Reactive<usize>);
impl MaxConcurrency {
#[must_use]
pub fn new(max: usize) -> Self {
Self(Reactive::new(max))
}
pub fn set(&self, max: usize) {
self.0.set(max);
}
#[must_use]
pub fn get(&self) -> usize {
self.0.get()
}
#[must_use]
pub fn watch(&self) -> Changed<usize> {
self.0.watch()
}
}