This PR implements a custom scheduler for audio threads, which reduces thread use and (often) memory consumption. To save threads and memory (e.g., packet buffer allocations), Songbird parks Mixer tasks which do not have any live Tracks. These are now all co-located on a single async 'Idle' task. This task is responsible for managing UDP keepalive messages for each task, maintaining event state, and executing any Mixer task messages. Whenever any message arrives which adds a `Track`, the mixer task is moved to a live thread. The Idle task inspects task counts and execution time on each thread, choosing the first live thread with room, and creating a new one if needed. Each live thread is responsible for running as many live mixers as it can in a single tick every 20ms: this currently defaults to 16 mixers per thread, but is user-configurable. A live thread also stores RTP packet blocks to be written into by each sub-task. Each live thread has a conservative limit of 18ms that it will aim to stay under: if all work takes longer than this, it will offload the task with the highest mixing cost once per tick onto another (possibly new) live worker thread.
152 lines
5.0 KiB
Rust
152 lines
5.0 KiB
Rust
//! Driver and gateway error handling.
|
|
|
|
#[cfg(feature = "serenity")]
|
|
use futures::channel::mpsc::TrySendError;
|
|
#[cfg(not(feature = "simd-json"))]
|
|
pub use serde_json::Error as JsonError;
|
|
#[cfg(feature = "serenity")]
|
|
use serenity::gateway::ShardRunnerMessage;
|
|
#[cfg(feature = "simd-json")]
|
|
pub use simd_json::Error as JsonError;
|
|
#[cfg(feature = "gateway")]
|
|
use std::{error::Error, fmt};
|
|
#[cfg(feature = "twilight")]
|
|
use twilight_gateway::error::SendError;
|
|
|
|
#[cfg(feature = "gateway")]
|
|
#[derive(Debug)]
|
|
#[non_exhaustive]
|
|
/// Error returned when a manager or call handler is
|
|
/// unable to send messages over Discord's gateway.
|
|
pub enum JoinError {
|
|
/// Request to join was dropped, cancelled, or replaced.
|
|
Dropped,
|
|
/// No available gateway connection was provided to send
|
|
/// voice state update messages.
|
|
NoSender,
|
|
/// Tried to leave a [`Call`] which was not found.
|
|
///
|
|
/// [`Call`]: crate::Call
|
|
NoCall,
|
|
/// Connection details were not received from Discord in the
|
|
/// time given in [the `Call`'s configuration].
|
|
///
|
|
/// This can occur if a message is lost by the Discord client
|
|
/// between restarts, or if Discord's gateway believes that
|
|
/// this bot is still in the channel it attempts to join.
|
|
///
|
|
/// *Users should `leave` the server on the gateway before
|
|
/// re-attempting connection.*
|
|
///
|
|
/// [the `Call`'s configuration]: crate::Config
|
|
TimedOut,
|
|
#[cfg(feature = "driver")]
|
|
/// The driver failed to establish a voice connection.
|
|
///
|
|
/// *Users should `leave` the server on the gateway before
|
|
/// re-attempting connection.*
|
|
Driver(ConnectionError),
|
|
#[cfg(feature = "serenity")]
|
|
/// Serenity-specific WebSocket send error.
|
|
Serenity(Box<TrySendError<ShardRunnerMessage>>),
|
|
#[cfg(feature = "twilight")]
|
|
/// Twilight-specific WebSocket send error when a message fails to send over websocket.
|
|
Twilight(SendError),
|
|
}
|
|
|
|
#[cfg(feature = "gateway")]
|
|
impl JoinError {
|
|
/// Indicates whether this failure may have left (or been
|
|
/// caused by) Discord's gateway state being in an
|
|
/// inconsistent state.
|
|
///
|
|
/// Failure to `leave` before rejoining may cause further
|
|
/// timeouts.
|
|
pub fn should_leave_server(&self) -> bool {
|
|
matches!(self, JoinError::TimedOut)
|
|
}
|
|
|
|
#[cfg(feature = "driver")]
|
|
/// Indicates whether this failure can be reattempted via
|
|
/// [`Driver::connect`] with retreived connection info.
|
|
///
|
|
/// Failure to `leave` before rejoining may cause further
|
|
/// timeouts.
|
|
///
|
|
/// [`Driver::connect`]: crate::driver::Driver
|
|
pub fn should_reconnect_driver(&self) -> bool {
|
|
matches!(self, JoinError::Driver(_))
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "gateway")]
|
|
impl fmt::Display for JoinError {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "failed to join voice channel: ")?;
|
|
match self {
|
|
JoinError::Dropped => write!(f, "request was cancelled/dropped"),
|
|
JoinError::NoSender => write!(f, "no gateway destination"),
|
|
JoinError::NoCall => write!(f, "tried to leave a non-existent call"),
|
|
JoinError::TimedOut => write!(f, "gateway response from Discord timed out"),
|
|
#[cfg(feature = "driver")]
|
|
JoinError::Driver(_) => write!(f, "establishing connection failed"),
|
|
#[cfg(feature = "serenity")]
|
|
JoinError::Serenity(e) => e.fmt(f),
|
|
#[cfg(feature = "twilight")]
|
|
JoinError::Twilight(e) => e.fmt(f),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "gateway")]
|
|
impl Error for JoinError {
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
match self {
|
|
JoinError::Dropped => None,
|
|
JoinError::NoSender => None,
|
|
JoinError::NoCall => None,
|
|
JoinError::TimedOut => None,
|
|
#[cfg(feature = "driver")]
|
|
JoinError::Driver(e) => Some(e),
|
|
#[cfg(feature = "serenity")]
|
|
JoinError::Serenity(e) => e.source(),
|
|
#[cfg(feature = "twilight")]
|
|
JoinError::Twilight(e) => e.source(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(all(feature = "serenity", feature = "gateway"))]
|
|
impl From<Box<TrySendError<ShardRunnerMessage>>> for JoinError {
|
|
fn from(e: Box<TrySendError<ShardRunnerMessage>>) -> Self {
|
|
JoinError::Serenity(e)
|
|
}
|
|
}
|
|
|
|
#[cfg(all(feature = "twilight", feature = "gateway"))]
|
|
impl From<SendError> for JoinError {
|
|
fn from(e: SendError) -> Self {
|
|
JoinError::Twilight(e)
|
|
}
|
|
}
|
|
|
|
#[cfg(all(feature = "driver", feature = "gateway"))]
|
|
impl From<ConnectionError> for JoinError {
|
|
fn from(e: ConnectionError) -> Self {
|
|
JoinError::Driver(e)
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "gateway")]
|
|
/// Convenience type for Discord gateway error handling.
|
|
pub type JoinResult<T> = Result<T, JoinError>;
|
|
|
|
#[cfg(feature = "driver")]
|
|
pub use crate::{
|
|
driver::{
|
|
connection::error::{Error as ConnectionError, Result as ConnectionResult},
|
|
SchedulerError,
|
|
},
|
|
tracks::{ControlError, PlayError, TrackResult},
|
|
};
|