From 5138193c001b8d99f707dffb254b71ce8cefa182 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Sun, 21 Jan 2018 09:28:05 -0800 Subject: [PATCH] Execute framework commands in a ThreadPool The v0.5.x branch threadpools commands and events in the same pool, but this isn't possible in the v0.4.x branch due to backwards compatibility. To resolve this, create a second threadpool for just the framework. Closes #250. --- src/framework/standard/mod.rs | 38 +++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index a806817e357..0ea38cf058f 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -22,6 +22,7 @@ use std::default::Default; use std::sync::Arc; use client::Context; use super::Framework; +use threadpool::ThreadPool; use model::{ChannelId, GuildId, Guild, Member, Message, UserId}; use model::permissions::Permissions; use internal::RwLockExt; @@ -195,6 +196,7 @@ pub struct StandardFramework { /// ../client/event_handler/trait.EventHandler.html#method.on_message /// [`Event::MessageCreate`]: ../model/event/enum.Event.html#variant.MessageCreate pub initialized: bool, + threadpool: ThreadPool, user_info: (u64, bool), } @@ -933,27 +935,29 @@ impl Framework for StandardFramework { return; } - if let Some(before) = before { - if !(before)(&mut context, &message, &built) { - return; + self.threadpool.execute(move || { + if let Some(before) = before { + if !(before)(&mut context, &message, &built) { + return; + } } - } - let result = match command.exec { - CommandType::StringResponse(ref x) => { - let _ = message.channel_id.say(x); + let result = match command.exec { + CommandType::StringResponse(ref x) => { + let _ = message.channel_id.say(x); - Ok(()) - }, - CommandType::Basic(ref x) => (x)(&mut context, &message, args), - CommandType::WithCommands(ref x) => { - (x)(&mut context, &message, groups, args) - }, - }; + Ok(()) + }, + CommandType::Basic(ref x) => (x)(&mut context, &message, args), + CommandType::WithCommands(ref x) => { + (x)(&mut context, &message, groups, args) + }, + }; - if let Some(after) = after { - (after)(&mut context, &message, &built, result); - } + if let Some(after) = after { + (after)(&mut context, &message, &built, result); + } + }); return; }