service.rs

Overview

This file implements the RoutingService, which acts as a central router and dispatcher for network messages and external messages within the system. It manages the lifecycle of message-handling threads, routes incoming messages to appropriate handlers, and handles feedback for external messages. The service ensures efficient message forwarding, thread management, and feedback processing using multiple threads and channels.

The core responsibilities include:

Key Types and Constants

Enumerations

Command

Defines commands sent to the routing service's control loop.

Variants:

Structs

RoutingService

Main service struct managing routing and feedback channels:

Fields

Methods


Internal Functions (Private)


Implementation Details and Algorithms


Interaction with Other Components


Visual Diagram

classDiagram
class RoutingService {
+cmd_sender: InstrumentedSender<Command>
+feedback_sender: InstrumentedSender<ExtMsgFeedbackList>
+new()
+start()
+join_thread()
-create_node_thread()
-route()
-inner_main_loop()
-inner_network_messages_forwarding_loop()
-inner_external_messages_forwarding_loop()
-inner_feedback_loop()
}
class Dispatcher {
+add_route()
+dispatch()
+has_route()
}
class Node {
+execute()
}
RoutingService --> Dispatcher : uses
RoutingService --> Node : manages nodes
RoutingService --> "InstrumentedSender<Command>" : sends commands
RoutingService --> "InstrumentedSender<ExtMsgFeedbackList>" : sends feedback

Usage Example

// Setup inbound receivers (e.g., from network stack)
let inbound_network_receiver = ...; // InstrumentedReceiver<IncomingMessage>
let inbound_ext_messages_receiver = ...; // InstrumentedReceiver<FeedbackMessage>
let metrics = Some(BlockProductionMetrics::new());
let net_metrics = Some(NetMetrics::new());

// Create routing service and forwarding threads
let (routing_service, cmd_receiver, network_thread, ext_thread) = RoutingService::new(
    inbound_network_receiver,
    inbound_ext_messages_receiver,
    metrics.clone(),
    net_metrics.clone(),
);

// Define node factory function
let node_factory = |parent_block: Option<BlockIdentifier>,
                    thread_id: &ThreadIdentifier,
                    incoming_rx,
                    authority_rx,
                    incoming_tx,
                    authority_tx,
                    feedback_tx,
                    ext_rx| {
    // Create and return node instance
    Node::new(...)
};

// Start main routing service loop
let (service, main_thread) = RoutingService::start((routing_service, cmd_receiver), metrics, node_factory);

// Start handling threads and messages...

Trait Implementation

The RoutingService implements the Subscriber trait from threads_tracking_service:

This integration allows the routing service to react to thread lifecycle events within the threading tracking infrastructure.


Please refer to the topics related to Network Message Routing, Thread Management, External Message Feedback, and Dispatcher Pattern for further context on how this service fits within the overall system.