Commit 3315d5a1 authored by Scott Barr's avatar Scott Barr
Browse files

SSL related changes submitted by @almost in pull request tweepy/132

This commit contains the SSL related changes submitted by @almost in
tweepy/132

I didn't include the delimited changes because this change has not been
merged upstream yet, maybe because the changes are not separate.
parent 517a0f25
...@@ -7,6 +7,7 @@ import httplib ...@@ -7,6 +7,7 @@ import httplib
from socket import timeout from socket import timeout
from threading import Thread from threading import Thread
from time import sleep from time import sleep
import ssl
from tweepy.models import Status from tweepy.models import Status
from tweepy.api import API from tweepy.api import API
...@@ -61,6 +62,10 @@ class StreamListener(object): ...@@ -61,6 +62,10 @@ class StreamListener(object):
"""Called when a new status arrives""" """Called when a new status arrives"""
return return
def on_exception(self, exception):
"""Called when an unhandled exception occurs."""
return
def on_delete(self, status_id, user_id): def on_delete(self, status_id, user_id):
"""Called when a delete notice arrives for a status""" """Called when a delete notice arrives for a status"""
return return
...@@ -96,8 +101,10 @@ class Stream(object): ...@@ -96,8 +101,10 @@ class Stream(object):
self.running = False self.running = False
self.timeout = options.get("timeout", 300.0) self.timeout = options.get("timeout", 300.0)
self.retry_count = options.get("retry_count") self.retry_count = options.get("retry_count")
self.retry_time = options.get("retry_time", 10.0) self.retry_time_start = options.get("retry_time", 10.0)
self.snooze_time = options.get("snooze_time", 5.0) self.retry_time_cap = options.get("retry_time_cap", 240.0)
self.snooze_time_start = options.get("snooze_time", 0.25)
self.snooze_time_cap = options.get("snooze_time_cap", 16)
self.buffer_size = options.get("buffer_size", 1500) self.buffer_size = options.get("buffer_size", 1500)
if options.get("secure", True): if options.get("secure", True):
self.scheme = "https" self.scheme = "https"
...@@ -108,6 +115,8 @@ class Stream(object): ...@@ -108,6 +115,8 @@ class Stream(object):
self.headers = options.get("headers") or {} self.headers = options.get("headers") or {}
self.parameters = None self.parameters = None
self.body = None self.body = None
self.retry_time = self.retry_time_start
self.snooze_time = self.snooze_time_start
def _run(self): def _run(self):
# Authenticate # Authenticate
...@@ -135,17 +144,26 @@ class Stream(object): ...@@ -135,17 +144,26 @@ class Stream(object):
break break
error_counter += 1 error_counter += 1
sleep(self.retry_time) sleep(self.retry_time)
self.retry_time = min(self.retry_time * 2, self.retry_time_cap)
else: else:
error_counter = 0 error_counter = 0
self.retry_time = self.retry_time_start
self.snooze_time = self.snooze_time_start
self.listener.on_connect() self.listener.on_connect()
self._read_loop(resp) self._read_loop(resp)
except timeout: except (timeout, ssl.SSLError), exc:
# If it's not time out treat it like any other exception
if isinstance(exc, ssl.SSLError) and not (exc.args and 'timed out' in str(exc.args[0])):
exception = exc
break
if self.listener.on_timeout() == False: if self.listener.on_timeout() == False:
break break
if self.running is False: if self.running is False:
break break
conn.close() conn.close()
sleep(self.snooze_time) sleep(self.snooze_time)
self.snooze_time = min(self.snooze_time+0.25, self.snooze_time_cap)
except Exception, exception: except Exception, exception:
# any other exception is fatal, so kill loop # any other exception is fatal, so kill loop
break break
...@@ -156,6 +174,8 @@ class Stream(object): ...@@ -156,6 +174,8 @@ class Stream(object):
conn.close() conn.close()
if exception: if exception:
# call a handler first so that the exception can be logged.
self.listener.on_exception(exception)
raise raise
def _data(self, data): def _data(self, data):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment