Implement bandwidth meter for remote recording
This commit is contained in:
parent
a4ceb2f513
commit
583c4ec722
|
@ -10,7 +10,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||
|
||||
public class BandwidthMeter {
|
||||
|
||||
private static final Duration MEASURE_TIMEFRAME = Duration.ofSeconds(10);
|
||||
public static final Duration MEASURE_TIMEFRAME = Duration.ofSeconds(10);
|
||||
private static List<Record> records = new ArrayList<>(100);
|
||||
private static Lock lock = new ReentrantLock(true);
|
||||
private static List<Listener> listeners = new ArrayList<>();
|
||||
|
@ -30,14 +30,28 @@ public class BandwidthMeter {
|
|||
|
||||
Instant oneSecondAgo = Instant.now().minus(Duration.ofSeconds(1));
|
||||
if (lastUpdate.isBefore(oneSecondAgo)) {
|
||||
long throughput = getThroughput();
|
||||
for (Listener listener : listeners) {
|
||||
listener.bandwidthCalculated(throughput, MEASURE_TIMEFRAME);
|
||||
}
|
||||
fireEvent(getThroughput(), MEASURE_TIMEFRAME);
|
||||
lastUpdate = Instant.now();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setThroughput(long bytes, Duration d) {
|
||||
lock.lock();
|
||||
try {
|
||||
records.clear();
|
||||
records.add(new Record(bytes));
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
fireEvent(bytes, d);
|
||||
}
|
||||
|
||||
private static void fireEvent(long throughput, Duration timeframe) {
|
||||
for (Listener listener : listeners) {
|
||||
listener.bandwidthCalculated(throughput, timeframe);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the throughput over the last 10 seconds
|
||||
* @return throughput in bytes
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.io.IOException;
|
|||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
@ -24,6 +25,7 @@ import ctbrec.Model;
|
|||
import ctbrec.Recording;
|
||||
import ctbrec.event.EventBusHolder;
|
||||
import ctbrec.event.RecordingStateChangedEvent;
|
||||
import ctbrec.io.BandwidthMeter;
|
||||
import ctbrec.io.HttpClient;
|
||||
import ctbrec.io.HttpException;
|
||||
import ctbrec.io.InstantJsonAdapter;
|
||||
|
@ -208,6 +210,9 @@ public class RemoteRecorder implements Recorder {
|
|||
JSONObject resp = new JSONObject(json);
|
||||
spaceTotal = resp.getLong("spaceTotal");
|
||||
spaceFree = resp.getLong("spaceFree");
|
||||
long throughput = resp.getLong("throughput");
|
||||
Duration timeframe = Duration.ofSeconds(resp.getInt("throughputTimeframe"));
|
||||
BandwidthMeter.setThroughput(throughput, timeframe);
|
||||
} else {
|
||||
LOG.error(COULDNT_SYNCHRONIZE_WITH_SERVER_HTTP_STATUS, response.code(), json);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue