Driver: Automate (re)connection logic (#81)
This PR adds several enhancements to Driver connection logic: * Driver (re)connection attempts now have a default timeout of around 10s. * The driver will now attempt to retry full connection attempts using a user-provided strategy: currently, this defaults to 5 attempts under an exponential backoff strategy. * The driver will now fire `DriverDisconnect` events at the end of any session -- this unifies (re)connection failure events with session expiry as seen in #76, which should provide users with enough detail to know *which* voice channel to reconnect to. Users still need to be careful to read the session/channel IDs to ensure that they aren't overwriting another join. This has been tested using `cargo make ready`, and by setting low timeouts to force failures in the voice receive example (with some additional error handlers). Closes #68.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#[cfg(feature = "driver-core")]
|
||||
use super::driver::{CryptoMode, DecodeMode};
|
||||
use super::driver::{retry::Retry, CryptoMode, DecodeMode};
|
||||
|
||||
#[cfg(feature = "gateway-core")]
|
||||
use std::time::Duration;
|
||||
|
||||
/// Configuration for drivers and calls.
|
||||
@@ -61,6 +60,20 @@ pub struct Config {
|
||||
/// Changes to this field in a running driver will only ever increase
|
||||
/// the capacity of the track store.
|
||||
pub preallocated_tracks: usize,
|
||||
#[cfg(feature = "driver-core")]
|
||||
/// Connection retry logic for the [`Driver`].
|
||||
///
|
||||
/// This controls how many times the [`Driver`] should retry any connections,
|
||||
/// as well as how long to wait between attempts.
|
||||
///
|
||||
/// [`Driver`]: crate::driver::Driver
|
||||
pub driver_retry: Retry,
|
||||
#[cfg(feature = "driver-core")]
|
||||
/// Configures the maximum amount of time to wait for an attempted voice
|
||||
/// connection to Discord.
|
||||
///
|
||||
/// Defaults to 10 seconds. If set to `None`, connections will never time out.
|
||||
pub driver_timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
@@ -74,6 +87,10 @@ impl Default for Config {
|
||||
gateway_timeout: Some(Duration::from_secs(10)),
|
||||
#[cfg(feature = "driver-core")]
|
||||
preallocated_tracks: 1,
|
||||
#[cfg(feature = "driver-core")]
|
||||
driver_retry: Default::default(),
|
||||
#[cfg(feature = "driver-core")]
|
||||
driver_timeout: Some(Duration::from_secs(10)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,6 +115,18 @@ impl Config {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets this `Config`'s timeout for establishing a voice connection.
|
||||
pub fn driver_timeout(mut self, driver_timeout: Option<Duration>) -> Self {
|
||||
self.driver_timeout = driver_timeout;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets this `Config`'s voice connection retry configuration.
|
||||
pub fn driver_retry(mut self, driver_retry: Retry) -> Self {
|
||||
self.driver_retry = driver_retry;
|
||||
self
|
||||
}
|
||||
|
||||
/// This is used to prevent changes which would invalidate the current session.
|
||||
pub(crate) fn make_safe(&mut self, previous: &Config, connected: bool) {
|
||||
if connected {
|
||||
|
||||
Reference in New Issue
Block a user