Crate instance_chart
source · [−]Expand description
Provides data about other instances on the same machine/network
This crate provides a lightweight alternative to mDNS
. It discovers other instances on the
same network or (optionally) machine. You provide an Id
and some data
you want to share.
Usually this is a port your service uses. This gives you a live updating chart
of all the discovered Ids
-data
pairs and the corrosponding ip adresses.
The chart can contain instances that are now down. It can not be used to check if a service is up.
Usage
Add a dependency on instance-chart
in Cargo.toml
:
instance_chart = "0.1"
Now add the following snippet somewhere in your codebase. Discovery will stop when you drop the maintain future.
use std::error::Error;
use instance_chart::{discovery, ChartBuilder};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let chart = ChartBuilder::new()
.with_id(1)
.with_service_port(8042)
.finish()?;
let maintain = discovery::maintain(chart.clone());
let _ = tokio::spawn(maintain); // maintain task will run forever
Ok(())
}
Modules
Structs
The chart keeping track of the discoverd nodes. That a node appears in the chart is no guarentee that it is reachable at this moment.
Construct a Chart using a builder-like pattern. You must always set an id
. You also
need to set service port
or service ports
and build with finish()
or set a custom msg
and build using custom_msg()
. See the examples of ChartBuilder::custom_msg()
or ChartBuilder::finish().
Wait for notifications of new discoveries, buffering up to 16 discoveries, created using
Chart::notify()
.