Skip to main content

maestro_rs/
lib.rs

1//! # maestro-rs
2//!
3//! Supervised TCP and UDP services bound to a specific network interface.
4//!
5//! Implement [`TcpHandler`] or [`UdpHandler`] for your protocol logic, register
6//! the handlers on a [`Supervisor`] for a [`NetworkInterface`], and call
7//! [`Supervisor::run`]. Maestro takes care of:
8//!
9//! - **Binding**: to every address of the interface (including scoped IPv6
10//!   link-local addresses), to all interfaces, or to one address. See [`BindMode`].
11//! - **Concurrency**: one task per TCP connection, and UDP receive work on
12//!   every CPU core (`SO_REUSEPORT` sharding on Linux, shared sockets elsewhere).
13//! - **Portability**: Linux, macOS, Windows and BSD. See
14//!   [`NetworkInterface::loopback`] for a portable loopback interface.
15//! - **Multicast**: joining groups on the chosen interface.
16//!   See [`UdpHandler::multicast_groups`].
17//! - **Supervision**: restarting failed services with exponential backoff.
18//!   See [`RestartPolicy`].
19//! - **Shutdown**: on Ctrl+C, SIGTERM or Windows console close, or on any future you pass to
20//!   [`Supervisor::run_until`], services stop and in-flight work gets time to finish.
21//!
22//! # Example
23//!
24//! ```no_run
25//! use maestro_rs::{NetworkInterface, Result, Supervisor, TcpHandler, UdpHandler};
26//! use std::{net::SocketAddr, sync::Arc};
27//! use tokio::{
28//!     io::{AsyncReadExt, AsyncWriteExt},
29//!     net::{TcpStream, UdpSocket},
30//! };
31//!
32//! struct Echo;
33//!
34//! impl TcpHandler for Echo {
35//!     fn name(&self) -> &str { "tcp-echo" }
36//!     fn port(&self) -> u16 { 8080 }
37//!
38//!     async fn on_connection(&self, mut stream: TcpStream, _peer: SocketAddr) {
39//!         let mut buf = [0u8; 1024];
40//!         while let Ok(n @ 1..) = stream.read(&mut buf).await {
41//!             if stream.write_all(&buf[..n]).await.is_err() {
42//!                 break;
43//!             }
44//!         }
45//!     }
46//! }
47//!
48//! impl UdpHandler for Echo {
49//!     fn name(&self) -> &str { "udp-echo" }
50//!     fn port(&self) -> u16 { 8080 }
51//!
52//!     async fn on_packet(&self, data: &[u8], socket: &Arc<UdpSocket>, peer: SocketAddr) {
53//!         let _ = socket.send_to(data, peer).await;
54//!     }
55//! }
56//!
57//! #[tokio::main]
58//! async fn main() -> Result<()> {
59//!     let iface = NetworkInterface::loopback()?; // or "eth0".parse()?
60//!     let mut supervisor = Supervisor::new(iface);
61//!     // `Echo` implements both traits, so name the protocol explicitly.
62//!     supervisor
63//!         .add::<maestro_rs::Tcp, _>(Echo)
64//!         .add::<maestro_rs::Udp, _>(Echo);
65//!     supervisor.run().await
66//! }
67//! ```
68//!
69//! # Features
70//!
71//! - `tracing` (default): emit logs through [`tracing`](https://docs.rs/tracing).
72//!   Install a subscriber in your application to see them.
73#![cfg_attr(not(feature = "tracing"), allow(unused_variables))]
74
75mod bind;
76mod error;
77mod interface;
78mod log;
79mod restart;
80mod service;
81mod supervisor;
82mod tcp;
83mod udp;
84
85pub use bind::BindMode;
86pub use error::{Error, Result};
87pub use interface::NetworkInterface;
88pub use restart::RestartPolicy;
89pub use service::{Service, Tcp, Udp};
90pub use supervisor::Supervisor;
91pub use tcp::TcpHandler;
92pub use udp::UdpHandler;