10 releases (4 breaking)

0.5.0 Jul 22, 2024
0.4.1 Jul 22, 2024
0.3.1 Jan 24, 2024
0.3.0 Dec 28, 2023
0.1.1 Nov 17, 2023

#725 in Network programming

Download history 19/week @ 2024-06-30 47/week @ 2024-07-07 182/week @ 2024-07-14 695/week @ 2024-07-21 496/week @ 2024-07-28 413/week @ 2024-08-04 263/week @ 2024-08-11 323/week @ 2024-08-18 145/week @ 2024-08-25 132/week @ 2024-09-01 249/week @ 2024-09-08 199/week @ 2024-09-15 274/week @ 2024-09-22 359/week @ 2024-09-29 85/week @ 2024-10-06 116/week @ 2024-10-13

871 downloads per month
Used in 17 crates (14 directly)

MIT license

475KB
10K SLoC

Network is split into 2 parts

  • Network and routing tables
  • Custom behaviours

Network and routing tables

This part handles the network and routing tables. The logic of routing table is not fixed, instead it is inject into with trait RoutingTable which provide bellow function

pub trait RouterTable: Send + Sync {
    fn path_to_node(&self, dest: NodeId) -> RouteAction;
    fn path_to_key(&self, key: NodeId) -> RouteAction;
    fn path_to_service(&self, service_id: u8) -> RouteAction;
    fn path_to(&self, route: &RouteRule, service_id: u8) -> RouteAction {
        match route {
            RouteRule::Node(dest) => self.path_to_node(*dest),
            RouteRule::Closest(key) => self.path_to_key(*key),
            RouteRule::Service => self.path_to_service(service_id),
        }
    }
}

By the way, network providing some based function to sending and handler message:

  • Send to other Node by NodeId
  • Send to other Node, which served by a service which is identified by service_id
  • Send to local Node

Message is sending to other node by routing table and transport

Custom behaviours

By provide a custom behaviour, we can add logic to application with modules style, each that will need to defined some parts:

  • Behaviour: defined main logic (this is heart of behaviour), which handle some main event like: new incoming-outgoing connection, connection disconnected,
  • Connection Handler: defined logic which handle separate with each connection like: connection event, transport message ...

Each part also can exchanged data with other part by using Agent, which is provide in each function handler

  • From behavior to each connections
  • From each connection back to behavior
  • From each connection to each other connection

For reference bellow is trait of behaviour

pub trait NetworkBehavior<BE, HE, MSG>
where
    MSG: Send + Sync,
{
    fn service_id(&self) -> u8;
    fn on_tick(&mut self, agent: &BehaviorAgent<HE, MSG>, ts_ms: u64, interval_ms: u64);
    fn check_incoming_connection(
        &mut self,
        node: NodeId,
        conn_id: ConnId,
    ) -> Result<(), ConnectionRejectReason>;
    fn check_outgoing_connection(
        &mut self,
        node: NodeId,
        conn_id: ConnId,
    ) -> Result<(), ConnectionRejectReason>;
    fn on_incoming_connection_connected(
        &mut self,
        agent: &BehaviorAgent<HE, MSG>,
        conn: Arc<dyn ConnectionSender<MSG>>,
    ) -> Option<Box<dyn ConnectionHandler<BE, HE, MSG>>>;
    fn on_outgoing_connection_connected(
        &mut self,
        agent: &BehaviorAgent<HE, MSG>,
        conn: Arc<dyn ConnectionSender<MSG>>,
    ) -> Option<Box<dyn ConnectionHandler<BE, HE, MSG>>>;
    fn on_incoming_connection_disconnected(
        &mut self,
        agent: &BehaviorAgent<HE, MSG>,
        conn: Arc<dyn ConnectionSender<MSG>>,
    );
    fn on_outgoing_connection_disconnected(
        &mut self,
        agent: &BehaviorAgent<HE, MSG>,
        conn: Arc<dyn ConnectionSender<MSG>>,
    );
    fn on_outgoing_connection_error(
        &mut self,
        agent: &BehaviorAgent<HE, MSG>,
        node_id: NodeId,
        conn_id: ConnId,
        err: &OutgoingConnectionError,
    );
    fn on_handler_event(
        &mut self,
        agent: &BehaviorAgent<HE, MSG>,
        node_id: NodeId,
        conn_id: ConnId,
        event: BE,
    );
    fn on_rpc(
        &mut self,
        agent: &BehaviorAgent<HE, MSG>,
        req: Req,
        res: Box<dyn RpcAnswer<Res>>,
    ) -> bool;
}

Bellow is trait of ConnectionHandler

pub trait ConnectionHandler<BE, HE, MSG>: Send + Sync {
    fn on_opened(&mut self, agent: &ConnectionAgent<BE, HE, MSG>);
    fn on_tick(&mut self, agent: &ConnectionAgent<BE, HE, MSG>, ts_ms: u64, interval_ms: u64);
    fn on_event(&mut self, agent: &ConnectionAgent<BE, HE, MSG>, event: ConnectionEvent<MSG>);
    fn on_other_handler_event(
        &mut self,
        agent: &ConnectionAgent<BE, HE, MSG>,
        from_node: NodeId,
        from_conn: ConnId,
        event: HE,
    );
    fn on_behavior_event(&mut self, agent: &ConnectionAgent<BE, HE, MSG>, event: HE);
    fn on_closed(&mut self, agent: &ConnectionAgent<BE, HE, MSG>);
}

Dependencies

~9–15MB
~218K SLoC