-
Notifications
You must be signed in to change notification settings - Fork 3
/
MutualHeartbeatMonitor.h
60 lines (51 loc) · 1.69 KB
/
MutualHeartbeatMonitor.h
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
56
57
58
59
60
#pragma once
#include <chrono>
#include <ostream>
struct MutualHeartbeatMonitor
{
static
constexpr std::chrono::milliseconds period{3 * 1000};
private:
std::chrono::steady_clock::time_point selfTimestamp;
std::chrono::steady_clock::time_point peerTimestamp;
std::chrono::milliseconds peerDiff() const
{
const auto diff = std::chrono::steady_clock::now() - peerTimestamp;
const auto msDiff = std::chrono::duration_cast<std::chrono::milliseconds>(diff);
return msDiff;
}
std::chrono::milliseconds selfDiff() const
{
const auto diff = std::chrono::steady_clock::now() - selfTimestamp;
const auto msDiff = std::chrono::duration_cast<std::chrono::milliseconds>(diff);
return msDiff;
}
public:
MutualHeartbeatMonitor(
std::chrono::steady_clock::time_point tp = std::chrono::steady_clock::now()):
selfTimestamp{tp},
peerTimestamp{tp}
{}
bool peerHeartbeatExpired() const {return peerDiff() > 3 * period;}
bool shouldHeartbeat() const {return selfDiff() > period;}
void selfHeartbeat() {selfTimestamp = std::chrono::steady_clock::now();}
void peerHeartbeat() {peerTimestamp = std::chrono::steady_clock::now();}
void reset()
{
const auto now = std::chrono::steady_clock::now();
selfTimestamp = now;
peerTimestamp = now;
}
void stats(std::ostream &os) const
{
os
<< "selfDiff " << selfDiff().count() << "ms"
<< " peerDiff " << peerDiff().count() << "ms";
}
friend
std::ostream &operator<< (std::ostream &os, const MutualHeartbeatMonitor &monitor)
{
monitor.stats(os);
return os;
}
};