cln_plugin: add shutdown() method to Plugin

When plugins receive a "shutdown" notification, then can call this
method which will shutdown `cln_plugin`.

Then they can await `plugin.join()` and do any remaining cleanup
there.

This helps avoid a pain-point where plugin authors need to handle
2 separate plugin shutdown mechanisms https://github.com/ElementsProject/lightning/issues/6040
This commit is contained in:
Justin Moon 2023-02-25 12:37:07 -06:00 committed by Vincenzo Palazzo
parent acf01f4c09
commit 57d21206db

View File

@ -691,6 +691,7 @@ impl<S> Plugin<S>
where
S: Send + Clone,
{
/// Wait for plugin shutdown
pub async fn join(&self) -> Result<(), Error> {
self.wait_handle
.subscribe()
@ -698,6 +699,14 @@ where
.await
.context("error waiting for shutdown")
}
/// Request plugin shutdown
pub fn shutdown(&self) -> Result<(), Error> {
self.wait_handle
.send(())
.context("error waiting for shutdown")?;
Ok(())
}
}
#[cfg(test)]