Skip to content

Commit 707ca94

Browse files
Merge pull request Netflix#86 from benjchristensen/issue-85
hystrix.stream holds connection open if no metrics
2 parents d8d28e5 + b975eb4 commit 707ca94

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/metrics/eventstream/HystrixMetricsStreamServlet.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,22 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
8585
private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
8686
/* ensure we aren't allowing more connections than we want */
8787
int numberConnections = concurrentConnections.incrementAndGet();
88-
89-
int delay = 500;
90-
try {
91-
String d = request.getParameter("delay");
92-
if (d != null) {
93-
delay = Integer.parseInt(d);
94-
}
95-
} catch (Exception e) {
96-
// ignore if it's not a number
97-
}
98-
9988
HystrixMetricsPoller poller = null;
10089
try {
10190
if (numberConnections > maxConcurrentConnections.get()) {
10291
response.sendError(503, "MaxConcurrentConnections reached: " + maxConcurrentConnections.get());
10392
} else {
10493

94+
int delay = 500;
95+
try {
96+
String d = request.getParameter("delay");
97+
if (d != null) {
98+
delay = Integer.parseInt(d);
99+
}
100+
} catch (Exception e) {
101+
// ignore if it's not a number
102+
}
103+
105104
/* initialize response */
106105
response.setHeader("Content-Type", "text/event-stream");
107106
response.setHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
@@ -118,14 +117,24 @@ private void handleRequest(HttpServletRequest request, HttpServletResponse respo
118117
try {
119118
while (poller.isRunning()) {
120119
List<String> jsonMessages = jsonListener.getJsonMetrics();
121-
for (String json : jsonMessages) {
122-
response.getWriter().println("data: " + json + "\n");
120+
if (jsonMessages.isEmpty()) {
121+
// https://github.com/Netflix/Hystrix/issues/85 hystrix.stream holds connection open if no metrics
122+
// we send a ping to test the connection so that we'll get an IOException if the client has disconnected
123+
response.getWriter().println("ping: \n");
124+
} else {
125+
for (String json : jsonMessages) {
126+
response.getWriter().println("data: " + json + "\n");
127+
}
123128
}
124129
// after outputting all the messages we will flush the stream
125130
response.flushBuffer();
126131
// now wait the 'delay' time
127132
Thread.sleep(delay);
128133
}
134+
} catch (IOException e) {
135+
poller.shutdown();
136+
// debug instead of error as we expect to get these whenever a client disconnects or network issue occurs
137+
logger.debug("IOException while trying to write (generally caused by client disconnecting). Will stop polling.", e);
129138
} catch (Exception e) {
130139
poller.shutdown();
131140
logger.error("Failed to write. Will stop polling.", e);

0 commit comments

Comments
 (0)