pub struct ChartBuilder<const N: usize, IdSet, PortSet, PortsSet>where
    IdSet: ToAssign,
    PortSet: ToAssign,
    PortsSet: ToAssign,{ /* private fields */ }
Expand description

Construct a Chart using a builder-like pattern. You must always set an id. You also need to set service port or service ports. Now you can build with finish or using custom_msg. The latter allowes you to set a custom message to share with other instances when they discover you.

Implementations§

source§

impl<const N: usize> ChartBuilder<N, No, No, No>

source

pub fn new() -> ChartBuilder<N, No, No, No>

Create a new chart builder

source§

impl<const N: usize, IdSet, PortSet, PortsSet> ChartBuilder<N, IdSet, PortSet, PortsSet>where IdSet: ToAssign, PortSet: ToAssign, PortsSet: ToAssign,

source

pub fn with_id(self, id: Id) -> ChartBuilder<N, Yes, PortSet, PortsSet>

Set the Id for this node, the Id is the key for this node in the chart

Note

Always needed, you can not build without an Id set. The Id must be unique

source

pub fn with_random_id(self) -> ChartBuilder<N, Yes, PortSet, PortsSet>

Use a true random number from a reliable source of randomness as an Id.

Note

I recommend setting the Id in a deterministic way if possible, it makes debugging a lot easier. Theoretically using this method can fail if multiple instances get the same random number, the chance of this is unrealistically small.

It is extreemly unlikely though possible that this fails. This happens if the systems source of random is configured incorrectly.

source

pub fn with_service_port(self, port: u16) -> ChartBuilder<N, IdSet, Yes, No>

Set a port for use by your application. This will appear to the other nodes in the Chart.

Note

You need to use this or with_service_ports if building with finish(). Cant be used when bulding with custom_msg.

source

pub fn with_service_ports( self, ports: [u16; N] ) -> ChartBuilder<N, IdSet, No, Yes>

Set mutiple ports for use by your application. This will appear to the other nodes in the Chart.

Note

You need to use this or with_service_port if building with finish(). Cant be used when bulding with custom_msg.

source

pub fn with_header( self, header: u64 ) -> ChartBuilder<N, IdSet, PortSet, PortsSet>

set a custom header number. The header is used to identify your application’s chart from others multicast traffic when deployed your should set this to a random number.

source

pub fn with_discovery_port( self, port: u16 ) -> ChartBuilder<N, IdSet, PortSet, PortsSet>

set custom port for discovery. With [local discovery] enabled this port needs to be free and unused on all nodes it is not free the multicast traffic caused by this library might corrupt network data of other applications. The default port is 8080.

Warning

Not all ports seem to pass multicast traffic, you might need to experiment a bit.

source

pub fn with_rampdown( self, min: Duration, max: Duration, rampdown: Duration ) -> ChartBuilder<N, IdSet, PortSet, PortsSet>

set duration between discovery broadcasts, decreases linearly from max to min over rampdown period.

Panics

panics if min is larger then max

source

pub fn local_discovery( self, is_enabled: bool ) -> ChartBuilder<N, IdSet, PortSet, PortsSet>

set whether discovery is enabled within the same host. Defaults to false.

Warning

When this is enabled you might not be warned if the discovery port is in use by another application. The other application will recieve network traffic from this crate. This might lead to corruption in the application if it can not handle this. ChartBuilder will still fail if the discovery port is already bound to a multicast adress without SO_REUSEADDR set.

source§

impl ChartBuilder<1, Yes, No, No>

source

pub fn custom_msg<Msg>(self, msg: Msg) -> Result<Chart<1, Msg>, Error>where Msg: Debug + Serialize + Clone,

build a chart with a custom msg instead of a service port. The message can be any struct that implements Debug, Clone, serde::Serialize and serde::Deserialize

Errors

This errors if the discovery port could not be opened. see: Self::with_discovery_port.

Example
use instance_chart::{discovery, ChartBuilder};
use serde::{Serialize, Deserialize};
use std::error::Error;
use std::time::Duration;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Msg(u32);

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
  let msg = Msg(0);
  let chart = ChartBuilder::new()
      .with_id(1)
      .with_discovery_port(8888)
      .with_header(17249479) // optional
      .with_rampdown( // optional
          Duration::from_millis(10),
          Duration::from_secs(10),
          Duration::from_secs(60))
      .custom_msg(msg)?;
  let maintain = discovery::maintain(chart.clone());
  let _ = tokio::spawn(maintain); // maintain task will run forever
  Ok(())
}
source§

impl ChartBuilder<1, Yes, Yes, No>

source

pub fn finish(self) -> Result<Chart<1, u16>, Error>

build a chart that has a single service ports set

Errors

This errors if the discovery port could not be opened. see: Self::with_discovery_port.

Example
use std::error::Error;
use instance_chart::{discovery, ChartBuilder};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
  let chart = ChartBuilder::new()
      .with_id(1)
      .with_service_port(8042)
      .with_discovery_port(8888)
      .with_header(17249479) // optional
      .with_rampdown( // optional
          Duration::from_millis(10),
          Duration::from_secs(10),
          Duration::from_secs(60))
      .finish()?;
  let maintain = discovery::maintain(chart.clone());
  let _ = tokio::spawn(maintain); // maintain task will run forever
  Ok(())
}
source§

impl<const N: usize> ChartBuilder<N, Yes, No, Yes>

source

pub fn finish(self) -> Result<Chart<N, u16>, Error>

build a chart that has a multiple service ports set

Errors

This errors if the discovery port could not be opened. see: Self::with_discovery_port.

Example
use std::error::Error;
use instance_chart::{discovery, ChartBuilder};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
  let chart = ChartBuilder::new()
      .with_id(1)
      .with_service_ports([8042,9042])
      .with_discovery_port(8888)
      .with_header(17249479) // optional
      .with_rampdown( // optional
          Duration::from_millis(10),
          Duration::from_secs(10),
          Duration::from_secs(60))
      .finish()?;
  let maintain = discovery::maintain(chart.clone());
  let _ = tokio::spawn(maintain); // maintain task will run forever
  Ok(())
}

Auto Trait Implementations§

§

impl<const N: usize, IdSet, PortSet, PortsSet> RefUnwindSafe for ChartBuilder<N, IdSet, PortSet, PortsSet>where IdSet: RefUnwindSafe, PortSet: RefUnwindSafe, PortsSet: RefUnwindSafe,

§

impl<const N: usize, IdSet, PortSet, PortsSet> Send for ChartBuilder<N, IdSet, PortSet, PortsSet>where IdSet: Send, PortSet: Send, PortsSet: Send,

§

impl<const N: usize, IdSet, PortSet, PortsSet> Sync for ChartBuilder<N, IdSet, PortSet, PortsSet>where IdSet: Sync, PortSet: Sync, PortsSet: Sync,

§

impl<const N: usize, IdSet, PortSet, PortsSet> Unpin for ChartBuilder<N, IdSet, PortSet, PortsSet>where IdSet: Unpin, PortSet: Unpin, PortsSet: Unpin,

§

impl<const N: usize, IdSet, PortSet, PortsSet> UnwindSafe for ChartBuilder<N, IdSet, PortSet, PortsSet>where IdSet: UnwindSafe, PortSet: UnwindSafe, PortsSet: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more