feat(API): Use mio::Token based polling

Avoid polling every single IO source to collect events,
poll those specific IO sources mio tells us about.
This commit is contained in:
Karolin Varner
2024-08-18 21:19:44 +02:00
parent 53e560191f
commit 77760d71df
13 changed files with 403 additions and 98 deletions

View File

@@ -16,6 +16,7 @@ use crate::{SerializedBrokerConfig, WireGuardBroker, WireguardBrokerMio};
#[derive(Debug)]
pub struct MioBrokerClient {
inner: BrokerClient<MioBrokerClientIo>,
mio_token: Option<mio::Token>,
}
#[derive(Debug)]
@@ -59,7 +60,10 @@ impl MioBrokerClient {
write_buffer,
};
let inner = BrokerClient::new(io);
Self { inner }
Self {
inner,
mio_token: None,
}
}
fn poll(&mut self) -> anyhow::Result<()> {
@@ -104,6 +108,7 @@ impl WireguardBrokerMio for MioBrokerClient {
registry: &mio::Registry,
token: mio::Token,
) -> Result<(), Self::MioError> {
self.mio_token = Some(token);
registry.register(
&mut self.inner.io_mut().socket,
token,
@@ -118,9 +123,14 @@ impl WireguardBrokerMio for MioBrokerClient {
}
fn unregister(&mut self, registry: &mio::Registry) -> Result<(), Self::MioError> {
self.mio_token = None;
registry.deregister(&mut self.inner.io_mut().socket)?;
Ok(())
}
fn mio_token(&self) -> Option<mio::Token> {
self.mio_token
}
}
impl BrokerClientIo for MioBrokerClientIo {

View File

@@ -16,7 +16,9 @@ const MAX_B64_KEY_SIZE: usize = WG_KEY_LEN * 5 / 3;
const MAX_B64_PEER_ID_SIZE: usize = WG_PEER_LEN * 5 / 3;
#[derive(Debug)]
pub struct NativeUnixBroker {}
pub struct NativeUnixBroker {
mio_token: Option<mio::Token>,
}
impl Default for NativeUnixBroker {
fn default() -> Self {
@@ -26,7 +28,7 @@ impl Default for NativeUnixBroker {
impl NativeUnixBroker {
pub fn new() -> Self {
Self {}
Self { mio_token: None }
}
}
@@ -88,8 +90,9 @@ impl WireguardBrokerMio for NativeUnixBroker {
fn register(
&mut self,
_registry: &mio::Registry,
_token: mio::Token,
token: mio::Token,
) -> Result<(), Self::MioError> {
self.mio_token = Some(token);
Ok(())
}
@@ -98,8 +101,13 @@ impl WireguardBrokerMio for NativeUnixBroker {
}
fn unregister(&mut self, _registry: &mio::Registry) -> Result<(), Self::MioError> {
self.mio_token = None;
Ok(())
}
fn mio_token(&self) -> Option<mio::Token> {
self.mio_token
}
}
#[derive(Debug, Builder)]

View File

@@ -28,6 +28,8 @@ pub trait WireguardBrokerMio: WireGuardBroker {
registry: &mio::Registry,
token: mio::Token,
) -> Result<(), Self::MioError>;
fn mio_token(&self) -> Option<mio::Token>;
/// Run after a mio::poll operation
fn process_poll(&mut self) -> Result<(), Self::MioError>;