thread_state.rs

Overview

This file manages the state and lifecycle of external messages within a particular thread context. It encapsulates the queuing, caching, processing, and feedback mechanisms for external messages that interact with the system. The core component, ExternalMessagesThreadState, maintains an internal queue of external messages, enforces cache size limits, supports message insertion and removal, and reports metrics related to message handling.

The file leverages thread-safe structures (Arc, Mutex) to allow concurrent access and modification of the message queue. It also integrates with feedback mechanisms to handle cases where the queue overflows, sending notifications about discarded messages.


Main Structures and Their Roles

ExternalMessagesThreadStateConfig

A builder-configured structure used to initialize an ExternalMessagesThreadState instance. It provides typed and validated construction of the thread state with necessary dependencies and configuration parameters.

ExternalMessagesThreadState

Represents the runtime state of external messages for a given thread. It encapsulates:


Methods and Functions

Builder Pattern

push_external_messages

erase_processed

get_remaining_external_messages


Important Implementation Details and Algorithms


Interactions with Other Components


Visual Diagram: Class Structure of ExternalMessagesThreadState

classDiagram
class ExternalMessagesThreadState {
-queue: Arc<Mutex<ExternalMessagesQueue>>
-report_metrics: Option<BlockProductionMetrics>
-thread_id: ThreadIdentifier
-cache_size: usize
-feedback_sender: InstrumentedSender<ExtMsgFeedbackList>
+builder()
+push_external_messages()
+erase_processed()
+get_remaining_external_messages()
}
class ExternalMessagesThreadStateConfig {
-report_metrics: Option<BlockProductionMetrics>
-thread_id: ThreadIdentifier
-cache_size: usize
-feedback_sender: InstrumentedSender<ExtMsgFeedbackList>
+build()
}
ExternalMessagesThreadStateConfig --> ExternalMessagesThreadState : builds >

This diagram displays the two main structs: the configuration builder (ExternalMessagesThreadStateConfig) and the runtime state (ExternalMessagesThreadState), with their primary fields and public methods, illustrating the builder relationship.