diff options
| author | Zeyla Hellyer <[email protected]> | 2017-10-09 11:30:56 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-10-09 11:30:56 -0700 |
| commit | 8109619184867fc843a1e73d18d37726a34f7fbf (patch) | |
| tree | 68ad2b22c2e650bc01b62e4208e21bbcc38014fb /src | |
| parent | Add a threadpool to the shard runner (diff) | |
| download | serenity-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')
| -rw-r--r-- | src/client/bridge/gateway/shard_manager.rs | 5 | ||||
| -rw-r--r-- | src/client/bridge/gateway/shard_queuer.rs | 4 | ||||
| -rw-r--r-- | src/client/bridge/gateway/shard_runner.rs | 32 | ||||
| -rw-r--r-- | src/client/mod.rs | 12 |
4 files changed, 38 insertions, 15 deletions
diff --git a/src/client/bridge/gateway/shard_manager.rs b/src/client/bridge/gateway/shard_manager.rs index 5dbe0c3..2068bef 100644 --- a/src/client/bridge/gateway/shard_manager.rs +++ b/src/client/bridge/gateway/shard_manager.rs @@ -12,6 +12,7 @@ use super::{ ShardQueuerMessage, ShardRunnerInfo, }; +use threadpool::ThreadPool; use typemap::ShareMap; #[cfg(feature = "framework")] @@ -40,6 +41,7 @@ impl ShardManager { data: Arc<ParkingLotMutex<ShareMap>>, event_handler: Arc<H>, framework: Arc<Mutex<Option<Box<Framework + Send>>>>, + threadpool: ThreadPool, ) -> Self where H: EventHandler + Send + Sync + 'static { let (thread_tx, thread_rx) = mpsc::channel(); let (shard_queue_tx, shard_queue_rx) = mpsc::channel(); @@ -56,6 +58,7 @@ impl ShardManager { rx: shard_queue_rx, token: token.clone(), ws_url: ws_url.clone(), + threadpool, }; thread::spawn(move || { @@ -81,6 +84,7 @@ impl ShardManager { token: Arc<Mutex<String>>, data: Arc<ParkingLotMutex<ShareMap>>, event_handler: Arc<H>, + threadpool: ThreadPool, ) -> Self where H: EventHandler + Send + Sync + 'static { let (thread_tx, thread_rx) = mpsc::channel(); let (shard_queue_tx, shard_queue_rx) = mpsc::channel(); @@ -96,6 +100,7 @@ impl ShardManager { rx: shard_queue_rx, token: token.clone(), ws_url: ws_url.clone(), + threadpool, }; thread::spawn(move || { diff --git a/src/client/bridge/gateway/shard_queuer.rs b/src/client/bridge/gateway/shard_queuer.rs index f35acb7..ea49de4 100644 --- a/src/client/bridge/gateway/shard_queuer.rs +++ b/src/client/bridge/gateway/shard_queuer.rs @@ -14,6 +14,7 @@ use super::{ ShardRunner, ShardRunnerInfo, }; +use threadpool::ThreadPool; use typemap::ShareMap; #[cfg(feature = "framework")] @@ -34,6 +35,7 @@ pub struct ShardQueuer<H: EventHandler + Send + Sync + 'static> { pub manager_tx: Sender<ShardManagerMessage>, pub runners: Arc<ParkingLotMutex<HashMap<ShardId, ShardRunnerInfo>>>, pub rx: Receiver<ShardQueuerMessage>, + pub threadpool: ThreadPool, pub token: Arc<Mutex<String>>, pub ws_url: Arc<Mutex<String>>, } @@ -88,6 +90,7 @@ impl<H: EventHandler + Send + Sync + 'static> ShardQueuer<H> { self.framework.clone(), self.data.clone(), self.event_handler.clone(), + self.threadpool.clone(), ) } else { ShardRunner::new( @@ -95,6 +98,7 @@ impl<H: EventHandler + Send + Sync + 'static> ShardQueuer<H> { self.manager_tx.clone(), self.data.clone(), self.event_handler.clone(), + self.threadpool.clone(), ) }}; diff --git a/src/client/bridge/gateway/shard_runner.rs b/src/client/bridge/gateway/shard_runner.rs index 6f6b050..1b1de0e 100644 --- a/src/client/bridge/gateway/shard_runner.rs +++ b/src/client/bridge/gateway/shard_runner.rs @@ -30,16 +30,17 @@ pub struct ShardRunner<H: EventHandler + Send + Sync + 'static> { impl<H: EventHandler + Send + Sync + 'static> ShardRunner<H> { #[cfg(feature = "framework")] - pub fn new(shard: LockedShard, - manager_tx: Sender<ShardManagerMessage>, - framework: Arc<Mutex<Option<Box<Framework + Send>>>>, - data: Arc<ParkingLotMutex<ShareMap>>, - event_handler: Arc<H>) -> Self { + pub fn new( + shard: LockedShard, + manager_tx: Sender<ShardManagerMessage>, + framework: Arc<Mutex<Option<Box<Framework + Send>>>>, + data: Arc<ParkingLotMutex<ShareMap>>, + event_handler: Arc<H>, + threadpool: ThreadPool, + ) -> Self { let (tx, rx) = mpsc::channel(); let shard_info = shard.lock().shard_info(); - let name = format!("threadpool {:?}", shard_info); - Self { runner_rx: rx, runner_tx: tx, @@ -49,20 +50,21 @@ impl<H: EventHandler + Send + Sync + 'static> ShardRunner<H> { manager_tx, shard, shard_info, - threadpool: ThreadPool::with_name(name, 15), + threadpool, } } #[cfg(not(feature = "framework"))] - pub fn new(shard: LockedShard, - manager_tx: Sender<ShardManagerMessage>, - data: Arc<ParkingLotMutex<ShareMap>>, - event_handler: Arc<H>) -> Self { + pub fn new( + shard: LockedShard, + manager_tx: Sender<ShardManagerMessage>, + data: Arc<ParkingLotMutex<ShareMap>>, + event_handler: Arc<H>, + threadpool: ThreadPool, + ) -> Self { let (tx, rx) = mpsc::channel(); let shard_info = shard.lock().shard_info(); - let name = format!("threadpool {:?}", shard_info); - Self { runner_rx: rx, runner_tx: tx, @@ -71,7 +73,7 @@ impl<H: EventHandler + Send + Sync + 'static> ShardRunner<H> { manager_tx, shard, shard_info, - threadpool: ThreadPool::with_name(name, 15), + threadpool, } } 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(); |