-
Notifications
You must be signed in to change notification settings - Fork 119
/
ElectionTimeout.cs
55 lines (48 loc) · 1.86 KB
/
ElectionTimeout.cs
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
47
48
49
50
51
52
53
54
55
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace DotNext.Net.Cluster.Consensus.Raft;
/// <summary>
/// Represents leader election timeout.
/// </summary>
[StructLayout(LayoutKind.Auto)]
public readonly struct ElectionTimeout
{
private readonly int lowerValue, upperValue;
/// <summary>
/// Gets recommended election timeout.
/// </summary>
public static ElectionTimeout Recommended => new() { LowerValue = 150, UpperValue = 300 };
/// <summary>
/// Generates random election timeout.
/// </summary>
/// <param name="random">The source of random values.</param>
/// <returns>The randomized election timeout.</returns>
public int RandomTimeout(Random random) => random.Next(LowerValue, UpperValue + 1);
/// <summary>
/// Gets lower possible value of leader election timeout, in milliseconds.
/// </summary>
required public int LowerValue
{
get => lowerValue;
init => lowerValue = value > 0 ? value : throw new ArgumentOutOfRangeException(nameof(value));
}
/// <summary>
/// Gets upper possible value of leader election timeout, in milliseconds.
/// </summary>
required public int UpperValue
{
get => upperValue;
init => upperValue = value is > 0 and < int.MaxValue ? value : throw new ArgumentOutOfRangeException(nameof(value));
}
/// <summary>
/// Deconstructs the election timeout.
/// </summary>
/// <param name="lowerValue">The lower possible value of leader election timeout, in milliseconds.</param>
/// <param name="upperValue">The upper possible value of leader election timeout, in milliseconds.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public void Deconstruct(out int lowerValue, out int upperValue)
{
lowerValue = this.lowerValue;
upperValue = this.upperValue;
}
}