From f2c21ef5b15ef1f345cdc30f4b793e55905f15f4 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Mon, 13 Nov 2017 13:28:08 -0800 Subject: Use the threadpool for framework command execution Instead of executing framework commands in the shard runner thread (potentially blocking the shard runner from reading new messages over the websocket and heartbeating), dispatch framework commands to the shard runner's threadpool. --- src/framework/mod.rs | 11 ++++++----- src/framework/standard/mod.rs | 44 +++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 23 deletions(-) (limited to 'src/framework') diff --git a/src/framework/mod.rs b/src/framework/mod.rs index 85ae6f4..c5ddb04 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -62,13 +62,14 @@ pub use self::standard::StandardFramework; use client::Context; use model::Message; +use threadpool::ThreadPool; #[cfg(feature = "standard_framework")] use model::UserId; /// This trait allows for serenity to either use its builtin framework, or yours. pub trait Framework { - fn dispatch(&mut self, Context, Message); + fn dispatch(&mut self, Context, Message, &ThreadPool); #[doc(hidden)] #[cfg(feature = "standard_framework")] @@ -76,8 +77,8 @@ pub trait Framework { } impl Framework for Box { - fn dispatch(&mut self, ctx: Context, msg: Message) { - (**self).dispatch(ctx, msg); + fn dispatch(&mut self, ctx: Context, msg: Message, threadpool: &ThreadPool) { + (**self).dispatch(ctx, msg, threadpool); } #[cfg(feature = "standard_framework")] @@ -87,8 +88,8 @@ impl Framework for Box { } impl<'a, F: Framework + ?Sized> Framework for &'a mut F { - fn dispatch(&mut self, ctx: Context, msg: Message) { - (**self).dispatch(ctx, msg); + fn dispatch(&mut self, ctx: Context, msg: Message, threadpool: &ThreadPool) { + (**self).dispatch(ctx, msg, threadpool); } #[cfg(feature = "standard_framework")] diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 12ad797..dd7aaae 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -25,6 +25,7 @@ use std::collections::HashMap; use std::default::Default; use std::sync::Arc; use super::Framework; +use threadpool::ThreadPool; #[cfg(feature = "cache")] use client::CACHE; @@ -836,7 +837,12 @@ impl StandardFramework { } impl Framework for StandardFramework { - fn dispatch(&mut self, mut context: Context, message: Message) { + fn dispatch( + &mut self, + mut context: Context, + message: Message, + threadpool: &ThreadPool, + ) { let res = command::positions(&mut context, &message, &self.configuration); let positions = match res { @@ -926,27 +932,29 @@ impl Framework for StandardFramework { return; } - if let Some(before) = before { - if !(before)(&mut context, &message, &built) { - return; + 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; } -- cgit v1.2.3