Skip to content

Commit

Permalink
Fix memory leak in kuma-client
Browse files Browse the repository at this point in the history
resolves #1
  • Loading branch information
BigBoot committed Feb 6, 2024
1 parent 53004bf commit 6315376
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Memory leak in kuma-client [#1](https://github.com/BigBoot/AutoKuma/issues/1)

## [0.3.0] - 2024-01-13
### Added
Expand Down
68 changes: 38 additions & 30 deletions kuma-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ use rust_socketio::{
};
use serde::de::DeserializeOwned;
use serde_json::{json, Value};
use std::{collections::HashMap, mem, str::FromStr, sync::Arc, time::Duration};
use std::{
collections::HashMap,
mem,
str::FromStr,
sync::{Arc, Weak},
time::Duration,
};
use tokio::{runtime::Handle, sync::Mutex};

struct Ready {
Expand Down Expand Up @@ -952,47 +958,49 @@ impl Worker {
}

let handle = Handle::current();
let self_ref = self.to_owned();
let self_ref = Arc::downgrade(self);
let client = builder
.on_any(move |event, payload, _| {
let handle = handle.clone();
let self_ref: Arc<Worker> = self_ref.clone();
let self_ref: Weak<Worker> = self_ref.clone();
trace!("Client::on_any({:?}, {:?})", &event, &payload);
async move {
match (event, payload) {
(SocketIOEvent::Message, Payload::Text(params)) => {
if let Ok(e) = Event::from_str(
&params[0]
.as_str()
.log_warn(|| "Error while deserializing Event...")
.unwrap_or(""),
) {
handle.clone().spawn(async move {
_ = self_ref.clone().on_event(e, json!(null)).await.log_warn(
|e| {
if let Some(arc) = self_ref.upgrade() {
match (event, payload) {
(SocketIOEvent::Message, Payload::Text(params)) => {
if let Ok(e) = Event::from_str(
&params[0]
.as_str()
.log_warn(|| "Error while deserializing Event...")
.unwrap_or(""),
) {
handle.clone().spawn(async move {
_ = arc.on_event(e, json!(null)).await.log_warn(|e| {
format!(
"Error while sending message event: {}",
e.to_string()
)
},
);
});
}
}
(event, Payload::Text(params)) => {
if let Ok(e) = Event::from_str(&String::from(event)) {
handle.clone().spawn(async move {
_ = self_ref
.clone()
.on_event(e, params.into_iter().next().unwrap())
.await
.log_warn(|e| {
format!("Error while sending event: {}", e.to_string())
});
});
});
}
}
(event, Payload::Text(params)) => {
if let Ok(e) = Event::from_str(&String::from(event)) {
handle.clone().spawn(async move {
_ = arc
.on_event(e, params.into_iter().next().unwrap())
.await
.log_warn(|e| {
format!(
"Error while sending event: {}",
e.to_string()
)
});
});
}
}
_ => {}
}
_ => {}
}
}
.boxed()
Expand Down

0 comments on commit 6315376

Please sign in to comment.