Skip to content

Commit

Permalink
[core] improve ping and timeout handling
Browse files Browse the repository at this point in the history
We should reset the timer on every recived message, because if we got a message the connection is working.
Also, 60s is too low, so I doubled it.
Some servers send pings in much larger intervals, so we should send pings ourselves if we didn't recive anything in a while
  • Loading branch information
Elad Alfassa committed May 19, 2013
1 parent 9babfec commit 632b291
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion willie/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init__(self, filename, load=True, ignore_errors=False):
if not self.parser.has_option('core', 'verify_ssl'):
self.parser.set('core', 'verify_ssl', 'True')
if not self.parser.has_option('core', 'timeout'):
self.parser.set('core', 'timeout', '60')
self.parser.set('core', 'timeout', '120')
else:
self.parser.add_section('core')

Expand Down
12 changes: 10 additions & 2 deletions willie/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ def handle_connect(self):
stderr('Connected.')
self.last_ping_time = datetime.now();
timeout_check_thread = threading.Thread(target=self._timeout_check)
timeout_check_thread.start();
timeout_check_thread.start()
ping_thread = threading.Thread(target=self._send_ping)
ping_thread.start()

def _timeout_check(self):
while True:
Expand All @@ -306,6 +308,12 @@ def _timeout_check(self):
else:
time.sleep(int(self.config.timeout))

def _send_ping(self):
while True:
if (datetime.now() - self.last_ping_time).seconds > int(self.config.timeout)/2:
self.write(('PING', self.config.host))
time.sleep(int(self.config.timeout)/2)

def _ssl_send(self, data):
""" Replacement for self.send() during SSL connections. """
try:
Expand Down Expand Up @@ -381,9 +389,9 @@ def found_terminator(self):
args = line.split()
text = args[-1]

self.last_ping_time = datetime.now()
if args[0] == 'PING':
self.write(('PONG', text))
self.last_ping_time = datetime.now();
elif args[0] == 'ERROR':
self.debug('IRC Server Error', text, 'always')
elif args[0] == '433':
Expand Down

0 comments on commit 632b291

Please sign in to comment.