3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-28 05:29:25 +01:00

Irc: fix the ping timeout/reconnection logic

- reset connection state and last ping time on reconnect
- move Ping timeout logic to somewhere where it'd actually run - the code for breaking out of the run() loop on a ping timeout could previously only run when a ping timeout hadn't occured yet
- consistently use log.warning() instead of log.warn()

This hopefully fixes PyLink going into a disconnect/reconnect loop sometimes instead of reconnecting properly.
This commit is contained in:
James Lu 2015-08-12 21:31:46 -07:00
parent d3e335edee
commit 18576ad488

15
main.py
View File

@ -20,6 +20,8 @@ class Irc():
def initVars(self): def initVars(self):
# Server, channel, and user indexes to be populated by our protocol module # Server, channel, and user indexes to be populated by our protocol module
self.connected = threading.Event()
self.lastping = time.time()
self.servers = {self.sid: classes.IrcServer(None, self.serverdata['hostname'], internal=True)} self.servers = {self.sid: classes.IrcServer(None, self.serverdata['hostname'], internal=True)}
self.users = {} self.users = {}
self.channels = defaultdict(classes.IrcChannel) self.channels = defaultdict(classes.IrcChannel)
@ -54,7 +56,6 @@ class Irc():
def __init__(self, netname, proto, conf): def __init__(self, netname, proto, conf):
# Initialize some variables # Initialize some variables
self.connected = threading.Event()
self.name = netname.lower() self.name = netname.lower()
self.conf = conf self.conf = conf
self.serverdata = conf['servers'][netname] self.serverdata = conf['servers'][netname]
@ -69,7 +70,6 @@ class Irc():
self.connection_thread = threading.Thread(target = self.connect) self.connection_thread = threading.Thread(target = self.connect)
self.connection_thread.start() self.connection_thread.start()
self.pingTimer = None self.pingTimer = None
self.lastping = time.time()
def connect(self): def connect(self):
ip = self.serverdata["ip"] ip = self.serverdata["ip"]
@ -167,14 +167,15 @@ class Irc():
def run(self): def run(self):
buf = b"" buf = b""
data = b"" data = b""
while (time.time() - self.lastping) < self.pingtimeout: while True:
log.debug('(%s) time_since_last_ping: %s', self.name, (time.time() - self.lastping))
log.debug('(%s) self.pingtimeout: %s', self.name, self.pingtimeout)
data = self.socket.recv(2048) data = self.socket.recv(2048)
buf += data buf += data
if self.connected.is_set() and not data: if self.connected.is_set() and not data:
log.warn('(%s) No data received and self.connected is set; disconnecting!', self.name) log.warning('(%s) No data received and self.connected is set; disconnecting!', self.name)
break return
elif (time.time() - self.lastping) > self.pingtimeout:
log.warning('(%s) Connection timed out.', self.name)
return
while b'\n' in buf: while b'\n' in buf:
line, buf = buf.split(b'\n', 1) line, buf = buf.split(b'\n', 1)
line = line.strip(b'\r') line = line.strip(b'\r')