aboutsummaryrefslogtreecommitdiff
path: root/src/framework
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-11-13 13:28:08 -0800
committerZeyla Hellyer <[email protected]>2017-11-13 13:29:03 -0800
commitf2c21ef5b15ef1f345cdc30f4b793e55905f15f4 (patch)
tree33a0d5e68976a31d750cefaf78df8b2577b63578 /src/framework
parentFix strange behaviour when the prefix has spaces (#215) (diff)
downloadserenity-f2c21ef5b15ef1f345cdc30f4b793e55905f15f4.tar.xz
serenity-f2c21ef5b15ef1f345cdc30f4b793e55905f15f4.zip
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.
Diffstat (limited to 'src/framework')
-rw-r--r--src/framework/mod.rs11
-rw-r--r--src/framework/standard/mod.rs44
2 files changed, 32 insertions, 23 deletions
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<F: Framework + ?Sized> Framework for Box<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")]
@@ -87,8 +88,8 @@ impl<F: Framework + ?Sized> Framework for Box<F> {
}
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;
}