made the client-side ping-out logic more badass.

This commit is contained in:
jesopo 2016-04-10 17:29:03 +01:00
parent eac1d127bd
commit 520a6ecd08
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3

View file

@ -13,7 +13,6 @@ class Bot(object):
self.modules = ModuleManager.ModuleManager(self) self.modules = ModuleManager.ModuleManager(self)
self.events = EventManager.EventHook(self) self.events = EventManager.EventHook(self)
self.timers = [] self.timers = []
self.last_ping = None
def add_server(self, id, hostname, port, password, ipv4, tls, def add_server(self, id, hostname, port, password, ipv4, tls,
nickname, username, realname, connect=False): nickname, username, realname, connect=False):
@ -63,7 +62,8 @@ class Bot(object):
select.EPOLLIN|select.EPOLLOUT) select.EPOLLIN|select.EPOLLOUT)
def since_last_read(self, server): def since_last_read(self, server):
return time.time()-server.last_read return None if not server.last_read else time.time(
)-server.last_read
def disconnect(self, server): def disconnect(self, server):
self.poll.unregister(server.fileno()) self.poll.unregister(server.fileno())
@ -95,15 +95,17 @@ class Bot(object):
self.register_read(server) self.register_read(server)
elif event & select.EPULLHUP: elif event & select.EPULLHUP:
print("hangup") print("hangup")
if not self.last_ping or time.time()-self.last_ping >= 60: server.disconnect()
for server in self.servers.values():
server.send_ping()
self.last_ping = time.time()
for server in list(self.servers.values()): for server in list(self.servers.values()):
if server.last_read and self.since_last_read(server since_last_read = self.since_last_read(server)
) > 160: if since_last_read:
if since_last_read > 120:
print("pingout from %s" % str(server)) print("pingout from %s" % str(server))
server.disconnect() server.disconnect()
elif since_last_read > 30 and not server.ping_sent:
server.send_ping()
server.ping_sent = True
if not server.connected: if not server.connected:
self.disconnect(server) self.disconnect(server)