From 3905f17f498d4fa834a407ff9ae020eb9ba7c152 Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Sat, 19 Mar 2016 08:41:10 -0700 Subject: [PATCH] Use requests directly, instead of sseclient. This will hopefully resolve some of the issues seen with SSE mode (see https://github.com/mesosphere/marathon-lb/issues/35). --- marathon_lb.py | 24 +++++++++++++++++++++--- requirements.txt | 1 - 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/marathon_lb.py b/marathon_lb.py index 6b370570..92ee492a 100755 --- a/marathon_lb.py +++ b/marathon_lb.py @@ -48,7 +48,6 @@ from tempfile import mkstemp from textwrap import dedent from wsgiref.simple_server import make_server -from sseclient import SSEClient from six.moves.urllib import parse from itertools import cycle from common import * @@ -649,7 +648,26 @@ def get_event_stream(self): url = self.host+"/v2/events" logger.info( "SSE Active, trying fetch events from from {0}".format(url)) - return SSEClient(url, auth=self.__auth) + + headers = { + 'Cache-Control': 'no-cache', + 'Accept': 'text/event-stream' + } + + resp = requests.get(url, stream=True, + headers=headers, auth=self.__auth) + + class Event(object): + def __init__(self, data): + self.data = data + + for line in resp.iter_lines(): + if line.strip() != '': + for real_event_data in re.split(r'\r\n', + line.decode('utf-8')): + if real_event_data[:6] == "data: ": + event = Event(data=real_event_data[6:]) + yield event @property def host(self): @@ -1621,10 +1639,10 @@ def process_sse_events(marathon, config_file, groups, args.ssl_certs) except: logger.exception("Caught exception") - logger.error("Reconnecting...") backoff = backoff * 1.5 if backoff > 300: backoff = 300 + logger.error("Reconnecting in {}s...", backoff) # Reset the backoff if it's been more than 10 minutes if time.time() - stream_started > 600: backoff = 3 diff --git a/requirements.txt b/requirements.txt index 40214b6f..539fa3e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ requests six -sseclient python-dateutil