From 620bea052161e43913eb757b49043b46c41b3c85 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 27 Sep 2020 09:06:57 +0000 Subject: [PATCH] Only use one instance of state instead of cloning it everywhere. --- .#README.md | 1 + README.md | 7 +++++++ src/bot.rs | 15 ++++++++------- 3 files changed, 16 insertions(+), 7 deletions(-) create mode 120000 .#README.md diff --git a/.#README.md b/.#README.md new file mode 120000 index 0000000..ae7fb98 --- /dev/null +++ b/.#README.md @@ -0,0 +1 @@ +jeff@seraph.30389:1600505143 \ No newline at end of file diff --git a/README.md b/README.md index 327b678..3b26718 100644 --- a/README.md +++ b/README.md @@ -113,8 +113,15 @@ application, and creating a config file that looks like this: home_server = https://example.com' username = 'thisismyusername' password = 'thisismypassword' + +[bot] +oldest_message_age = 300 ``` +By adding the `[bot]` section, one can send the `oldset_message_age` +setting, which is the number of seconds to ceck in the test before +attempting to repsond to a connection. + Make sure to replace the information with your own. Then you can run the "dicebot" binary. It takes the path to the configuration file as its single argument. diff --git a/src/bot.rs b/src/bot.rs index cfb9e54..b62cc5e 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -14,7 +14,7 @@ use matrix_sdk_common_macros::async_trait; use serde::{self, Deserialize, Serialize}; use std::clone::Clone; use std::ops::Sub; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::time::{Duration, SystemTime}; use thiserror::Error; use url::Url; @@ -79,7 +79,7 @@ pub struct DiceBot { /// Current state of the dice bot. Held in an Arc because it /// accessed by the multi-threaded matrix SDK event handlers. - state: Arc, + state: Arc>, } impl DiceBot { @@ -89,7 +89,7 @@ impl DiceBot { DiceBot { config: config, client: client, - state: Arc::new(DiceBotState::new()), + state: Arc::new(Mutex::new(DiceBotState::new())), } } } @@ -98,7 +98,7 @@ impl DiceBot { /// transitions. This is a simple mutable trait whose values represent /// the current state of the dicebot. It provides mutable methods to /// change state. -#[derive(Clone, Copy)] +//#[derive(Clone, Copy)] pub struct DiceBotState { logged_skipped_old_message: bool, } @@ -113,9 +113,9 @@ impl DiceBotState { /// Log and record that we have skipped some old messages. This /// method will log once, and then no-op from that point on. - fn skipped_old_messages(mut self) { + fn skipped_old_messages(&mut self) { if !self.logged_skipped_old_message { - info!("Skipped some messages because they are too old."); + info!("Skipped some messages received while offline because they are too old."); } self.logged_skipped_old_message = true; @@ -216,7 +216,8 @@ impl EventEmitter for DiceBot { //Ignore messages that are older than configured duration. if !check_message_age(event, get_oldest_message_age(&self.config)) { - self.state.skipped_old_messages(); + let mut state = self.state.lock().unwrap(); + (*state).skipped_old_messages(); return; }