From 06a7a875fe2705044b98f3ef285944b5513be7de Mon Sep 17 00:00:00 2001 From: codl Date: Sun, 8 Apr 2018 23:06:09 +0200 Subject: [PATCH 1/2] add timeouts to streams (GH-127) --- mastodon/Mastodon.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 8b7064f..ea54649 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -89,6 +89,7 @@ class Mastodon: """ __DEFAULT_BASE_URL = 'https://mastodon.social' __DEFAULT_TIMEOUT = 300 + __DEFAULT_STREAM_TIMEOUT = 300 __SUPPORTED_MASTODON_VERSION = "2.2.0" ### @@ -1395,37 +1396,37 @@ class Mastodon: return self.__stream('/api/v1/streaming/user', listener, async=async) @api_version("1.1.0", "1.4.2") - def stream_public(self, listener, async=False): + def stream_public(self, listener, async=False, timeout=__DEFAULT_STREAM_TIMEOUT): """ Streams public events. """ - return self.__stream('/api/v1/streaming/public', listener, async=async) + return self.__stream('/api/v1/streaming/public', listener, async=async, timeout=timeout) @api_version("1.1.0", "1.4.2") - def stream_local(self, listener, async=False): + def stream_local(self, listener, async=False, timeout=__DEFAULT_STREAM_TIMEOUT): """ Streams local public events. """ - return self.__stream('/api/v1/streaming/public/local', listener, async=async) + return self.__stream('/api/v1/streaming/public/local', listener, async=async, timeout=timeout) @api_version("1.1.0", "1.4.2") - def stream_hashtag(self, tag, listener, async=False): + def stream_hashtag(self, tag, listener, async=False, timeout=__DEFAULT_STREAM_TIMEOUT): """ Stream for all public statuses for the hashtag 'tag' seen by the connected instance. """ if tag.startswith("#"): raise MastodonIllegalArgumentError("Tag parameter should omit leading #") - return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener, async=async) + return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener, async=async, timeout=timeout) @api_version("2.1.0", "2.1.0") - def stream_list(self, id, listener, async=False): + def stream_list(self, id, listener, async=False, timeout=__DEFAULT_STREAM_TIMEOUT): """ Stream events for the current user, restricted to accounts on the given list. """ id = self.__unpack_id(id) - return self.__stream("/api/v1/streaming/list?list={}".format(id), listener, async=async) + return self.__stream("/api/v1/streaming/list?list={}".format(id), listener, async=async, timeout=timeout) ### # Internal helpers, dragons probably @@ -1667,7 +1668,7 @@ class Mastodon: return response - def __stream(self, endpoint, listener, params={}, async=False): + def __stream(self, endpoint, listener, params={}, async=False, timeout=__DEFAULT_STREAM_TIMEOUT): """ Internal streaming API helper. @@ -1697,7 +1698,8 @@ class Mastodon: url = url[:-1] headers = {"Authorization": "Bearer " + self.access_token} - connection = requests.get(url + endpoint, headers = headers, data = params, stream = True) + connection = requests.get(url + endpoint, headers = headers, data = params, stream = True, + timeout=(self.request_timeout, timeout)) if connection.status_code != 200: raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason) From 06e32c14bcb5a1ef1a5e618a1b413ea011416c9d Mon Sep 17 00:00:00 2001 From: codl Date: Thu, 19 Apr 2018 17:10:42 +0200 Subject: [PATCH 2/2] raise MastodonReadTimeout when a stream times out --- mastodon/Mastodon.py | 4 ++++ mastodon/streaming.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 5461661..49597ca 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1872,6 +1872,10 @@ class MastodonNetworkError(MastodonIOError): """Raised when network communication with the server fails""" pass +class MastodonReadTimeout(MastodonNetworkError): + """Raised when a stream times out""" + pass + class MastodonAPIError(MastodonError): """Raised when the mastodon API generates a response that cannot be handled""" diff --git a/mastodon/streaming.py b/mastodon/streaming.py index 1c73f48..3fbd569 100644 --- a/mastodon/streaming.py +++ b/mastodon/streaming.py @@ -6,8 +6,8 @@ https://github.com/tootsuite/mastodon/blob/master/docs/Using-the-API/Streaming-A import json import six from mastodon import Mastodon -from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError -from requests.exceptions import ChunkedEncodingError +from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError, MastodonReadTimeout +from requests.exceptions import ChunkedEncodingError, ReadTimeout class StreamListener(object): """Callbacks for the streaming API. Create a subclass, override the on_xxx @@ -68,7 +68,12 @@ class StreamListener(object): MastodonNetworkError("Server ceased communication."), err ) - + except MastodonReadTimeout as err: + six.raise_from( + MastodonReadTimeout("Timed out while reading from server."), + err + ) + def _parse_line(self, line, event): if line.startswith(':'): self.handle_heartbeat()