Overview
The Node struct represents a TAPLE node instance. A node must be instantiated using the build method, which requires a set of configuration parameters in order to be properly initialized.
Node Struct
The Node structure is defined with the following signature:
pub struct Node<M: DatabaseManager<C>, C: DatabaseCollection> {
notification_rx: mpsc::Receiver<Notification>,
token: CancellationToken,
_m: PhantomData<M>,
_c: PhantomData<C>,
}
The struct is generic over:
M: A database manager implementing DatabaseManager<C>
C: A database collection implementing DatabaseCollection
Methods
build
Creates and initializes a TAPLE node.
pub fn build(settings: Settings, database: M) -> Result<(Self, Api), Error>
Configuration parameters for the TAPLE node. See Settings for details.
Database manager instance implementing DatabaseManager<C> trait.
The initialized TAPLE node instance.
API interface for interacting with the node. See Api for details.
Returns an error if it has not been possible to generate the necessary data for initialization, mainly due to problems in the initial configuration.
Usage Example
use taple_core::{Node, Settings};
use taple_core::database::MemoryManager;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create settings
let settings = Settings::default();
// Create database
let database = MemoryManager::new();
// Build the node
let (node, api) = Node::build(settings, database)?;
// Use the API to interact with the node
let subjects = api.get_subjects("".to_string(), None, None).await?;
Ok(())
}
This method panics if it has not been possible to generate the network layer.
recv_notification
Receives a single notification from the node.
pub async fn recv_notification(&mut self) -> Option<Notification>
Returns Some(Notification) if a notification is available, or None if the notification channel is closed.
All notifications must be consumed. If the notification buffer is full, the node will be blocked until there is space in the buffer.
Usage Example
while let Some(notification) = node.recv_notification().await {
println!("Received notification: {:?}", notification);
}
handle_notifications
Processes all notifications from the node using a provided handler function.
pub async fn handle_notifications<H>(mut self, handler: H)
where
H: Fn(Notification),
Function that will be called for each notification received.
This function blocks the task where it is invoked until the shutdown signal is produced.
Usage Example
node.handle_notifications(|notification| {
match notification {
Notification::StateChange { .. } => {
println!("State changed!");
},
_ => {}
}
}).await;
drop_notifications
Discards all notifications from the node.
pub async fn drop_notifications(self)
This method consumes all notifications without processing them, preventing the notification buffer from becoming full.
Usage Example
// Discard all notifications
node.drop_notifications().await;
bind_with_shutdown
Binds the node with a shutdown signal.
pub fn bind_with_shutdown(&self, signal: impl Future<Output = ()> + Send + 'static)
signal
Future<Output = ()>
required
A future that completes when shutdown should be triggered.
When the signal completes, the server will start the graceful shutdown process. The node can be bound to multiple signals.
Usage Example
use tokio::signal;
// Bind to CTRL+C signal
node.bind_with_shutdown(async {
signal::ctrl_c().await.expect("Failed to listen for ctrl-c");
});
shutdown_gracefully
Shutdown the node gracefully.
pub async fn shutdown_gracefully(self)
This function triggers the shutdown signal and waits until the node is safely terminated.
This function can only be used if handle_notifications or drop_notifications has not been used to process the notifications.
Usage Example
// Perform graceful shutdown
node.shutdown_gracefully().await;
Source Reference
For more details, see the source code at core/src/node.rs:50-440.