Skip to content

Commit

Permalink
#814 always close socket
Browse files Browse the repository at this point in the history
(cherry picked from commit 2762ce5)
  • Loading branch information
osigida authored and Artem Prigoda committed Jan 5, 2017
1 parent 9ba1053 commit 5a0e903
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.codahale.metrics.graphite;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.SocketFactory;

import java.io.*;
Expand Down Expand Up @@ -27,6 +30,8 @@ public class Graphite implements GraphiteSender {
private Writer writer;
private int failures;

private static final Logger LOGGER = LoggerFactory.getLogger(Graphite.class);

/**
* Creates a new client which connects to the given address using the default
* {@link SocketFactory}.
Expand Down Expand Up @@ -159,12 +164,19 @@ public void close() throws IOException {
writer.close();
}
} catch (IOException ex) {
LOGGER.debug("Error closing writer", ex);
} finally {
this.writer = null;
}

try {
if (socket != null) {
socket.close();
}
} catch (IOException ex) {
LOGGER.debug("Error closing socket", ex);
} finally {
this.socket = null;
this.writer = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ public void report(SortedMap<String, Gauge> gauges,

// oh it'd be lovely to use Java 7 here
try {
if (!graphite.isConnected()) {
graphite.connect();
}
graphite.connect();

for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
reportGauge(entry.getKey(), entry.getValue(), timestamp);
Expand All @@ -188,10 +186,10 @@ public void report(SortedMap<String, Gauge> gauges,
for (Map.Entry<String, Timer> entry : timers.entrySet()) {
reportTimer(entry.getKey(), entry.getValue(), timestamp);
}

graphite.flush();
} catch (IOException e) {
LOGGER.warn("Unable to report to Graphite", graphite, e);
} finally {
try {
graphite.close();
} catch (IOException e1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public void doesNotReportStringGaugeValues() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite, never()).send("prefix.gauge", "value", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -56,10 +56,10 @@ public void reportsByteGaugeValues() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.gauge", "1", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -73,10 +73,10 @@ public void reportsShortGaugeValues() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.gauge", "1", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -90,10 +90,10 @@ public void reportsIntegerGaugeValues() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.gauge", "1", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -107,10 +107,10 @@ public void reportsLongGaugeValues() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.gauge", "1", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -124,10 +124,10 @@ public void reportsFloatGaugeValues() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.gauge", "1.10", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -141,10 +141,10 @@ public void reportsDoubleGaugeValues() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.gauge", "1.10", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -161,10 +161,10 @@ public void reportsCounters() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.counter.count", "100", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand Down Expand Up @@ -195,7 +195,6 @@ public void reportsHistograms() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.histogram.count", "1", timestamp);
inOrder.verify(graphite).send("prefix.histogram.max", "2", timestamp);
Expand All @@ -209,6 +208,7 @@ public void reportsHistograms() throws Exception {
inOrder.verify(graphite).send("prefix.histogram.p99", "10.00", timestamp);
inOrder.verify(graphite).send("prefix.histogram.p999", "11.00", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -229,14 +229,14 @@ public void reportsMeters() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.meter.count", "1", timestamp);
inOrder.verify(graphite).send("prefix.meter.m1_rate", "2.00", timestamp);
inOrder.verify(graphite).send("prefix.meter.m5_rate", "3.00", timestamp);
inOrder.verify(graphite).send("prefix.meter.m15_rate", "4.00", timestamp);
inOrder.verify(graphite).send("prefix.meter.mean_rate", "5.00", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand Down Expand Up @@ -272,7 +272,6 @@ public void reportsTimers() throws Exception {
map("timer", timer));

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).send("prefix.timer.max", "100.00", timestamp);
inOrder.verify(graphite).send("prefix.timer.mean", "200.00", timestamp);
Expand All @@ -290,6 +289,7 @@ public void reportsTimers() throws Exception {
inOrder.verify(graphite).send("prefix.timer.m15_rate", "5.00", timestamp);
inOrder.verify(graphite).send("prefix.timer.mean_rate", "2.00", timestamp);
inOrder.verify(graphite).flush();
inOrder.verify(graphite).close();

verifyNoMoreInteractions(graphite);
}
Expand All @@ -304,10 +304,10 @@ public void closesConnectionIfGraphiteIsUnavailable() throws Exception {
this.<Timer>map());

final InOrder inOrder = inOrder(graphite);
inOrder.verify(graphite).isConnected();
inOrder.verify(graphite).connect();
inOrder.verify(graphite).close();


verifyNoMoreInteractions(graphite);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void disconnectsFromGraphite() throws Exception {
graphite.connect();
graphite.close();

verify(socket).close();
verify(socket, times(2)).close();
}

@Test
Expand Down

0 comments on commit 5a0e903

Please sign in to comment.