From 40168879e2d4e6a06fcaa0363b79c0841ac4f075 Mon Sep 17 00:00:00 2001 From: Eli Bishop Date: Mon, 30 Mar 2020 14:47:20 -0700 Subject: [PATCH] streams shouldn't use the same read timeout as the rest of the SDK (#132) --- ldclient/impl/http.py | 11 +++++++++-- ldclient/sse_client.py | 4 +--- ldclient/streaming.py | 9 ++++++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/ldclient/impl/http.py b/ldclient/impl/http.py index eaa82077..bcc97e4e 100644 --- a/ldclient/impl/http.py +++ b/ldclient/impl/http.py @@ -17,15 +17,22 @@ def _http_factory(config): return HTTPFactory(_base_headers(config), config.http) class HTTPFactory(object): - def __init__(self, base_headers, http_config): + def __init__(self, base_headers, http_config, override_read_timeout=None): self.__base_headers = base_headers self.__http_config = http_config - self.__timeout = urllib3.Timeout(connect=http_config.connect_timeout, read=http_config.read_timeout) + self.__timeout = urllib3.Timeout( + connect=http_config.connect_timeout, + read=http_config.read_timeout if override_read_timeout is None else override_read_timeout + ) @property def base_headers(self): return self.__base_headers + @property + def http_config(self): + return self.__http_config + @property def timeout(self): return self.__timeout diff --git a/ldclient/sse_client.py b/ldclient/sse_client.py index b257a443..eca088f6 100644 --- a/ldclient/sse_client.py +++ b/ldclient/sse_client.py @@ -28,8 +28,6 @@ def __init__(self, url, last_id=None, retry=3000, connect_timeout=10, read_timeo self.url = url self.last_id = last_id self.retry = retry - self._connect_timeout = connect_timeout - self._read_timeout = read_timeout self._chunk_size = chunk_size if http_factory: @@ -37,7 +35,7 @@ def __init__(self, url, last_id=None, retry=3000, connect_timeout=10, read_timeo base_headers = http_factory.base_headers else: # for backward compatibility in case anyone else is using this class - self._timeout = urllib3.Timeout(connect=self._connect_timeout, read=self._read_timeout) + self._timeout = urllib3.Timeout(connect=connect_timeout, read=read_timeout) base_headers = {} # Optional support for passing in an HTTP client diff --git a/ldclient/streaming.py b/ldclient/streaming.py index 7e0fd52b..abc54247 100644 --- a/ldclient/streaming.py +++ b/ldclient/streaming.py @@ -12,7 +12,7 @@ import math import time -from ldclient.impl.http import _http_factory +from ldclient.impl.http import HTTPFactory, _http_factory from ldclient.impl.retry_delay import RetryDelayStrategy, DefaultBackoffStrategy, DefaultJitterStrategy from ldclient.interfaces import UpdateProcessor from ldclient.sse_client import SSEClient @@ -75,8 +75,8 @@ def run(self): messages = self._connect() for msg in messages: if not self._running: - log.warning("but I'm done") break + self._retry_delay.set_good_since(time.time()) message_ok = self.process_message(self._store, self._requester, msg) if message_ok: self._record_stream_init(False) @@ -104,10 +104,13 @@ def _record_stream_init(self, failed): self._diagnostic_accumulator.record_stream_init(current_time, current_time - self._es_started, failed) def _connect(self): + # We don't want the stream to use the same read timeout as the rest of the SDK. + http_factory = _http_factory(self._config) + stream_http_factory = HTTPFactory(http_factory.base_headers, http_factory.http_config, override_read_timeout=stream_read_timeout) return SSEClient( self._uri, retry = None, # we're implementing our own retry - http_factory = _http_factory(self._config) + http_factory = stream_http_factory ) def stop(self):