aboutsummaryrefslogtreecommitdiff
path: root/src/client/mod.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-10-09 11:30:56 -0700
committerZeyla Hellyer <[email protected]>2017-10-09 11:30:56 -0700
commit8109619184867fc843a1e73d18d37726a34f7fbf (patch)
tree68ad2b22c2e650bc01b62e4208e21bbcc38014fb /src/client/mod.rs
parentAdd a threadpool to the shard runner (diff)
downloadserenity-8109619184867fc843a1e73d18d37726a34f7fbf.tar.xz
serenity-8109619184867fc843a1e73d18d37726a34f7fbf.zip
Make the client threadpool user-customizable
In addition to making the threadpool used by client shards customizable by the user, make only a single threadpool (as opposed to one per shard) and share it across all shards.
Diffstat (limited to 'src/client/mod.rs')
-rw-r--r--src/client/mod.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/client/mod.rs b/src/client/mod.rs
index d1de13e..f2fb297 100644
--- a/src/client/mod.rs
+++ b/src/client/mod.rs
@@ -44,6 +44,7 @@ use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
use parking_lot::Mutex;
use std::collections::HashMap;
use std::mem;
+use threadpool::ThreadPool;
use typemap::ShareMap;
use http;
use internal::prelude::*;
@@ -244,6 +245,11 @@ pub struct Client<H: EventHandler + Send + Sync + 'static> {
/// [`Client::start_shard`]: #method.start_shard
/// [`Client::start_shards`]: #method.start_shards
pub shard_runners: Arc<Mutex<HashMap<ShardId, ShardRunnerInfo>>>,
+ /// The threadpool shared by all shards.
+ ///
+ /// Defaults to 5 threads, which should suffice small bots. Consider
+ /// increasing this number as your bot grows.
+ pub threadpool: ThreadPool,
token: Arc<sync::Mutex<String>>,
}
@@ -287,12 +293,16 @@ impl<H: EventHandler + Send + Sync + 'static> Client<H> {
http::set_token(&token);
let locked = Arc::new(sync::Mutex::new(token));
+ let name = "serenity client".to_owned();
+ let threadpool = ThreadPool::with_name(name, 5);
+
feature_framework! {{
Client {
data: Arc::new(Mutex::new(ShareMap::custom())),
event_handler: Arc::new(handler),
framework: Arc::new(sync::Mutex::new(None)),
shard_runners: Arc::new(Mutex::new(HashMap::new())),
+ threadpool,
token: locked,
}
} else {
@@ -300,6 +310,7 @@ impl<H: EventHandler + Send + Sync + 'static> Client<H> {
data: Arc::new(Mutex::new(ShareMap::custom())),
event_handler: Arc::new(handler),
shard_runners: Arc::new(Mutex::new(HashMap::new())),
+ threadpool,
token: locked,
}
}}
@@ -761,6 +772,7 @@ impl<H: EventHandler + Send + Sync + 'static> Client<H> {
self.event_handler.clone(),
#[cfg(feature = "framework")]
self.framework.clone(),
+ self.threadpool.clone(),
);
self.shard_runners = manager.runners.clone();