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:
Gnome!
2022-09-26 22:03:00 +01:00
committed by Kyle Simpson
parent 03b0803a1d
commit be3a4e9b24
4 changed files with 78 additions and 20 deletions

View File

@@ -49,7 +49,7 @@ pub struct Songbird {
client_data: OnceCell<ClientData>,
calls: DashMap<GuildId, Arc<Mutex<Call>>>,
sharder: Sharder,
config: PRwLock<Option<Config>>,
config: PRwLock<Config>,
}
impl Songbird {
@@ -76,7 +76,7 @@ impl Songbird {
client_data: OnceCell::new(),
calls: DashMap::new(),
sharder: Sharder::Serenity(SerenitySharder::default()),
config: Some(config).into(),
config: config.initialise_disposer().into(),
})
}
@@ -114,7 +114,7 @@ impl Songbird {
}),
calls: DashMap::new(),
sharder: Sharder::TwilightCluster(cluster),
config: Some(config).into(),
config: config.initialise_disposer().into(),
}
}
@@ -176,7 +176,7 @@ impl Songbird {
guild_id,
shard_handle,
info.user_id,
self.config.read().clone().unwrap_or_default(),
self.config.read().clone(),
);
Arc::new(Mutex::new(call))
@@ -193,7 +193,7 @@ impl Songbird {
/// Requires the `"driver"` feature.
pub fn set_config(&self, new_config: Config) {
let mut config = self.config.write();
*config = Some(new_config);
*config = new_config;
}
#[cfg(feature = "driver")]