/
mod.rs
57 lines (49 loc) · 1.86 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
mod data;
mod handle;
mod identifier;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::Weak;
use tokio_util::sync::CancellationToken;
use crate::errors::PartialShutdownError;
use crate::runner::SubsystemRunner;
use crate::shutdown_token::ShutdownToken;
use crate::utils::ShutdownGuard;
use crate::ErrTypeTraits;
use self::identifier::SubsystemIdentifier;
/// The data stored per subsystem, like name or nested subsystems
pub struct SubsystemData<ErrType: ErrTypeTraits = crate::BoxedError> {
name: String,
subsystems: Mutex<Option<Vec<SubsystemDescriptor<ErrType>>>>,
shutdown_subsystems: tokio::sync::Mutex<Vec<SubsystemDescriptor<ErrType>>>,
local_shutdown_token: ShutdownToken,
global_shutdown_token: ShutdownToken,
cancellation_token: CancellationToken,
shutdown_guard: Weak<ShutdownGuard>,
}
/// The handle given to each subsystem through which the subsystem can interact with this crate.
pub struct SubsystemHandle<ErrType: ErrTypeTraits = crate::BoxedError> {
data: Arc<SubsystemData<ErrType>>,
}
// Implement `Clone` manually because the compiler cannot derive `Clone
// from Generics that don't implement `Clone`.
// (https://stackoverflow.com/questions/72150623/)
impl<ErrType: ErrTypeTraits> Clone for SubsystemHandle<ErrType> {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
}
}
}
/// A running subsystem. Can be used to stop the subsystem or get its return value.
struct SubsystemDescriptor<ErrType: ErrTypeTraits = crate::BoxedError> {
id: SubsystemIdentifier,
data: Arc<SubsystemData<ErrType>>,
subsystem_runner: SubsystemRunner<ErrType>,
}
/// A nested subsystem. Can be used to perform a partial shutdown.
///
/// For more information, see [`SubsystemHandle::start()`] and [`SubsystemHandle::perform_partial_shutdown()`].
pub struct NestedSubsystem {
id: SubsystemIdentifier,
}