Better internet outage detection based on last message from connections

This commit is contained in:
HelloZeroNet 2016-03-19 18:14:09 +01:00
parent fcd4253a8d
commit f3c8d5e541
2 changed files with 28 additions and 10 deletions

View file

@ -33,6 +33,7 @@ class ConnectionServer:
self.ip_incoming = {} # Incoming connections from ip in the last minute to avoid connection flood
self.broken_ssl_peer_ids = {} # Peerids of broken ssl connections
self.ips = {} # Connection by ip
self.has_internet = True # Internet outage detection
self.running = True
self.thread_checker = gevent.spawn(self.checkConnections)
@ -170,8 +171,10 @@ class ConnectionServer:
time.sleep(60) # Check every minute
self.ip_incoming = {} # Reset connected ips counter
self.broken_ssl_peer_ids = {} # Reset broken ssl peerids count
last_message_time = 0
for connection in self.connections[:]: # Make a copy
idle = time.time() - max(connection.last_recv_time, connection.start_time, connection.last_message_time)
last_message_time = max(last_message_time, connection.last_message_time)
if connection.unpacker and idle > 30:
# Delete the unpacker if not needed
@ -215,3 +218,21 @@ class ConnectionServer:
elif run_i % 30 == 0:
# Reset bad action counter every 30 min
connection.bad_actions = 0
# Internet outage detection
if time.time() - last_message_time > max(30, 60*5/max(1,len(self.connections)/30)):
# Offline: Last message more than 30-300sec depending on connection number
if self.has_internet:
self.has_internet = False
self.onInternetOffline()
else:
# Online
if not self.has_internet:
self.has_internet = True
self.onInternetOnline()
def onInternetOnline(self):
self.log.info("Internet online")
def onInternetOffline(self):
self.log.info("Internet offline")