Fixed issue #41 memory leak on metrics collectors
This commit is contained in:
+13
-10
@@ -15,25 +15,28 @@
|
|||||||
*/
|
*/
|
||||||
package org.eclipse.moquette.server.netty.metrics;
|
package org.eclipse.moquette.server.netty.metrics;
|
||||||
|
|
||||||
import java.util.Queue;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collects all the metrics from the various pipeline.
|
* Collects all the metrics from the various pipeline.
|
||||||
*/
|
*/
|
||||||
public class BytesMetricsCollector {
|
public class BytesMetricsCollector {
|
||||||
private Queue<BytesMetrics> m_allMetrics = new ConcurrentLinkedQueue<BytesMetrics>();
|
|
||||||
|
|
||||||
void addMetrics(BytesMetrics metrics) {
|
private AtomicLong readBytes = new AtomicLong();
|
||||||
m_allMetrics.add(metrics);
|
private AtomicLong wroteBytes = new AtomicLong();
|
||||||
}
|
|
||||||
|
|
||||||
public BytesMetrics computeMetrics() {
|
public BytesMetrics computeMetrics() {
|
||||||
BytesMetrics allMetrics = new BytesMetrics();
|
BytesMetrics allMetrics = new BytesMetrics();
|
||||||
for (BytesMetrics m : m_allMetrics) {
|
allMetrics.incrementRead(readBytes.get());
|
||||||
allMetrics.incrementRead(m.readBytes());
|
allMetrics.incrementWrote(wroteBytes.get());
|
||||||
allMetrics.incrementWrote(m.wroteBytes());
|
|
||||||
}
|
|
||||||
return allMetrics;
|
return allMetrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sumReadBytes(long count) {
|
||||||
|
readBytes.getAndAdd(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sumWroteBytes(long count) {
|
||||||
|
wroteBytes.getAndAdd(count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -59,7 +59,8 @@ public class BytesMetricsHandler extends ChannelDuplexHandler {
|
|||||||
public void close(ChannelHandlerContext ctx,
|
public void close(ChannelHandlerContext ctx,
|
||||||
ChannelPromise promise) throws Exception {
|
ChannelPromise promise) throws Exception {
|
||||||
BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
||||||
m_collector.addMetrics(metrics);
|
m_collector.sumReadBytes(metrics.readBytes());
|
||||||
|
m_collector.sumWroteBytes(metrics.wroteBytes());
|
||||||
super.close(ctx, promise);
|
super.close(ctx, promise);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-9
@@ -17,23 +17,27 @@ package org.eclipse.moquette.server.netty.metrics;
|
|||||||
|
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collects all the metrics from the various pipeline.
|
* Collects all the metrics from the various pipeline.
|
||||||
*/
|
*/
|
||||||
public class MessageMetricsCollector {
|
public class MessageMetricsCollector {
|
||||||
private Queue<MessageMetrics> m_allMetrics = new ConcurrentLinkedQueue<MessageMetrics>();
|
private AtomicLong readMsgs = new AtomicLong();
|
||||||
|
private AtomicLong wroteMsgs = new AtomicLong();
|
||||||
void addMetrics(MessageMetrics metrics) {
|
|
||||||
m_allMetrics.add(metrics);
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessageMetrics computeMetrics() {
|
public MessageMetrics computeMetrics() {
|
||||||
MessageMetrics allMetrics = new MessageMetrics();
|
MessageMetrics allMetrics = new MessageMetrics();
|
||||||
for (MessageMetrics m : m_allMetrics) {
|
allMetrics.incrementRead(readMsgs.get());
|
||||||
allMetrics.incrementRead(m.messagesRead());
|
allMetrics.incrementWrote(wroteMsgs.get());
|
||||||
allMetrics.incrementWrote(m.messagesWrote());
|
|
||||||
}
|
|
||||||
return allMetrics;
|
return allMetrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sumReadMessages(long count) {
|
||||||
|
readMsgs.getAndAdd(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sumWroteMessages(long count) {
|
||||||
|
wroteMsgs.getAndAdd(count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -58,7 +58,8 @@ public class MessageMetricsHandler extends ChannelDuplexHandler {
|
|||||||
public void close(ChannelHandlerContext ctx,
|
public void close(ChannelHandlerContext ctx,
|
||||||
ChannelPromise promise) throws Exception {
|
ChannelPromise promise) throws Exception {
|
||||||
MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
||||||
m_collector.addMetrics(metrics);
|
m_collector.sumReadMessages(metrics.messagesRead());
|
||||||
|
m_collector.sumWroteMessages(metrics.messagesWrote());
|
||||||
super.close(ctx, promise);
|
super.close(ctx, promise);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user