Avoid spawning a disposal thread per driver (#151)
Adds a new field to Config, disposer, an Option<Sender<DisposalMessage>> responsible for dropping the DisposalMessage on a separate thread. If this is not set, and the Config is passed into manager::Songbird, a thread is spawned for this purpose (which previously was spawned per driver). If this is not set, and the Config is passed directly into Driver or Call, a thread is spawned locally, which is the current behavior as there is no where to store the Sender. This disposer is then used in Driver as previously, to run possibly blocking destructors (which should only block the disposal thread). I cannot see this disposal thread getting overloaded, but if it is the DisposalMessages will simply be queued in the flume channel until it can be dropped. Co-authored-by: Kyle Simpson <kyleandrew.simpson@gmail.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
use crate::driver::DecodeMode;
|
||||
#[cfg(feature = "driver")]
|
||||
use crate::{
|
||||
driver::{retry::Retry, CryptoMode, MixMode},
|
||||
driver::{retry::Retry, tasks::disposal::DisposalThread, CryptoMode, MixMode},
|
||||
input::codecs::*,
|
||||
};
|
||||
|
||||
@@ -143,6 +143,16 @@ pub struct Config {
|
||||
///
|
||||
/// [`PROBE`]: static@PROBE
|
||||
pub format_registry: &'static Probe,
|
||||
#[cfg(feature = "driver")]
|
||||
/// The Sender for a channel that will run the destructor of possibly blocking values.
|
||||
///
|
||||
/// If not set, a thread will be spawned to perform this, but it is recommended to create
|
||||
/// a long running thread instead of relying on a per-driver thread.
|
||||
///
|
||||
/// Note: When using [`Songbird`] this is overwritten automatically by its disposal thread.
|
||||
///
|
||||
/// [`Songbird`]: crate::Songbird
|
||||
pub disposer: Option<DisposalThread>,
|
||||
|
||||
// Test only attributes
|
||||
#[cfg(feature = "driver")]
|
||||
@@ -181,6 +191,8 @@ impl Default for Config {
|
||||
#[cfg(feature = "driver")]
|
||||
format_registry: &PROBE,
|
||||
#[cfg(feature = "driver")]
|
||||
disposer: None,
|
||||
#[cfg(feature = "driver")]
|
||||
#[cfg(test)]
|
||||
tick_style: TickStyle::Timed,
|
||||
#[cfg(feature = "driver")]
|
||||
@@ -264,6 +276,23 @@ impl Config {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets this `Config`'s channel for sending disposal messages.
|
||||
#[must_use]
|
||||
pub fn disposer(mut self, disposer: DisposalThread) -> Self {
|
||||
self.disposer = Some(disposer);
|
||||
self
|
||||
}
|
||||
|
||||
/// Ensures a global disposer has been set, initializing one if not.
|
||||
#[must_use]
|
||||
pub(crate) fn initialise_disposer(self) -> Self {
|
||||
if self.disposer.is_some() {
|
||||
self
|
||||
} else {
|
||||
self.disposer(DisposalThread::run())
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
@@ -272,6 +301,13 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "driver"))]
|
||||
impl Config {
|
||||
pub(crate) fn initialise_disposer(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
// Test only attributes
|
||||
#[cfg(all(test, feature = "driver"))]
|
||||
impl Config {
|
||||
|
||||
Reference in New Issue
Block a user