mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +01:00
Impose limit on outgoing connections.
This commit is contained in:
@@ -34,6 +34,8 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private boolean keyAgreementInProgress = false;
|
private boolean keyAgreementInProgress = false;
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
|
private int connectionLimit = 2;
|
||||||
|
@GuardedBy("lock")
|
||||||
private long timeOfLastChange = 0;
|
private long timeOfLastChange = 0;
|
||||||
|
|
||||||
BluetoothConnectionLimiterImpl(EventBus eventBus, Clock clock) {
|
BluetoothConnectionLimiterImpl(EventBus eventBus, Clock clock) {
|
||||||
@@ -64,9 +66,15 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
if (keyAgreementInProgress) {
|
if (keyAgreementInProgress) {
|
||||||
LOG.info("Can't open contact connection during key agreement");
|
LOG.info("Can't open contact connection during key agreement");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
}
|
||||||
|
long now = clock.currentTimeMillis();
|
||||||
|
countStableConnections(now);
|
||||||
|
if (connections.size() < connectionLimit) {
|
||||||
LOG.info("Can open contact connection");
|
LOG.info("Can open contact connection");
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
LOG.info("Can't open contact connection due to limit");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +97,8 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
boolean exception) {
|
boolean exception) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
if (!exception) countStableConnections(now);
|
if (exception) LOG.info("Connection failed");
|
||||||
|
else countStableConnections(now);
|
||||||
connections.remove(conn);
|
connections.remove(conn);
|
||||||
timeOfLastChange = now;
|
timeOfLastChange = now;
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
@@ -109,10 +118,13 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GuardedBy("lock")
|
||||||
private void countStableConnections(long now) {
|
private void countStableConnections(long now) {
|
||||||
if (now - timeOfLastChange >= STABILITY_PERIOD_MS) {
|
if (now - timeOfLastChange >= STABILITY_PERIOD_MS &&
|
||||||
|
connections.size() > connectionLimit) {
|
||||||
|
connectionLimit = connections.size();
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info(connections.size() + " connections are stable");
|
LOG.info("Raising connection limit to " + connectionLimit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user