mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Measure stability of each connection separately.
This should reduce the impact of connections that fail at the remote end.
This commit is contained in:
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
|||||||
import org.briarproject.bramble.api.sync.event.CloseSyncConnectionsEvent;
|
import org.briarproject.bramble.api.sync.event.CloseSyncConnectionsEvent;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@@ -29,14 +30,11 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
|
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private final List<DuplexTransportConnection> connections =
|
private final List<ConnectionRecord> connections = new LinkedList<>();
|
||||||
new LinkedList<>();
|
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private boolean keyAgreementInProgress = false;
|
private boolean keyAgreementInProgress = false;
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private int connectionLimit = 2;
|
private int connectionLimit = 2;
|
||||||
@GuardedBy("lock")
|
|
||||||
private long timeOfLastChange = 0;
|
|
||||||
|
|
||||||
BluetoothConnectionLimiterImpl(EventBus eventBus, Clock clock) {
|
BluetoothConnectionLimiterImpl(EventBus eventBus, Clock clock) {
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
@@ -84,8 +82,7 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
countStableConnections(now);
|
countStableConnections(now);
|
||||||
connections.add(conn);
|
connections.add(new ConnectionRecord(conn, now));
|
||||||
timeOfLastChange = now;
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Connection opened, " + connections.size() + " open");
|
LOG.info("Connection opened, " + connections.size() + " open");
|
||||||
}
|
}
|
||||||
@@ -96,11 +93,15 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
public void connectionClosed(DuplexTransportConnection conn,
|
public void connectionClosed(DuplexTransportConnection conn,
|
||||||
boolean exception) {
|
boolean exception) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
long now = clock.currentTimeMillis();
|
|
||||||
if (exception) LOG.info("Connection failed");
|
if (exception) LOG.info("Connection failed");
|
||||||
countStableConnections(now);
|
countStableConnections(clock.currentTimeMillis());
|
||||||
connections.remove(conn);
|
Iterator<ConnectionRecord> it = connections.iterator();
|
||||||
timeOfLastChange = now;
|
while (it.hasNext()) {
|
||||||
|
if (it.next().conn == conn) {
|
||||||
|
it.remove();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Connection closed, " + connections.size() + " open");
|
LOG.info("Connection closed, " + connections.size() + " open");
|
||||||
}
|
}
|
||||||
@@ -113,19 +114,33 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
countStableConnections(now);
|
countStableConnections(now);
|
||||||
connections.clear();
|
connections.clear();
|
||||||
timeOfLastChange = now;
|
|
||||||
LOG.info("All connections closed");
|
LOG.info("All connections closed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private void countStableConnections(long now) {
|
private void countStableConnections(long now) {
|
||||||
if (now - timeOfLastChange >= STABILITY_PERIOD_MS &&
|
int stable = 0;
|
||||||
connections.size() > connectionLimit) {
|
for (ConnectionRecord rec : connections) {
|
||||||
connectionLimit = connections.size();
|
if (now - rec.timeOpened >= STABILITY_PERIOD_MS) stable++;
|
||||||
|
}
|
||||||
|
if (stable > connectionLimit) {
|
||||||
|
connectionLimit = stable;
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Raising connection limit to " + connectionLimit);
|
LOG.info("Raising connection limit to " + connectionLimit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class ConnectionRecord {
|
||||||
|
|
||||||
|
private final DuplexTransportConnection conn;
|
||||||
|
private final long timeOpened;
|
||||||
|
|
||||||
|
private ConnectionRecord(DuplexTransportConnection conn,
|
||||||
|
long timeOpened) {
|
||||||
|
this.conn = conn;
|
||||||
|
this.timeOpened = timeOpened;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user