Drop client after 1 minute of not responding for pings

This commit is contained in:
Tomasz Fluxid Kowalczyk 2012-02-15 20:37:58 +01:00
parent 4e8cd4b6b3
commit caaa176247

View File

@ -133,6 +133,7 @@ class WatcherInfo(object):
def __init__(self, watcher_proto, name): def __init__(self, watcher_proto, name):
self.watcher_proto = watcher_proto self.watcher_proto = watcher_proto
self.name = name self.name = name
self.active = True
self.position = 0 self.position = 0
self.filename = None self.filename = None
@ -144,6 +145,7 @@ class WatcherInfo(object):
self.time_offset = 0 self.time_offset = 0
self.time_offset_data = [] self.time_offset_data = []
self.pings_sent = dict() self.pings_sent = dict()
self.last_ping_received = time.time()
self.counter = 0 self.counter = 0
@ -179,6 +181,7 @@ class SyncFactory(Factory):
def remove_watcher(self, watcher_proto): def remove_watcher(self, watcher_proto):
watcher = self.watchers.pop(watcher_proto, None) watcher = self.watchers.pop(watcher_proto, None)
watcher.active = False
for receiver in self.watchers.itervalues(): for receiver in self.watchers.itervalues():
if receiver != watcher: if receiver != watcher:
receiver.watcher_proto.send_left(watcher.name) receiver.watcher_proto.send_left(watcher.name)
@ -281,6 +284,7 @@ class SyncFactory(Factory):
ping_time = watcher.pings_sent.pop(value, None) ping_time = watcher.pings_sent.pop(value, None)
if ping_time is not None: if ping_time is not None:
curtime = time.time() curtime = time.time()
watcher.last_ping_received = curtime
watcher.ping = ping = (curtime - ping_time)/2 watcher.ping = ping = (curtime - ping_time)/2
if watcher.time_offset_data is not None: if watcher.time_offset_data is not None:
@ -305,11 +309,19 @@ class SyncFactory(Factory):
#print watcher.name, 'last ping', watcher.ping, 'time offset %.6f' % watcher.time_offset #print watcher.name, 'last ping', watcher.ping, 'time offset %.6f' % watcher.time_offset
def send_ping_to(self, watcher): def send_ping_to(self, watcher):
if not watcher.active:
return
chars = None chars = None
while not chars or chars in watcher.pings_sent: while not chars or chars in watcher.pings_sent:
chars = random_chars() chars = random_chars()
ctime = time.time() ctime = time.time()
print ctime - watcher.last_ping_received
if ctime - watcher.last_ping_received > 60:
watcher.watcher_proto.drop()
print 'dropped', watcher.name
return
watcher.watcher_proto.send_ping(chars) watcher.watcher_proto.send_ping(chars)
watcher.pings_sent[chars] = time.time() watcher.pings_sent[chars] = time.time()