Skip to content

Commit

Permalink
Gateway: Add debug logging around shard handling
Browse files Browse the repository at this point in the history
Adds some additional logging around some critical sections, rarely hit (i.e., during shard reconnections) in pursuit of issue #69. It's strongly suspected to lie here, at any rate...
  • Loading branch information
FelixMcFelix committed May 16, 2021
1 parent fc94ddb commit b3caf05
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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<InterMessage>) {
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<String>, token: &str) {
Expand Down
23 changes: 22 additions & 1 deletion src/shards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -134,34 +134,55 @@ pub struct SerenityShardHandle {
#[cfg(feature = "serenity")]
impl SerenityShardHandle {
fn register(&self, sender: Sender<InterMessage>) {
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<InterMessage>> {
let sender_lock = self.sender.read();
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(())
}
}
Expand Down

0 comments on commit b3caf05

Please sign in to comment.