Gateway: Add debug logging around shard handling

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...
This commit is contained in:
Kyle Simpson
2021-05-16 23:13:03 +01:00
parent 6d66b499e5
commit 0f54d18455
2 changed files with 38 additions and 1 deletions

View File

@@ -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<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) {

View File

@@ -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<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>> {
@@ -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(())
}
}