1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! This mod defines and manipulates the ratio of multiple messages to handle.
//!
//! The ratio should be adjust so that every channel won't be starved.
/// Balance the ratio of different kind of message to handle.
pub(crate) struct Balancer {
total: u64,
/// The number of RaftMsg to handle in each round.
raft_msg: u64,
}
impl Balancer {
pub(crate) fn new(total: u64) -> Self {
Self {
total,
// RaftMsg is the input entry.
// We should consume as many as internal messages as possible.
raft_msg: total / 10,
}
}
pub(crate) fn raft_msg(&self) -> u64 {
self.raft_msg
}
pub(crate) fn notification(&self) -> u64 {
self.total - self.raft_msg
}
pub(crate) fn increase_notification(&mut self) {
self.raft_msg = self.raft_msg * 15 / 16;
if self.raft_msg == 0 {
self.raft_msg = 1;
}
}
pub(crate) fn increase_raft_msg(&mut self) {
self.raft_msg = self.raft_msg * 17 / 16;
// Always leave some budget for other channels
if self.raft_msg > self.total / 2 {
self.raft_msg = self.total / 2;
}
}
}