Connections are now severed after a configurable time (12h by default) or when a limit is exceeded (150 by default)
This commit is contained in:
@ -51,8 +51,10 @@ import static ch.dissem.bitmessage.utils.UnixTime.MINUTE;
|
||||
*/
|
||||
public class Connection {
|
||||
public static final int READ_TIMEOUT = 2000;
|
||||
private final static Logger LOG = LoggerFactory.getLogger(Connection.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Connection.class);
|
||||
private static final int CONNECT_TIMEOUT = 5000;
|
||||
|
||||
private final long startTime;
|
||||
private final ConcurrentMap<InventoryVector, Long> ivCache;
|
||||
private final InternalContext ctx;
|
||||
private final Mode mode;
|
||||
@ -89,6 +91,7 @@ public class Connection {
|
||||
|
||||
private Connection(InternalContext context, Mode mode, MessageListener listener, Socket socket,
|
||||
Map<InventoryVector, Long> requestedObjectsMap, NetworkAddress node, long syncTimeout) {
|
||||
this.startTime = UnixTime.now();
|
||||
this.ctx = context;
|
||||
this.mode = mode;
|
||||
this.state = CONNECTING;
|
||||
@ -109,6 +112,10 @@ public class Connection {
|
||||
timeoutInSeconds);
|
||||
}
|
||||
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public Mode getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
||||
import ch.dissem.bitmessage.ports.NetworkHandler;
|
||||
import ch.dissem.bitmessage.utils.Collections;
|
||||
import ch.dissem.bitmessage.utils.Property;
|
||||
import ch.dissem.bitmessage.utils.UnixTime;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -112,9 +113,21 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
||||
while (running) {
|
||||
try {
|
||||
int active = 0;
|
||||
long now = UnixTime.now();
|
||||
synchronized (connections) {
|
||||
int diff = connections.size() - ctx.getConnectionLimit();
|
||||
if (diff > 0) {
|
||||
for (Connection c : connections) {
|
||||
c.disconnect();
|
||||
diff--;
|
||||
if (diff == 0) break;
|
||||
}
|
||||
}
|
||||
for (Iterator<Connection> iterator = connections.iterator(); iterator.hasNext(); ) {
|
||||
Connection c = iterator.next();
|
||||
if (now - c.getStartTime() > ctx.getConnectionTTL()) {
|
||||
c.disconnect();
|
||||
}
|
||||
if (c.getState() == DISCONNECTED) {
|
||||
// Remove the current element from the iterator and the list.
|
||||
iterator.remove();
|
||||
|
Reference in New Issue
Block a user