Add Instance::stop
This commit is contained in:
parent
5d1d12522e
commit
820da6db1b
1 changed files with 20 additions and 6 deletions
|
|
@ -170,6 +170,11 @@ pub enum Event {
|
||||||
Stopped(InstanceConfig),
|
Stopped(InstanceConfig),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Request {
|
||||||
|
GetConnTx(oneshot::Sender<ConnTx>),
|
||||||
|
Stop,
|
||||||
|
}
|
||||||
|
|
||||||
/// An error that occurred inside an [`Instance`] while it was running.
|
/// An error that occurred inside an [`Instance`] while it was running.
|
||||||
enum RunError {
|
enum RunError {
|
||||||
StoppedManually,
|
StoppedManually,
|
||||||
|
|
@ -191,7 +196,7 @@ enum RunError {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
config: InstanceConfig,
|
config: InstanceConfig,
|
||||||
request_tx: mpsc::UnboundedSender<oneshot::Sender<ConnTx>>,
|
request_tx: mpsc::UnboundedSender<Request>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
|
|
@ -226,14 +231,18 @@ impl Instance {
|
||||||
|
|
||||||
pub async fn conn_tx(&self) -> Option<ConnTx> {
|
pub async fn conn_tx(&self) -> Option<ConnTx> {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
let _ = self.request_tx.send(tx);
|
let _ = self.request_tx.send(Request::GetConnTx(tx));
|
||||||
rx.await.ok()
|
rx.await.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn stop(&self) {
|
||||||
|
let _ = self.request_tx.send(Request::Stop);
|
||||||
|
}
|
||||||
|
|
||||||
async fn run<F>(
|
async fn run<F>(
|
||||||
config: InstanceConfig,
|
config: InstanceConfig,
|
||||||
on_event: F,
|
on_event: F,
|
||||||
mut request_rx: mpsc::UnboundedReceiver<oneshot::Sender<ConnTx>>,
|
mut request_rx: mpsc::UnboundedReceiver<Request>,
|
||||||
) where
|
) where
|
||||||
F: Fn(Event),
|
F: Fn(Event),
|
||||||
{
|
{
|
||||||
|
|
@ -304,7 +313,7 @@ impl Instance {
|
||||||
async fn run_once<F>(
|
async fn run_once<F>(
|
||||||
config: &InstanceConfig,
|
config: &InstanceConfig,
|
||||||
on_event: &F,
|
on_event: &F,
|
||||||
request_rx: &mut mpsc::UnboundedReceiver<oneshot::Sender<ConnTx>>,
|
request_rx: &mut mpsc::UnboundedReceiver<Request>,
|
||||||
) -> Result<(), RunError>
|
) -> Result<(), RunError>
|
||||||
where
|
where
|
||||||
F: Fn(Event),
|
F: Fn(Event),
|
||||||
|
|
@ -375,11 +384,16 @@ impl Instance {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_requests(
|
async fn handle_requests(
|
||||||
request_rx: &mut mpsc::UnboundedReceiver<oneshot::Sender<ConnTx>>,
|
request_rx: &mut mpsc::UnboundedReceiver<Request>,
|
||||||
conn_tx: &ConnTx,
|
conn_tx: &ConnTx,
|
||||||
) -> RunError {
|
) -> RunError {
|
||||||
while let Some(request) = request_rx.recv().await {
|
while let Some(request) = request_rx.recv().await {
|
||||||
let _ = request.send(conn_tx.clone());
|
match request {
|
||||||
|
Request::GetConnTx(tx) => {
|
||||||
|
let _ = tx.send(conn_tx.clone());
|
||||||
|
}
|
||||||
|
Request::Stop => return RunError::StoppedManually,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RunError::InstanceDropped
|
RunError::InstanceDropped
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue