Skip to content

Commit

Permalink
Use requests directly, instead of sseclient.
Browse files Browse the repository at this point in the history
This will hopefully resolve some of the issues seen with SSE mode (see
d2iq-archive#35).
  • Loading branch information
brndnmtthws committed Mar 19, 2016
1 parent 491f777 commit bb4ae4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 21 additions & 3 deletions marathon_lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -650,7 +649,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):
Expand Down Expand Up @@ -1622,10 +1640,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
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
requests
six
sseclient
python-dateutil

0 comments on commit bb4ae4b

Please sign in to comment.