Skip to content

Commit

Permalink
refactor: use bounded channel (#579)
Browse files Browse the repository at this point in the history
This commit attempts to fix an issue with excessive CPU and memory
usage. At the time there are no steps to reproduce, but one suspicion is
masses of rogue input events from the kernel. Thus the code is changed
to use bounded channels with a reasonably large size (100) that I
wouldn't expect to get filled up.
  • Loading branch information
jtroo authored Oct 11, 2023
1 parent ef23c68 commit 58cfdf6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/kanata/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, bail, Result};
use log::info;
use parking_lot::Mutex;
use std::convert::TryFrom;
use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender as Sender;
use std::sync::Arc;

use super::*;
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Kanata {
}

// Send key events to the processing loop
if let Err(e) = tx.send(key_event) {
if let Err(e) = tx.try_send(key_event) {
bail!("failed to send on channel: {}", e)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use anyhow::{anyhow, bail, Result};
use log::{error, info};
use parking_lot::Mutex;
use std::sync::mpsc::{Receiver, Sender, TryRecvError};
use std::sync::mpsc::{Receiver, SyncSender as Sender, TryRecvError};

use kanata_keyberon::key_code::*;
use kanata_keyberon::layout::*;
Expand Down Expand Up @@ -1475,7 +1475,7 @@ impl Kanata {
self.print_layer(cur_layer);

if let Some(tx) = tx {
match tx.send(ServerMessage::LayerChange { new }) {
match tx.try_send(ServerMessage::LayerChange { new }) {
Ok(_) => {}
Err(error) => {
log::error!("could not send event notification: {}", error);
Expand Down
4 changes: 2 additions & 2 deletions src/kanata/windows/interception.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use kanata_interception as ic;
use parking_lot::Mutex;
use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender as Sender;
use std::sync::Arc;

use super::PRESSED_KEYS;
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Kanata {
}
_ => {}
}
tx.send(key_event)?;
tx.try_send(key_event)?;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/kanata/windows/llhook.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use parking_lot::Mutex;
use std::convert::TryFrom;
use std::sync::mpsc::{channel, Receiver, Sender, TryRecvError};
use std::sync::mpsc::{sync_channel, Receiver, SyncSender as Sender, TryRecvError};
use std::sync::Arc;
use std::time;

Expand All @@ -20,7 +20,7 @@ impl Kanata {
};
native_windows_gui::init()?;

let (preprocess_tx, preprocess_rx) = channel();
let (preprocess_tx, preprocess_rx) = sync_channel(100);
start_event_preprocessor(preprocess_rx, tx);

// This callback should return `false` if the input event is **not** handled by the
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Kanata {
}

fn try_send_panic(tx: &Sender<KeyEvent>, kev: KeyEvent) {
if let Err(e) = tx.send(kev) {
if let Err(e) = tx.try_send(kev) {
panic!("failed to send on channel: {e:?}")
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ fn main_impl() -> Result<()> {
let (server, ntx, nrx) = if let Some(port) = args.port {
let mut server = TcpServer::new(port);
server.start(kanata_arc.clone());
let (ntx, nrx) = std::sync::mpsc::channel();
let (ntx, nrx) = std::sync::mpsc::sync_channel(100);
(Some(server), Some(ntx), Some(nrx))
} else {
(None, None, None)
};

let (tx, rx) = std::sync::mpsc::channel();
let (tx, rx) = std::sync::mpsc::sync_channel(100);
Kanata::start_processing_loop(kanata_arc.clone(), rx, ntx, args.nodelay);

if let (Some(server), Some(nrx)) = (server, nrx) {
Expand Down

0 comments on commit 58cfdf6

Please sign in to comment.