@@ -118,6 +118,15 @@ impl ShardManager {
118118 }
119119 }
120120
121+ /// Retrieves a function which can be used to shut down the ShardManager later.
122+ ///
123+ /// This function will return `true` if the ShardManager has successfully been
124+ /// notified to shut down, or false if it has already shut down and been dropped.
125+ pub fn get_shutdown_trigger ( & self ) -> impl FnOnce ( ) -> bool + Send + use < > {
126+ let manager_tx = self . manager_tx . clone ( ) ;
127+ move || manager_tx. unbounded_send ( ShardManagerMessage :: Quit ( Ok ( ( ) ) ) ) . is_ok ( )
128+ }
129+
121130 /// The main interface for starting the management of shards. Initializes the shards by
122131 /// queueing them for starting, and then listens for [`ShardManagerMessage`]s in a loop.
123132 ///
@@ -161,7 +170,7 @@ impl ShardManager {
161170 /// Note that this queues all shards but does not actually start them. To start the manager's
162171 /// event loop and dispatch [`ShardRunner`]s as they get queued, call [`Self::run`] instead.
163172 #[ cfg_attr( feature = "tracing_instrument" , instrument( skip( self ) ) ) ]
164- pub fn initialize ( & mut self , shard_index : u16 , shard_init : u16 , shard_total : NonZeroU16 ) {
173+ fn initialize ( & mut self , shard_index : u16 , shard_init : u16 , shard_total : NonZeroU16 ) {
165174 let shard_to = shard_index + shard_init;
166175
167176 self . shard_total = shard_total;
@@ -177,39 +186,6 @@ impl ShardManager {
177186 self . queue . push_back ( shard_id) ;
178187 }
179188
180- /// Restarts a shard runner.
181- ///
182- /// Sends a shutdown signal to a shard's associated [`ShardRunner`], and then queues an
183- /// initialization of a new shard runner for the same shard.
184- ///
185- /// [`ShardRunner`]: super::ShardRunner
186- #[ cfg_attr( feature = "tracing_instrument" , instrument( skip( self ) ) ) ]
187- pub fn restart ( & mut self , shard_id : ShardId ) {
188- info ! ( "Restarting shard {shard_id}" ) ;
189-
190- if let Some ( ( _, ( _, tx) ) ) = self . runners . remove ( & shard_id) {
191- if let Err ( why) = tx. unbounded_send ( ShardRunnerMessage :: Restart ) {
192- warn ! ( "Failed to send restart signal to shard {shard_id}: {why:?}" ) ;
193- }
194- }
195- }
196-
197- /// Attempts to shut down the shard runner by Id.
198- ///
199- /// **Note**: If the receiving end of an mpsc channel - owned by the shard runner - no longer
200- /// exists, then the shard runner will not know it should shut down. This _should never happen_.
201- /// It may already be stopped.
202- #[ cfg_attr( feature = "tracing_instrument" , instrument( skip( self ) ) ) ]
203- pub fn shutdown ( & mut self , shard_id : ShardId , code : u16 ) {
204- info ! ( "Shutting down shard {}" , shard_id) ;
205-
206- if let Some ( ( _, ( _, tx) ) ) = self . runners . remove ( & shard_id) {
207- if let Err ( why) = tx. unbounded_send ( ShardRunnerMessage :: Shutdown ( code) ) {
208- warn ! ( "Failed to send shutdown signal to shard {shard_id}: {why:?}" ) ;
209- }
210- }
211- }
212-
213189 // This function assumes that each of the shard ids are bucketed separately according to
214190 // `max_concurrency`. If this assumption is violated, you will likely get ratelimited.
215191 //
@@ -291,23 +267,6 @@ impl ShardManager {
291267 Ok ( ( ) )
292268 }
293269
294- /// Returns whether the shard manager contains an active instance of a shard runner responsible
295- /// for the given ID.
296- ///
297- /// If a shard has been queued but has not yet been initiated, then this will return `false`.
298- #[ must_use]
299- pub fn has ( & self , shard_id : ShardId ) -> bool {
300- self . runners . contains_key ( & shard_id)
301- }
302-
303- /// Returns the [`ShardId`]s of the shards that have been instantiated and currently have a
304- /// valid [`ShardRunner`].
305- #[ cfg_attr( feature = "tracing_instrument" , instrument( skip( self ) ) ) ]
306- #[ must_use]
307- pub fn shards_instantiated ( & self ) -> Vec < ShardId > {
308- self . runners . iter ( ) . map ( |entries| * entries. key ( ) ) . collect ( )
309- }
310-
311270 /// Returns the gateway intents used for this gateway connection.
312271 #[ must_use]
313272 pub fn intents ( & self ) -> GatewayIntents {
0 commit comments