Skip to content

Commit

Permalink
Add a close handle for closing the shards in another thread
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Jul 15, 2017
1 parent c7b8ab8 commit 9900b20
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ use ::framework::Framework;

static HANDLE_STILL: AtomicBool = ATOMIC_BOOL_INIT;

#[derive(Clone)]
pub struct CloseHandle;

impl CloseHandle {
pub fn close(self) {
HANDLE_STILL.store(false, Ordering::Relaxed);
}
}

/// The Client is the way to be able to start sending authenticated requests
/// over the REST API, as well as initializing a WebSocket connection through
/// [`Shard`]s. Refer to the [documentation on using sharding][sharding docs]
Expand Down Expand Up @@ -595,9 +604,9 @@ impl<H: EventHandler + 'static> Client<H> {
self.start_connection([range[0], range[1], total_shards], http::get_gateway()?.url)
}

/// Closes all of the shards that are running.
pub fn close(&self) {
HANDLE_STILL.store(false, Ordering::Relaxed);
/// Returns a thread-safe handle for closing shards.
pub fn close_handle(&self) -> CloseHandle {
CloseHandle
}

// Shard data layout is:
Expand Down Expand Up @@ -696,7 +705,7 @@ impl<H: EventHandler + 'static> Client<H> {

impl<H: EventHandler + 'static> Drop for Client<H> {
fn drop(&mut self) {
self.close();
self.close_handle().close();
}
}

Expand Down

0 comments on commit 9900b20

Please sign in to comment.