diff --git a/src/manager.rs b/src/manager.rs index d758e2bad..edd6155d3 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -26,6 +26,7 @@ use std::sync::Arc; use tokio::sync::Mutex; #[cfg(feature = "tokio-02-marker")] use tokio_compat::sync::Mutex; +use tracing::debug; #[cfg(feature = "twilight")] use twilight_gateway::Cluster; #[cfg(feature = "twilight")] @@ -390,15 +391,30 @@ impl Songbird { #[async_trait] impl VoiceGatewayManager for Songbird { async fn initialise(&self, shard_count: u64, user_id: SerenityUser) { + debug!( + "Initialising Songbird for Serenity: ID {:?}, {} Shards", + user_id, shard_count + ); self.initialise_client_data(shard_count, user_id); + debug!("Songbird ({:?}) Initialised!", user_id); } async fn register_shard(&self, shard_id: u64, sender: Sender) { + debug!( + "Registering Serenity shard handle {} with Songbird", + shard_id + ); self.sharder.register_shard_handle(shard_id, sender); + debug!("Registered shard handle {}.", shard_id); } async fn deregister_shard(&self, shard_id: u64) { + debug!( + "Deregistering Serenity shard handle {} with Songbird", + shard_id + ); self.sharder.deregister_shard_handle(shard_id); + debug!("Deregistered shard handle {}.", shard_id); } async fn server_update(&self, guild_id: SerenityGuild, endpoint: &Option, token: &str) { diff --git a/src/shards.rs b/src/shards.rs index 7577b8fbc..03fa35268 100644 --- a/src/shards.rs +++ b/src/shards.rs @@ -10,7 +10,7 @@ use serde_json::Value; use serenity::gateway::InterMessage; #[cfg(feature = "serenity")] use std::{collections::HashMap, result::Result as StdResult, sync::Arc}; -use tracing::error; +use tracing::{debug, error}; #[cfg(feature = "twilight")] use twilight_gateway::{Cluster, Shard as TwilightShard}; @@ -134,25 +134,44 @@ pub struct SerenityShardHandle { #[cfg(feature = "serenity")] impl SerenityShardHandle { fn register(&self, sender: Sender) { + debug!("Adding shard handle send channel..."); + let mut sender_lock = self.sender.write(); *sender_lock = Some(sender); + debug!("Added shard handle send channel."); + let sender_lock = RwLockWriteGuard::downgrade(sender_lock); let mut messages_lock = self.queue.lock(); + debug!("Clearing queued messages..."); + if let Some(sender) = &*sender_lock { + let mut i = 0; for msg in messages_lock.drain(..) { if let Err(e) = sender.unbounded_send(msg) { error!("Error while clearing gateway message queue: {:?}", e); break; } + + i += 1; + } + + if i > 0 { + debug!("{} buffered messages sent to Serenity.", i); } } + + debug!("Cleared queued messages."); } fn deregister(&self) { + debug!("Removing shard handle send channel..."); + let mut sender_lock = self.sender.write(); *sender_lock = None; + + debug!("Removed shard handle send channel."); } fn send(&self, message: InterMessage) -> StdResult<(), TrySendError> { @@ -160,8 +179,10 @@ impl SerenityShardHandle { if let Some(sender) = &*sender_lock { sender.unbounded_send(message) } else { + debug!("Serenity shard temporarily disconnected: buffering message..."); let mut messages_lock = self.queue.lock(); messages_lock.push(message); + debug!("Buffered message."); Ok(()) } }