Skip to content

Commit

Permalink
perf: improve startup speed (#129)
Browse files Browse the repository at this point in the history
* perf: improve startup speed

* perf: make ctx a global variable
  • Loading branch information
micielski authored Jan 21, 2025
1 parent 99596bb commit 24e328d
Show file tree
Hide file tree
Showing 25 changed files with 308 additions and 355 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/target
.direnv/
.envrc
/result
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions rm-main/src/transmission/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub enum TorrentAction {
pub async fn action_handler(
mut client: TransClient,
mut trans_rx: UnboundedReceiver<TorrentAction>,
action_tx: UnboundedSender<UpdateAction>,
update_tx: UnboundedSender<UpdateAction>,
) {
while let Some(action) = trans_rx.recv().await {
match action {
Expand All @@ -74,15 +74,15 @@ pub async fn action_handler(
};
match client.torrent_add(args).await {
Ok(_) => {
action_tx.send(UpdateAction::StatusTaskSuccess).unwrap();
update_tx.send(UpdateAction::StatusTaskSuccess).unwrap();
}
Err(err) => {
let msg = format!("Failed to add torrent with URL/Path: \"{url}\"");
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
action_tx.send(UpdateAction::StatusTaskFailure).unwrap();
update_tx.send(UpdateAction::StatusTaskFailure).unwrap();
}
}
}
Expand All @@ -92,7 +92,7 @@ pub async fn action_handler(
Err(err) => {
let msg = format!("Failed to stop torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
Expand All @@ -104,35 +104,35 @@ pub async fn action_handler(
Err(err) => {
let msg = format!("Failed to start torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
}
}
TorrentAction::DelWithFiles(ids) => {
match client.torrent_remove(ids.clone(), true).await {
Ok(_) => action_tx.send(UpdateAction::StatusTaskSuccess).unwrap(),
Ok(_) => update_tx.send(UpdateAction::StatusTaskSuccess).unwrap(),
Err(err) => {
let msg = format!("Failed to remove torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
action_tx.send(UpdateAction::StatusTaskFailure).unwrap();
update_tx.send(UpdateAction::StatusTaskFailure).unwrap();
}
}
}
TorrentAction::DelWithoutFiles(ids) => {
match client.torrent_remove(ids.clone(), false).await {
Ok(_) => action_tx.send(UpdateAction::StatusTaskSuccess).unwrap(),
Ok(_) => update_tx.send(UpdateAction::StatusTaskSuccess).unwrap(),
Err(err) => {
let msg = format!("Failed to remove torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
action_tx.send(UpdateAction::StatusTaskFailure).unwrap();
update_tx.send(UpdateAction::StatusTaskFailure).unwrap();
}
}
}
Expand All @@ -145,7 +145,7 @@ pub async fn action_handler(
ids
);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
Expand All @@ -158,7 +158,7 @@ pub async fn action_handler(
Err(err) => {
let msg = "Failed to get session data";
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
Expand All @@ -170,7 +170,7 @@ pub async fn action_handler(
{
let msg = format!("Failed to move torrent to new directory:\n{new_directory}");
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
Expand Down Expand Up @@ -222,14 +222,14 @@ pub async fn action_handler(
..Default::default()
};
match client.torrent_set(args, Some(ids)).await {
Ok(_) => action_tx.send(UpdateAction::StatusTaskSuccess).unwrap(),
Ok(_) => update_tx.send(UpdateAction::StatusTaskSuccess).unwrap(),
Err(err) => {
let msg = "Failed to set category";
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
action_tx.send(UpdateAction::StatusTaskFailure).unwrap();
update_tx.send(UpdateAction::StatusTaskFailure).unwrap();
}
}
}
Expand All @@ -238,14 +238,14 @@ pub async fn action_handler(
.torrent_rename_path(vec![id], current_name, new_name)
.await
{
Ok(_) => action_tx.send(UpdateAction::StatusTaskSuccess).unwrap(),
Ok(_) => update_tx.send(UpdateAction::StatusTaskSuccess).unwrap(),
Err(err) => {
let msg = "Failed to rename a torrent";
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
update_tx
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
action_tx.send(UpdateAction::StatusTaskFailure).unwrap();
update_tx.send(UpdateAction::StatusTaskFailure).unwrap();
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions rm-main/src/transmission/fetchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,64 @@ use transmission_rpc::types::TorrentGetField;

use rm_shared::action::UpdateAction;

use crate::tui::app;
use crate::tui::app::CTX;

use super::TorrentAction;

pub async fn stats(ctx: app::Ctx) {
pub async fn stats() {
loop {
let (stats_tx, stats_rx) = oneshot::channel();
ctx.send_torrent_action(TorrentAction::GetSessionStats(stats_tx));
CTX.send_torrent_action(TorrentAction::GetSessionStats(stats_tx));

match stats_rx.await.unwrap() {
Ok(stats) => {
ctx.send_update_action(UpdateAction::SessionStats(stats));
CTX.send_update_action(UpdateAction::SessionStats(stats));
}
Err(err_message) => {
ctx.send_update_action(UpdateAction::Error(err_message));
CTX.send_update_action(UpdateAction::Error(err_message));
}
};

tokio::time::sleep(Duration::from_secs(CONFIG.connection.stats_refresh)).await;
}
}

pub async fn free_space(ctx: app::Ctx) {
pub async fn free_space() {
let download_dir = loop {
let (sess_tx, sess_rx) = oneshot::channel();
ctx.send_torrent_action(TorrentAction::GetSessionGet(sess_tx));
CTX.send_torrent_action(TorrentAction::GetSessionGet(sess_tx));
match sess_rx.await.unwrap() {
Ok(sess) => {
break sess.download_dir.leak();
}
Err(err_message) => {
ctx.send_update_action(UpdateAction::Error(err_message));
CTX.send_update_action(UpdateAction::Error(err_message));
tokio::time::sleep(Duration::from_secs(10)).await;
}
};
};

loop {
let (space_tx, space_rx) = oneshot::channel();
ctx.send_torrent_action(TorrentAction::GetFreeSpace(
CTX.send_torrent_action(TorrentAction::GetFreeSpace(
download_dir.to_string(),
space_tx,
));

match space_rx.await.unwrap() {
Ok(free_space) => {
ctx.send_update_action(UpdateAction::FreeSpace(Arc::new(free_space)));
CTX.send_update_action(UpdateAction::FreeSpace(Arc::new(free_space)));
}
Err(err_message) => {
ctx.send_update_action(UpdateAction::Error(err_message));
CTX.send_update_action(UpdateAction::Error(err_message));
}
}

tokio::time::sleep(Duration::from_secs(CONFIG.connection.free_space_refresh)).await;
}
}

pub async fn torrents(ctx: app::Ctx) {
pub async fn torrents() {
loop {
let fields = vec![
TorrentGetField::Id,
Expand All @@ -87,14 +87,14 @@ pub async fn torrents(ctx: app::Ctx) {
TorrentGetField::Labels,
];
let (torrents_tx, torrents_rx) = oneshot::channel();
ctx.send_torrent_action(TorrentAction::GetTorrents(fields, torrents_tx));
CTX.send_torrent_action(TorrentAction::GetTorrents(fields, torrents_tx));

match torrents_rx.await.unwrap() {
Ok(torrents) => {
ctx.send_update_action(UpdateAction::UpdateTorrents(torrents));
CTX.send_update_action(UpdateAction::UpdateTorrents(torrents));
}
Err(err_message) => {
ctx.send_update_action(UpdateAction::Error(err_message));
CTX.send_update_action(UpdateAction::Error(err_message));
}
};

Expand Down
Loading

0 comments on commit 24e328d

Please sign in to comment.