Skip to content

Commit 5138193

Browse files
author
Zeyla Hellyer
committed
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.
1 parent ef5c75a commit 5138193

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/framework/standard/mod.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::default::Default;
2222
use std::sync::Arc;
2323
use client::Context;
2424
use super::Framework;
25+
use threadpool::ThreadPool;
2526
use model::{ChannelId, GuildId, Guild, Member, Message, UserId};
2627
use model::permissions::Permissions;
2728
use internal::RwLockExt;
@@ -195,6 +196,7 @@ pub struct StandardFramework {
195196
/// ../client/event_handler/trait.EventHandler.html#method.on_message
196197
/// [`Event::MessageCreate`]: ../model/event/enum.Event.html#variant.MessageCreate
197198
pub initialized: bool,
199+
threadpool: ThreadPool,
198200
user_info: (u64, bool),
199201
}
200202

@@ -933,27 +935,29 @@ impl Framework for StandardFramework {
933935
return;
934936
}
935937

936-
if let Some(before) = before {
937-
if !(before)(&mut context, &message, &built) {
938-
return;
938+
self.threadpool.execute(move || {
939+
if let Some(before) = before {
940+
if !(before)(&mut context, &message, &built) {
941+
return;
942+
}
939943
}
940-
}
941944

942-
let result = match command.exec {
943-
CommandType::StringResponse(ref x) => {
944-
let _ = message.channel_id.say(x);
945+
let result = match command.exec {
946+
CommandType::StringResponse(ref x) => {
947+
let _ = message.channel_id.say(x);
945948

946-
Ok(())
947-
},
948-
CommandType::Basic(ref x) => (x)(&mut context, &message, args),
949-
CommandType::WithCommands(ref x) => {
950-
(x)(&mut context, &message, groups, args)
951-
},
952-
};
949+
Ok(())
950+
},
951+
CommandType::Basic(ref x) => (x)(&mut context, &message, args),
952+
CommandType::WithCommands(ref x) => {
953+
(x)(&mut context, &message, groups, args)
954+
},
955+
};
953956

954-
if let Some(after) = after {
955-
(after)(&mut context, &message, &built, result);
956-
}
957+
if let Some(after) = after {
958+
(after)(&mut context, &message, &built, result);
959+
}
960+
});
957961

958962
return;
959963
}

0 commit comments

Comments
 (0)