rmqtt_raft/
lib.rs

1#![allow(clippy::result_large_err)]
2
3use std::time::Duration;
4
5// Re-exporting necessary types and modules for external use.
6pub use crate::error::{Error, Result};
7pub use crate::message::Status;
8pub use crate::raft::{Mailbox, Raft, Store};
9pub use tikv_raft::{ReadOnlyOption, StateRole};
10
11// Importing modules for internal use.
12mod error;
13mod message;
14mod raft;
15mod raft_node;
16mod raft_server;
17mod raft_service;
18mod storage;
19mod timeout_recorder;
20
21/// Configuration options for the Raft-based system.
22#[derive(Clone)]
23pub struct Config {
24    #[cfg(feature = "reuseaddr")]
25    /// Whether to reuse local addresses. This option is enabled only if the `reuseaddr` feature is active.
26    pub reuseaddr: bool,
27
28    #[cfg(all(
29        feature = "reuseport",
30        not(any(target_os = "solaris", target_os = "illumos"))
31    ))]
32    /// Whether to reuse local ports. This option is enabled only if the `reuseport` feature is active
33    /// and the target OS is not Solaris or Illumos.
34    pub reuseport: bool,
35
36    /// The timeout duration for gRPC calls.
37    pub grpc_timeout: Duration,
38
39    /// The maximum number of concurrent gRPC calls.
40    pub grpc_concurrency_limit: usize,
41
42    /// The maximum size of gRPC messages in bytes.
43    pub grpc_message_size: usize,
44
45    /// The threshold for the gRPC circuit breaker. If the number of failed requests exceeds this threshold,
46    /// the circuit breaker will trip.
47    pub grpc_breaker_threshold: u64,
48
49    /// The interval at which the gRPC circuit breaker will retry after tripping.
50    pub grpc_breaker_retry_interval: Duration,
51
52    /// The maximum number of proposals to batch together before processing.
53    pub proposal_batch_size: usize,
54
55    /// The timeout duration for collecting proposals into a batch. If this timeout is reached,
56    /// the collected proposals will be processed regardless of the batch size.
57    pub proposal_batch_timeout: Duration,
58
59    /// The interval at which snapshots are generated.
60    pub snapshot_interval: Duration,
61
62    /// The interval at which heartbeat messages are sent to maintain leader election and cluster health.
63    pub heartbeat: Duration,
64
65    /// Configuration options for the Raft protocol.
66    pub raft_cfg: tikv_raft::Config,
67}
68
69impl Default for Config {
70    /// Provides default values for the `Config` struct.
71    fn default() -> Self {
72        Self {
73            #[cfg(feature = "reuseaddr")]
74            reuseaddr: false,
75
76            #[cfg(all(
77                feature = "reuseport",
78                not(any(target_os = "solaris", target_os = "illumos"))
79            ))]
80            reuseport: false,
81
82            grpc_timeout: Duration::from_secs(6),
83            grpc_concurrency_limit: 200,
84            grpc_message_size: 50 * 1024 * 1024, // 50 MB
85            grpc_breaker_threshold: 4,
86            grpc_breaker_retry_interval: Duration::from_millis(2500),
87            proposal_batch_size: 50,
88            proposal_batch_timeout: Duration::from_millis(200),
89            snapshot_interval: Duration::from_secs(600),
90            heartbeat: Duration::from_millis(100),
91            raft_cfg: tikv_raft::Config {
92                election_tick: 10,
93                heartbeat_tick: 5,
94                check_quorum: true,
95                pre_vote: true,
96                ..Default::default()
97            },
98        }
99    }
100}