Commit c29d2b1b authored by Dave Syer's avatar Dave Syer

Remove a small race condition

parent 894940d7
...@@ -35,22 +35,23 @@ public class InMemoryMetricRepository implements MetricRepository { ...@@ -35,22 +35,23 @@ public class InMemoryMetricRepository implements MetricRepository {
@Override @Override
public void increment(String metricName, int amount, Date timestamp) { public void increment(String metricName, int amount, Date timestamp) {
Measurement current = this.metrics.get(metricName);
if (current != null) {
Object lock = this.locks.putIfAbsent(metricName, new Object()); Object lock = this.locks.putIfAbsent(metricName, new Object());
if (lock == null) { if (lock == null) {
lock = this.locks.get(metricName); lock = this.locks.get(metricName);
} }
synchronized (lock) { synchronized (lock) {
current = this.metrics.get(metricName); Measurement current = this.metrics.get(metricName);
if (current != null) {
Metric metric = current.getMetric(); Metric metric = current.getMetric();
this.metrics.replace(metricName, current, new Measurement(timestamp, this.metrics.replace(metricName, current, new Measurement(timestamp,
metric.increment(amount))); metric.increment(amount)));
return; return;
} }
else {
this.metrics.putIfAbsent(metricName, new Measurement(timestamp,
new Metric(metricName, amount)));
}
} }
this.metrics.putIfAbsent(metricName, new Measurement(timestamp, new Metric(
metricName, amount)));
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment