Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

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
#35).
  • Loading branch information
brndnmtthws committed Mar 19, 2016
1 parent b2153df commit 3905f17
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 @@ -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):
Expand Down Expand Up @@ -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
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 3905f17

Please sign in to comment.