Improved performance and network stability

This commit is contained in:
Christian Basler 2016-01-19 21:09:46 +01:00
parent e29310102f
commit 733335ef42
3 changed files with 19 additions and 11 deletions

View File

@ -139,6 +139,9 @@ class Connection {
@SuppressWarnings("RedundantIfStatement")
private boolean syncFinished(NetworkMessage msg) {
if (mode != SYNC){
return false;
}
if (Thread.interrupted()) {
return true;
}
@ -239,11 +242,6 @@ class Connection {
} finally {
if (commonRequestedObjects.remove(objectMessage.getInventoryVector())) {
LOG.debug("Received object that wasn't requested.");
// if (!requestedObjects.isEmpty()) {
// DebugUtils.saveToFile(objectMessage);
// LOG.debug(objectMessage.getInventoryVector() + " was not in "
// + requestedObjects.toString());
// }
}
}
break;

View File

@ -43,16 +43,16 @@ import static ch.dissem.bitmessage.networking.Connection.Mode.CLIENT;
import static ch.dissem.bitmessage.networking.Connection.Mode.SERVER;
import static ch.dissem.bitmessage.networking.Connection.State.ACTIVE;
import static ch.dissem.bitmessage.utils.DebugUtils.inc;
import static ch.dissem.bitmessage.utils.UnixTime.MINUTE;
import static java.util.Collections.newSetFromMap;
/**
* Handles all the networky stuff.
*/
public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
public final static int NETWORK_MAGIC_NUMBER = 8;
private final static Logger LOG = LoggerFactory.getLogger(DefaultNetworkHandler.class);
private static final Random RANDOM = new Random();
public final static int NETWORK_MAGIC_NUMBER = 8;
private final Collection<Connection> connections = new ConcurrentLinkedQueue<>();
private final ExecutorService pool;
private InternalContext ctx;
@ -155,12 +155,14 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
if (diff == 0) break;
}
}
boolean forcedDisconnect = false;
for (Iterator<Connection> iterator = connections.iterator(); iterator.hasNext(); ) {
Connection c = iterator.next();
// Just in case they were all created at the same time, don't disconnect
// all at once.
if (now - c.getStartTime() + RANDOM.nextInt(5 * MINUTE) > ctx.getConnectionTTL()) {
if (!forcedDisconnect && now - c.getStartTime() > ctx.getConnectionTTL()) {
c.disconnect();
forcedDisconnect = true;
}
switch (c.getState()) {
case DISCONNECTED:
@ -306,8 +308,13 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
}
}
Iterator<InventoryVector> iterator = inventoryVectors.iterator();
InventoryVector next;
if (iterator.hasNext()) {
next = iterator.next();
} else {
return;
}
boolean firstRound = true;
InventoryVector next = iterator.next();
while (firstRound || iterator.hasNext()) {
if (!firstRound) {
next = iterator.next();

View File

@ -80,7 +80,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
@Override
public List<InventoryVector> getMissing(List<InventoryVector> offer, long... streams) {
for (long stream : streams) {
getCache(stream).forEach((iv, t) -> offer.remove(iv));
offer.removeAll(getCache(stream).keySet());
}
return offer;
}
@ -132,6 +132,9 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
@Override
public void storeObject(ObjectMessage object) {
if (getCache(object.getStream()).containsKey(object.getInventoryVector()))
return;
try (Connection connection = config.getConnection()) {
PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory (hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)");
InventoryVector iv = object.getInventoryVector();