mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-02-27 22:13:12 -08:00
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:
@@ -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 {
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user