mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-04 20:21:02 +02:00
fix: avoid creating loads of sway/mpd clients
This commit is contained in:
parent
649b0efb19
commit
6dcae66570
9 changed files with 254 additions and 136 deletions
|
@ -1,55 +1,67 @@
|
|||
use lazy_static::lazy_static;
|
||||
use mpd_client::commands::responses::Status;
|
||||
use mpd_client::raw::MpdProtocolError;
|
||||
use mpd_client::{Client, Connection};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::net::{TcpStream, UnixStream};
|
||||
use tokio::spawn;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub async fn wait_for_connection(
|
||||
hosts: Vec<String>,
|
||||
lazy_static! {
|
||||
static ref CLIENTS: Arc<Mutex<HashMap<String, Arc<Client>>>> =
|
||||
Arc::new(Mutex::new(HashMap::new()));
|
||||
}
|
||||
|
||||
pub async fn get_connection(host: &str) -> Option<Arc<Client>> {
|
||||
let mut clients = CLIENTS.lock().await;
|
||||
|
||||
match clients.get(host) {
|
||||
Some(client) => Some(Arc::clone(client)),
|
||||
None => {
|
||||
let client = wait_for_connection(host, Duration::from_secs(5), None).await?;
|
||||
let client = Arc::new(client);
|
||||
clients.insert(host.to_string(), Arc::clone(&client));
|
||||
Some(client)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_for_connection(
|
||||
host: &str,
|
||||
interval: Duration,
|
||||
max_retries: Option<usize>,
|
||||
) -> Option<Client> {
|
||||
let mut retries = 0;
|
||||
let max_retries = max_retries.unwrap_or(usize::MAX);
|
||||
|
||||
spawn(async move {
|
||||
let max_retries = max_retries.unwrap_or(usize::MAX);
|
||||
loop {
|
||||
if retries == max_retries {
|
||||
break None;
|
||||
}
|
||||
|
||||
if let Some(conn) = try_get_mpd_conn(&hosts).await {
|
||||
break Some(conn.0);
|
||||
}
|
||||
|
||||
retries += 1;
|
||||
sleep(interval).await;
|
||||
loop {
|
||||
if retries == max_retries {
|
||||
break None;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("Error occurred while handling tasks")
|
||||
|
||||
if let Some(conn) = try_get_mpd_conn(host).await {
|
||||
break Some(conn.0);
|
||||
}
|
||||
|
||||
retries += 1;
|
||||
sleep(interval).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Cycles through each MPD host and
|
||||
/// returns the first one which connects,
|
||||
/// or none if there are none
|
||||
async fn try_get_mpd_conn(hosts: &[String]) -> Option<Connection> {
|
||||
for host in hosts {
|
||||
let connection = if is_unix_socket(host) {
|
||||
connect_unix(host).await
|
||||
} else {
|
||||
connect_tcp(host).await
|
||||
};
|
||||
async fn try_get_mpd_conn(host: &str) -> Option<Connection> {
|
||||
let connection = if is_unix_socket(host) {
|
||||
connect_unix(host).await
|
||||
} else {
|
||||
connect_tcp(host).await
|
||||
};
|
||||
|
||||
if let Ok(connection) = connection {
|
||||
return Some(connection);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
connection.ok()
|
||||
}
|
||||
|
||||
fn is_unix_socket(host: &str) -> bool {
|
||||
|
|
|
@ -2,7 +2,7 @@ mod client;
|
|||
mod popup;
|
||||
|
||||
use self::popup::Popup;
|
||||
use crate::modules::mpd::client::{get_duration, get_elapsed, wait_for_connection};
|
||||
use crate::modules::mpd::client::{get_connection, get_duration, get_elapsed};
|
||||
use crate::modules::mpd::popup::{MpdPopup, PopupEvent};
|
||||
use crate::modules::{Module, ModuleInfo};
|
||||
use color_eyre::Result;
|
||||
|
@ -120,7 +120,7 @@ impl Module<Button> for MpdModule {
|
|||
let host = self.host.clone();
|
||||
let host2 = self.host.clone();
|
||||
spawn(async move {
|
||||
let client = wait_for_connection(vec![host], Duration::from_secs(1), None)
|
||||
let client = get_connection(&host)
|
||||
.await
|
||||
.expect("Unexpected error when trying to connect to MPD server");
|
||||
|
||||
|
@ -145,7 +145,7 @@ impl Module<Button> for MpdModule {
|
|||
});
|
||||
|
||||
spawn(async move {
|
||||
let client = wait_for_connection(vec![host2], Duration::from_secs(1), None)
|
||||
let client = get_connection(&host2)
|
||||
.await
|
||||
.expect("Unexpected error when trying to connect to MPD server");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue