Reduce cpu usage and fix network monitor

This commit is contained in:
ranfdev
2023-11-07 19:57:54 +01:00
parent ae2084c74a
commit 5c518322be
8 changed files with 68 additions and 28 deletions

View File

@ -14,7 +14,7 @@ pub struct SharedEnv {
db: message_repo::Db,
proxy: Arc<dyn models::NotificationProxy>,
http: reqwest::Client,
network: Arc<ashpd::desktop::network_monitor::NetworkMonitor<'static>>,
network: Arc<dyn models::NetworkMonitorProxy>,
}
#[derive(thiserror::Error, Debug)]

View File

@ -1,6 +1,8 @@
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::OnceLock;
use futures::stream::Stream;
use regex::Regex;
use serde::{Deserialize, Serialize};
@ -316,3 +318,7 @@ pub struct Notification {
pub trait NotificationProxy: Sync + Send {
fn send(&self, n: Notification) -> anyhow::Result<()>;
}
pub trait NetworkMonitorProxy: Sync + Send {
fn listen(&self) -> Pin<Box<dyn Stream<Item = ()>>>;
}

View File

@ -5,7 +5,6 @@ use std::sync::Arc;
use std::time::Duration;
use std::{collections::HashMap, hash::Hash};
use ashpd::desktop::network_monitor::NetworkMonitor;
use capnp::capability::Promise;
use capnp_rpc::{pry, rpc_twoparty_capnp, twoparty, RpcSystem};
use futures::future::join_all;
@ -343,7 +342,7 @@ impl SystemNotifier {
pub fn new(
dbpath: &str,
notification_proxy: Arc<dyn models::NotificationProxy>,
network: Arc<NetworkMonitor<'static>>,
network: Arc<dyn models::NetworkMonitorProxy>,
) -> Self {
Self {
watching: Rc::new(RefCell::new(HashMap::new())),
@ -457,12 +456,12 @@ pub fn start(
socket_path: std::path::PathBuf,
dbpath: &str,
notification_proxy: Arc<dyn models::NotificationProxy>,
network_proxy: Arc<dyn models::NetworkMonitorProxy>,
) -> anyhow::Result<()> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let network_monitor = rt.block_on(async move { NetworkMonitor::new().await.unwrap() });
let listener = rt.block_on(async move {
let _ = std::fs::remove_file(&socket_path);
UnixListener::bind(&socket_path).unwrap()
@ -471,8 +470,7 @@ pub fn start(
let dbpath = dbpath.to_owned();
let f = move || {
let local = tokio::task::LocalSet::new();
let mut system_notifier =
SystemNotifier::new(&dbpath, notification_proxy, Arc::new(network_monitor));
let mut system_notifier = SystemNotifier::new(&dbpath, notification_proxy, network_proxy);
local.spawn_local(async move {
system_notifier.watch_subscribed().await.unwrap();
let system_client: system_notifier::Client = capnp_rpc::new_client(system_notifier);

View File

@ -2,7 +2,6 @@ use std::ops::ControlFlow;
use std::sync::Arc;
use std::time::Duration;
use ashpd::desktop::network_monitor::NetworkMonitor;
use futures::prelude::*;
use reqwest::header::HeaderValue;
use serde::{Deserialize, Serialize};
@ -141,21 +140,14 @@ impl TopicListener {
}
async fn reload_on_network_change(
monitor: Arc<NetworkMonitor<'static>>,
monitor: Arc<dyn models::NetworkMonitorProxy>,
tx: mpsc::Sender<ControlFlow<()>>,
) -> anyhow::Result<()> {
let mut prev_available = false;
loop {
let _ = monitor.receive_changed().await?;
let available = monitor.is_available().await?;
if available && !prev_available {
if let Err(e) = tx.send(ControlFlow::Continue(())).await {
return Err(e.into());
}
}
prev_available = available;
let mut m = monitor.listen();
while let Some(_) = m.next().await {
tx.send(ControlFlow::Continue(())).await?;
}
Ok(())
}
fn send_current_status(&mut self) -> impl Future<Output = anyhow::Result<()>> {