Skip to content

Commit

Permalink
perf: handle logic responsible for broadcasting the ready event more …
Browse files Browse the repository at this point in the history
…efficiently
  • Loading branch information
vyfor committed Jan 1, 2025
1 parent e2e2fd3 commit a5c8ca3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/ipc/discord/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct RichClient {
#[cfg(not(target_os = "windows"))]
pub write_pipe: Option<std::os::unix::net::UnixStream>,
pub pid: u32,
pub is_ready: AtomicBool,
pub is_ready: Arc<AtomicBool>,
pub thread_handle: Option<JoinHandle<()>>,
pub is_reconnecting: Arc<AtomicBool>,
}
Expand Down
17 changes: 12 additions & 5 deletions src/ipc/discord/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env::var;
use std::io::{self, Read, Write};
use std::net::Shutdown;
use std::os::unix::net::UnixStream;
use std::sync::atomic::Ordering;
use std::sync::mpsc::Sender;
use std::sync::Arc;

Expand Down Expand Up @@ -51,7 +52,7 @@ impl Connection for RichClient {
read_pipe: Some(read_pipe),
write_pipe: Some(pipe),
pid: std::process::id(),
is_ready: false.into(),
is_ready: Arc::new(false.into()),
thread_handle: None,
is_reconnecting: Arc::new(false.into()),
});
Expand Down Expand Up @@ -82,6 +83,7 @@ impl Connection for RichClient {
fn start_read_thread(&mut self, tx: Sender<Message>) -> crate::Result<()> {
if let Some(mut read_pipe) = self.read_pipe.take() {
let client_id = self.client_id;
let is_ready = self.is_ready.clone();

let handle = std::thread::spawn(move || {
let mut buf = [0u8; 8192];
Expand Down Expand Up @@ -128,10 +130,15 @@ impl Connection for RichClient {
.ok();
break;
}
tx.send(server_event!(
0, Ready
))
.ok();
if !is_ready.swap(
true,
Ordering::SeqCst,
) {
tx.send(server_event!(
0, Ready
))
.ok();
}
}
Opcode::Close => {
tx.send(local_event!(
Expand Down
12 changes: 10 additions & 2 deletions src/ipc/discord/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ffi::OsStr;
use std::fs::File;
use std::os::windows::ffi::OsStrExt;
use std::os::windows::io::{AsRawHandle, FromRawHandle};
use std::sync::atomic::Ordering;
use std::sync::mpsc::Sender;
use std::sync::Arc;
use std::{io, ptr};
Expand Down Expand Up @@ -53,7 +54,7 @@ impl Connection for RichClient {
client_id,
pipe: Some(Arc::new(pipe)),
pid: std::process::id(),
is_ready: false.into(),
is_ready: Arc::new(false.into()),
thread_handle: None,
is_reconnecting: Arc::new(false.into()),
};
Expand All @@ -76,6 +77,7 @@ impl Connection for RichClient {
if let Some(pipe) = self.pipe.as_ref() {
let pipe = pipe.clone();
let client_id = self.client_id;
let is_ready = self.is_ready.clone();

let handle = std::thread::spawn(move || {
let mut buf = [0u8; 8192];
Expand Down Expand Up @@ -167,8 +169,14 @@ impl Connection for RichClient {
.ok();
break;
}
tx.send(server_event!(0, Ready))
if !is_ready
.swap(true, Ordering::SeqCst)
{
tx.send(server_event!(
0, Ready
))
.ok();
}
}
Opcode::Close => {
tx.send(local_event!(
Expand Down
13 changes: 1 addition & 12 deletions src/messages/events/server/ready.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::atomic::Ordering;

use crate::ipc::pipe::PipeServerImpl;
use crate::messages::events::event::{EventContext, OnEvent};
use crate::protocol::msgpack::serialize::Serialize;
Expand All @@ -11,16 +9,7 @@ pub struct ReadyEvent;

impl OnEvent for ReadyEvent {
fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> {
if !ctx
.cord
.rich_client
.read()
.unwrap()
.is_ready
.swap(true, Ordering::SeqCst)
{
ctx.cord.pipe.broadcast(&MsgPack::serialize(&self)?)?;
}
ctx.cord.pipe.broadcast(&MsgPack::serialize(&self)?)?;

Ok(())
}
Expand Down

0 comments on commit a5c8ca3

Please sign in to comment.