forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat.py
36 lines (28 loc) · 1.26 KB
/
heartbeat.py
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
from . import HEARTBEAT_GONE_COUNT, HEARTBEAT_INTERVAL
from .member import Component
class Heartbeat(Component):
def __init__(self, member, clock):
super(Heartbeat, self).__init__(member)
self.running = False
self.last_heard_from = {}
self.peers = None
self.clock = clock
def on_view_change_event(self, slot, view_id, peers):
self.peers = set(peers)
for peer in self.peers:
self.last_heard_from[peer] = self.clock()
if not self.running:
self.heartbeat()
self.running = True
def do_HEARTBEAT(self, sender):
self.last_heard_from[sender] = self.clock()
def heartbeat(self):
# send heartbeats to other nodes
self.send(self.peers, 'HEARTBEAT', sender=self.address)
# determine if any peers are down, and notify if so; note that this
# notification will occur repeatedly until a view change
too_old = self.clock() - HEARTBEAT_GONE_COUNT * HEARTBEAT_INTERVAL
active_peers = set(p for p in self.last_heard_from if self.last_heard_from[p] >= too_old)
if active_peers != self.peers:
self.event('peers_down', down=self.peers - active_peers)
self.set_timer(HEARTBEAT_INTERVAL, self.heartbeat)