Skip to content

Commit

Permalink
Execute framework commands in a ThreadPool
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Zeyla Hellyer committed Jan 21, 2018
1 parent ef5c75a commit 5138193
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/framework/standard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5138193

Please sign in to comment.